Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 129659 | Accepted: 40232 | |
Case Time Limit: 2000MS |
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.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
思路:模板题,网上都是详解,这里只贴个代码供自己翻阅。
树状数组:
#include<stdio.h>
#define For(a,b,c) for(int a = b; a <= c; a++)
#define ll long long
ll N, M;
ll c1[100005], c2[100005];
ll lowbit(ll x)
{
return x&(-x);
}
void Add(ll *v, ll pos, ll num)
{
for(; pos <= N; pos += lowbit(pos)) v[pos] += num;
}
ll Prefix_sum(ll *v, ll pos)
{
ll ans = 0;
for(; pos; pos-=lowbit(pos)) ans += v[pos];
return ans;
}
ll sum(ll x)
{
return x*Prefix_sum(c1,x) - Prefix_sum(c2,x);
}
ll Query(ll l, ll r)
{
return sum(r) - sum(l-1);
}
int main()
{
scanf("%lld %lld",&N,&M);
ll last = 0, a;
For(i,1,N)
{
scanf("%lld",&a);
Add(c1,i,a-last);
Add(c2,i,(i-1)*(a-last));
last = a;
// Add(c1,i+1,a);
// Add(c2,i+1,-i*a);
}
char flag;
ll l, r;
while(M--)
{
scanf(" %c",&flag);
if(flag == 'Q')
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",Query(l,r));
}
else
{
scanf("%lld%lld%lld",&l,&r,&a);
Add(c1,l,a);
Add(c1,r+1,-a);
Add(c2,l,(l-1)*a);
Add(c2,r+1,-r*a);
}
}
return 0;
}
线段树:
#include<stdio.h>
#define For(a,b,c) for(int a = b; a <= c; a++)
#define ll long long
ll tree[400005], lazy[400005];
void Build(int l, int r, int rt)
{
if(l == r)
{
scanf("%lld",&tree[rt]);
return;
}
int mid = (l+r)>>1;
Build(l, mid, rt<<1);
Build(mid+1, r, rt<<1|1);
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
void pushdown(int rt, int len)
{
if(lazy[rt])
{
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
tree[rt<<1] += lazy[rt]*(len-(len>>1));
tree[rt<<1|1] += lazy[rt]*(len>>1);
lazy[rt] = 0;
}
}
void update(int l, int r, int ql, int qr, int rt, ll w)
{
if(ql <= l && qr >= r)
{
tree[rt] += w*(r-l+1);
lazy[rt] += w;
return;
}
pushdown(rt,r-l+1);
int mid = (l+r)>>1;
if(ql <= mid) update(l,mid,ql,qr,rt<<1,w);
if(qr > mid) update(mid+1,r,ql,qr,rt<<1|1,w);
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
ll Query(int l, int r, int ql, int qr, int rt)
{
// printf("%d %d\n",l,r);
if(ql <= l && r <= qr) return tree[rt];
pushdown(rt,r-l+1);
ll ans = 0;
int mid = (l+r)>>1;
if(ql <= mid) ans += Query(l,mid,ql,qr,rt<<1);
if(qr > mid) ans += Query(mid+1,r,ql,qr,rt<<1|1);
return ans;
}
int main()
{
int N, Q;
scanf("%d%d",&N,&Q);
Build(1,N,1);
char flag;
int l, r;
ll w;
while(Q--)
{
scanf(" %c",&flag);
if(flag == 'C')
{
scanf("%d%d%lld",&l,&r,&w);
update(1,N,l,r,1,w);
}
else
{
scanf("%d%d",&l,&r);
printf("%lld\n",Query(1,N,l,r,1));
}
}
return 0;
}