HDU 2642 树状数组

点击打开链接

题意:给个二维矩阵,矩阵有0或者1两个值,然后有三个操作,Q问区间和,剩下两个是更新点的值

思路:更新点的值直接更新就行了,然后询问区间和的时候就处理一下,每次问的是X1,Y1到X2,Y2的区间和,而树状数组的和是从1,1开始的,所以总的减去多于的在加上多减去的就OK了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=1010;
int sum[maxn][maxn],n;
bool vis[maxn][maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int y,int val){
    for(int i=x;i<maxn;i+=lowbit(i)){
        for(int j=y;j<maxn;j+=lowbit(j)){
                sum[i][j]+=val;
        }
    }
}
int msum(int x,int y){
    int res=0;
    for(int i=x;i>0;i-=lowbit(i)){
        for(int j=y;j>0;j-=lowbit(j)){
                res+=sum[i][j];
        }
    }
    return res;
}
int main(){
    int x1,x2,y1,y2;
    char ch[10];
    while(scanf("%d",&n)!=-1){
        memset(sum,0,sizeof(sum));
        memset(vis,0,sizeof(vis));
        while(n--){
            scanf("%s",ch);
            if(ch[0]=='B'){
                scanf("%d%d",&x1,&y1);
                x1++;y1++;
                if(vis[x1][y1]) continue;
                vis[x1][y1]=1;update(x1,y1,1);
            }else if(ch[0]=='D'){
                scanf("%d%d",&x1,&y1);
                x1++;y1++;
                if(vis[x1][y1]) update(x1,y1,-1);
                vis[x1][y1]=0;
            }else if(ch[0]=='Q'){
                scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
                x1++;x2++;y1++;y2++;
                if(x1>x2) swap(x1,x2);
                if(y1>y2) swap(y1,y2);
                int ans=msum(x2,y2)-msum(x1-1,y2)-msum(x2,y1-1)+msum(x1-1,y1-1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}
时间: 2024-08-07 08:39:32

HDU 2642 树状数组的相关文章

HDU 2642(树状数组模板二维)

Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 1350    Accepted Submission(s): 554 Problem Description Yifenfei is a romantic guy and he likes to count the stars in the sky. To make the

hdu 3333 树状数组+离线处理

http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了很久不知道怎么处理,而且答案没看懂,然后找个例子模拟下别人的代码马上懂了---以后看不懂的话就拿个例子模拟下别人的代码 举个例子:1 3 3 5 3 5 查询 a, 2 4 b, 2 5 最初是这么想的:对于a查询,倘若把第二个数第三个数变成1个3,那么到b查询,又出现了两个3,再做处理似乎还是O(n),而且如果先出现2,5查询,后出现2,4查询,那么还需要把删除的数补回来.....o(╯

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

HDU 1754 树状数组 解法

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

HDU 1166(树状数组)

用树状数组把HDU1166再写了一次  感觉树状数组简洁 1 #include <cstdio> 2 #include <iostream> 3 #include <string.h> 4 using namespace std; 5 int c[50002],lv[50002],n; 6 int lowbit(int x){return x&(-x);} 7 int sum(int b){ 8 int sum=0; 9 while(b>0){ 10 su

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 4630 树状数组+离线操作+GCD

http://acm.hdu.edu.cn/showproblem.php?pid=4630 重新认识了树状数组. 首先要记住那个树形的图,然后+或-lowbit(i)是自己根据具体问题设定的,不要死于+或者-, 树状数组的特点: 1.+lowbit(i)可以到达包含结点i的上一层父节点    所以用于值的更改 2.-lowbit(i)可以到达不包含i所代表区间的上一层父节点  所以用于值的求和---每个不相交的段加起来 3.C[i]的含义也是根据具体问题去做设定的,但是c[i]覆盖了a[i-2

HDU 1394 树状数组(逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10100    Accepted Submission(s): 6192 Problem Description The inversion number of a given number sequence a1, a2, ..., an

hdu 4970 树状数组区间更新 思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4970 好像还没有用树状数组写过区间更新,但是树状数组的确比线段树快很多,不知道跟ZKW线段树比效率怎么样: 先贴个模板: #include <cstdio> const int MAXN = 1024; int B[MAXN], C[MAXN]; #define LOWBIT(x) ((x)&(-(x))) void bit_update(int *a, int p, int d) { for (