每日一题 day13 打卡
Analysis
用简单的树状数组维护单点修改和查询就行了
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 500000+10 6 using namespace std; 7 inline int read() 8 { 9 int x=0; 10 bool f=1; 11 char c=getchar(); 12 for(; !isdigit(c); c=getchar()) if(c=='-') f=0; 13 for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0'; 14 if(f) return x; 15 return 0-x; 16 } 17 inline void write(int x) 18 { 19 if(x<0){putchar('-');x=-x;} 20 if(x>9) write(x/10); 21 putchar(x%10+'0'); 22 } 23 int n,k; 24 int tree[maxn]; 25 inline int lowbit(int num){return num&-num;} 26 inline void build(int s,int num) 27 { 28 for(int i=s;i<=n;i+=lowbit(i)) tree[i]+=num; 29 } 30 inline int ask(int s) 31 { 32 int ans=0; 33 for(int i=s;i>=1;i-=lowbit(i)) ans+=tree[i]; 34 return ans; 35 } 36 int main() 37 { 38 n=read();k=read(); 39 for(int i=1;i<=k;i++) 40 { 41 char in; 42 scanf("%s",&in); 43 if(in=='A') 44 { 45 int x=read(); 46 int res=ask(x); 47 write(res); 48 printf(" "); 49 } 50 if(in=='B') 51 { 52 int x=read(),y=read(); 53 build(x,y); 54 } 55 if(in=='C') 56 { 57 int x=read(),y=read(); 58 build(x,-y); 59 } 60 } 61 return 0; 62 }
请各位大佬斧正(反正我不认识斧正是什么意思)