zoukankan      html  css  js  c++  java
  • SPOJ 1029 Matrix Summation【 二维树状数组 】

    题意:二维树状数组,更改值的时候有一点不一样,

    是将a[x][y]设置为一个值,所以add的时候要将它和以前的值作差一下

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 typedef long long LL;
    14 const int INF = (1<<30)-1;
    15 const int mod=1000000007;
    16 const int maxn=5005;
    17 
    18 int a[maxn][maxn];
    19 int c[maxn][maxn];//树状数组
    20 int n;
    21 
    22 int lowbit(int x){ return x & (-x);}
    23 
    24 int sum(int x,int y){
    25     int ret=0;
    26     int y1;
    27     while(x > 0){
    28         y1 = y;
    29         while(y1 > 0){
    30             ret += c[x][y1];y1-=lowbit(y1);
    31         }
    32         x-=lowbit(x);
    33     }
    34     return ret;
    35 }
    36 
    37 void add(int x,int y,int d){
    38     int y1;
    39     while(x < maxn){
    40         y1 = y;
    41         while (y1 < maxn){
    42             c[x][y1]+=d; y1+=lowbit(y1);
    43         } 
    44         x+=lowbit(x);
    45     }
    46 }
    47 
    48 int main(){
    49     int T;
    50     scanf("%d",&T);
    51     while(T--){
    52         memset(c,0,sizeof(c));
    53         memset(a,0,sizeof(a));
    54         scanf("%d",&n);
    55         char s[15];
    56         while(scanf("%s",s)!=EOF){
    57             if(s[0] == 'E') break;
    58             if(s[1]=='E'){
    59                 int x,y,num;
    60                 scanf("%d %d %d",&x,&y,&num);x++;y++;
    61                 int tmp = num - a[x][y];
    62                 a[x][y] = num;
    63                 add(x,y,tmp); 
    64             }
    65             if(s[1]=='U'){
    66                 int x,y,xx,yy;
    67                 int ans=0;
    68                 scanf("%d %d %d %d",&x,&y,&xx,&yy);
    69                 x++;y++;xx++;yy++;
    70                 ans = sum(xx,yy) - sum(x-1,yy) - sum(xx,y-1) + sum(x-1,y-1);
    71                 printf("%d
    ",ans);
    72             }
    73         }    
    74     }
    75     return 0;
    76 } 
    View Code
  • 相关阅读:
    Windows Phone MultiBinding :Cimbalino Toolkit
    Instant Django 1.5 Application Development Starter
    Writing your first Django
    Python urllib2 proxy
    TED Notes 1 (What leads to success)
    Py2.7 no module named tkinter
    某培训的笔记
    “假学习”和“真学习”[转]
    Selenium学习笔记
    反思之一
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4595278.html
Copyright © 2011-2022 走看看