hdu 1166 敌兵布阵(树状数组裸题)

  1. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
  2. 通过的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAX_N=5e4+6;
int n,a[MAX_N],c[MAX_N];
void add(int x,int val){
    while(x<=n)c[x]+=val,x+=x&-x;
}
int sum(int x){
    int res=0;
    while(x)res+=c[x],x-=x&-x;
    return res;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    int T;cin>>T;
    int cs=0;
    while(T--){
        cout<<"Case "<<cs<<":\n";
        char str[106];
        cin>>n;
        for(int i=1;i<=n;i++)c[i]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];add(i,a[i]);
        }
        while(cin>>str,strcmp(str,"End")!=0){
            int a,b;
            cin>>a>>b;
            if(strcmp(str,"Query")==0)cout<<sum(b)-sum(a-1)<<endl;
            if(strcmp(str,"Add")==0)add(a,b);
            if(strcmp(str,"Sub")==0)add(a,-b);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/light2chasers/article/details/80432992
今日推荐