Advertisement
MaskerQwQ

B - 敌兵布阵

Apr 30th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<iostream>
  5.  
  6. using namespace std;
  7.  
  8. int sum[50005],a,b;
  9. string s;
  10.  
  11.  
  12. int lowbit(int x){
  13.     return x&(-x);
  14. }
  15.  
  16. void update(int x,int y,int n)
  17. {
  18.     for(int i=x;i<=n;i+=lowbit(i)){
  19.         sum[i]+=y;
  20.     }
  21. }
  22.  
  23. int getsum(int x){
  24.     int ans=0;
  25.     for(int i=x;i;i-=lowbit(i)){
  26.         ans+=sum[i];
  27.     }
  28.     return ans;
  29. }
  30.  
  31. int main(){
  32.     int t,k=1;
  33.     cin>>t;
  34.     while(t--){
  35.         cout<<"Case "<<k<<":"<<endl;
  36.         memset(sum,0,sizeof(sum));
  37.         int n;
  38.         cin>>n;
  39.         for(int i=1;i<=n;i++){
  40.             cin>>a;
  41.             update(i,a,n);
  42.         }
  43.         cin>>s;
  44.         while(s!="End"){
  45.             cin>>a>>b;
  46.             if(s=="Add"){
  47.                 update(a,b,n);
  48.             }else{
  49.                 if(s=="Sub"){
  50.                     update(a,-b,n);
  51.                 }else{
  52.                     cout<<getsum(b)-getsum(a-1)<<endl;
  53.                 }
  54.             }
  55.             cin>>s;
  56.         }
  57.         k++;   
  58.     }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement