HDU 5465——Clarke and puzzle——————【树状数组BIT维护前缀和+Nim博弈】

Clarke and puzzle

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 673    Accepted Submission(s): 223

Problem Description

Clarke is a patient with multiple personality disorder. One day, Clarke split into two personality a and b, they are playing a game. 
There is a n∗m matrix, each grid of this matrix has a number ci,j. 
a wants to beat b every time, so a ask you for a help. 
There are q operations, each of them is belonging to one of the following two types: 
1. They play the game on a (x1,y1)−(x2,y2) sub matrix. They take turns operating. On any turn, the player can choose a grid which has a positive integer from the sub matrix and decrease it by a positive integer which less than or equal this grid‘s number. The player who can‘t operate is loser. a always operate first, he wants to know if he can win this game. 
2. Change ci,j to b.

Input

The first line contains a integer T(1≤T≤5), the number of test cases. 
For each test case: 
The first line contains three integers n,m,q(1≤n,m≤500,1≤q≤2∗105) 
Then n∗m matrix follow, the i row j column is a integer ci,j(0≤ci,j≤109) 
Then q lines follow, the first number is opt. 
if opt=1, then 4 integers x1,y1,x1,y2(1≤x1≤x2≤n,1≤y1≤y2≤m) follow, represent operation 1. 
if opt=2, then 3 integers i,j,b follow, represent operation 2.

Output

For each testcase, for each operation 1, print Yes if a can win this game, otherwise print No.

Sample Input

1

1 2 3

1 2

1 1 1 1 2

2 1 2 1

1 1 1 1 2

Sample Output

Yes

No

Hint:
The first enquiry: $a$ can decrease grid $(1, 2)$‘s number by $1$. No matter what $b$ operate next, there is always one grid with number $1$ remaining . So, $a$ wins.
The second enquiry: No matter what $a$ operate, there is always one grid with number $1$ remaining. So, $b$ wins.

Source

BestCoder Round #56 (div.2)

题目大意:给你t组测试数据。每组测试数据中有一组n,m,q,分别表示有一个n*m的矩阵,有q组询问。每组询问中,操作1表示查询(x1,y1) ---(x2,y2)在矩阵中做Nim游戏先手是否会赢。操作2表示要把矩阵中某个值修改为c。

知识补充:对于一维树状数组见的比较多。二维还是见的少(弱)。那么二维跟一维的区别是:一般意义上二维用来求的是矩阵的和。对于C[x][y],这里的定义就是从(1,1)---(x,y)矩阵的元素和。我们可以将每行抽象成一个点,这样其实就是抽象成了求一维的情况。其实一维跟二维实现的写法差别很容易想到。对于二维BIT,我们得到C数组,需要n^2log(n)^2的时间复杂度。

  

求和:

int sum(int x,int y){
    int ret=0;
    for(int i=x;i>0;i-=lowbit(i)){
        for(int j=y;j>0;j-=lowbit(j)){
            ret+=C[i][j];
        }
    }
    return ret;
}

修改:

void modify(int x,int y,int val){
    for(int i=x;i<=n;i+=lowbit(i)){
        for(int j=y;j<=m;j+=lowbit(j)){
            C[i][j] += val;
        }
    }
}

  

解题思路:

#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxn=550;
int a[maxn][maxn];
int C[maxn][maxn];
int n,m;
int lowbit(int x){
    return x & (-x);
}
void modify(int x,int y,int val){
    for(int i=x;i<=n;i+=lowbit(i)){
        for(int j=y;j<=m;j+=lowbit(j)){
            C[i][j] ^= val;
        }
    }
}
int sum(int x,int y){
    int ret=0;
    for(int i=x;i>0;i-=lowbit(i)){
        for(int j=y;j>0;j-=lowbit(j)){
            ret ^=C[i][j];
        }
    }
    return ret;
}
int main(){
    int t,q;
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        memset(C,0,sizeof(C));
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                modify(i,j,a[i][j]);
            }
        }
        int typ,x1,x2,y1,y2,c;
        for(int i=0;i<q;i++){
            scanf("%d",&typ);
            if(typ==1){
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                int ans1,ans2,ans3,ans4;
                ans1=sum(x2,y2);
                ans2=sum(x2,y1-1);
                ans3=sum(x1-1,y2);
                ans4=sum(x1-1,y1-1);
                int ans=ans1^ans2^ans3^ans4;
                printf("%s\n",ans==0?"No":"Yes");
            }else{
                scanf("%d%d%d",&x1,&y1,&c);
                modify(x1,y1,c^a[x1][y1]);
                a[x1][y1]=c;
            }
        }
    }
    return 0;
}

  

时间: 2025-01-05 00:32:03

HDU 5465——Clarke and puzzle——————【树状数组BIT维护前缀和+Nim博弈】的相关文章

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 269 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

HDU 2852 KiKi&#39;s K-Number (树状数组 &amp;&amp; 二分)

题意:给出对容器的总操作次数n, 接下来是这n个操作.这里对于一个容器提供三种操作, 分别是插入.删除和查找.输入0  e表示插入e.输入1  e表示删除e,若元素不存在输出No Elment!.输入2  e  k表示查找比e大且第k大的数, 若不存在则输出Not Find! 分析:这里考虑树状数组做的原因是在第三个操作的时候, 只要我们记录了元素的总数, 那通过求和操作, 便能够高效地知道到底有多少个数比现在求和的这个数要大, 例如 tot - sum(3)就能知道整个集合里面比3大的数到底有

HDU 3584 Cube (三维 树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3584 Cube Problem Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. Initially we have A[i, j, k] = 0 (1 <= i, 

HDU 5592 ZYB&#39;s Premutation(树状数组+二分)

题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字之外,它是在剩下的数字当中第k+1大的. 知道这个之后,可以用树状数组来帮助找出剩下的数中第k大的数,刚开始我们可以让1-n中每个元素都标记为1,那么他们的前缀和就代表它是第几小.所以,我们可以对于他们的和来二分快速寻找第k大数.其实在树状数组里面是按照第(i-k)小来找的.找完之后要删除这个元素的

hdu 5592 ZYB&#39;s Game 树状数组

ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5592 Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to restore the premutation

hdu 4368 树状数组 离线维护

http://acm.hdu.edu.cn/showproblem.php?pid=4638 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interv

HDU 1394 Minimum Inversion Number 树状数组&amp;&amp;线段树

题目给了你一串序列,然后每次 把最后一个数提到最前面来,直到原来的第一个数到了最后一个,每次操作都会产生一个新的序列,这个序列具有一个逆序数的值,问最小的你逆序数的值为多少 逆序数么 最好想到的是树状数组,敲了一把很快,注意把握把最后一个数提上来对逆序数的影响即可, #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #i

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th