Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
给定一个长为n的序列,接下来m次操作,每次操作会对序列某段区间内所有数加上一个值,或是询问一段区间的和。n,m≤10^5。
线段树的模板,刚开始死活A不了,后来发现输入的判定有问题
这道题的输入不是0,1,2
而是一串字符,取最开始的一个,但是后面的也要输入
坑啊!!!
下面给出代码:
#include<iostream> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> using namespace std; inline long long rd(){ long long x=0,f=1; char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x*f; } inline void write(long long x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); return ; } long long n,m; struct node{ long long sum,l,r; long long f; }tree[4000006]; inline void build(long long i,long long x,long long y){ tree[i].l=x,tree[i].r=y; if(x==y){ tree[i].sum=rd(); return ; } long long mid=(x+y)>>1; build(i<<1,x,mid); build(i<<1|1,mid+1,y); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; return ; } inline void pushdown(long long i){ if(!tree[i].f) return ; long long h=tree[i].f; tree[i<<1].f+=h,tree[i<<1|1].f+=h; tree[i<<1].sum+=(tree[i<<1].r-tree[i<<1].l+1)*h; tree[i<<1|1].sum+=(tree[i<<1|1].r-tree[i<<1|1].l+1)*h; tree[i].f=0; return ; } inline void add(long long i,long long x,long long y,long long z){ if(tree[i].l>=x&&tree[i].r<=y){ tree[i].f+=z; tree[i].sum+=(tree[i].r-tree[i].l+1)*z; return ; } pushdown(i); if(tree[i<<1].r>=x) add(i<<1,x,y,z); if(tree[i<<1|1].l<=y) add(i<<1|1,x,y,z); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; return ; } inline long long solve(long long i,long long x,long long y){ if(tree[i].l>=x&&tree[i].r<=y) return tree[i].sum; long long ans=0; pushdown(i); if(tree[i<<1].r>=x) ans+=solve(i<<1,x,y); if(tree[i<<1|1].l<=y) ans+=solve(i<<1|1,x,y); return ans; } char c[10006]; int main(){ n=rd(),m=rd(); build(1,1,n); for(long long i=1;i<=m;i++){ scanf("%s",&c); if(c[0]=='C'){ long long x=rd(),y=rd(),z=rd(); add(1,x,y,z); } else{ long long x=rd(),y=rd(); write(solve(1,x,y)),puts(""); } } return 0; }