poj3468 A Simple Problem with Integers 線段樹lazy標籤
阿新 • • 發佈:2019-02-12
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
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
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
其實最開始我調了很多次,並且這個程式過不了,因為我懶得改成int64了,留著自己以後看看
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct segtree{ long long l,r,v; }tree[500005]; long long ans,add[500005]; void build(long long l,long long r,long long root) { long long m=(l+r)/2; tree[root].l=l; tree[root].r=r; tree[root].v=0; if(l==r)return; build(l,m,2*root); build(m+1,r,2*root+1); } void adjust(long long root,long long m) { if(add[root]) { add[root*2]+=add[root]; add[root*2+1]+=add[root]; tree[root*2].v+=add[root]*(m-(m/2)); tree[root*2+1].v+=add[root]*(m/2); add[root]=0; } } void insert(long long v,long long l,long long root) { long long m; if(tree[root].l==tree[root].r&&tree[root].l==l) { tree[root].v+=v; return; } m=(tree[root].l+tree[root].r)/2; if(l<=m)insert(v,l,2*root); else insert(v,l,2*root+1); tree[root].v+=v; } void query(long long l,long long r,long long root) { long long m=(tree[root].l+tree[root].r)/2; adjust(root,tree[root].r-tree[root].l+1);//每次查詢時,把lazy標籤處理了 if(tree[root].l==l&&tree[root].r==r) { ans+=tree[root].v; return; } if(m>=r)query(l,r,2*root); else if(m<l)query(l,r,2*root+1); else{ query(l,m,2*root); query(m+1,r,2*root+1); } } void update(long long c,long long l,long long r,long long root) { if(tree[root].l==l&&tree[root].r==r) { add[root]+=c; tree[root].v+=c*(r-l+1);//區間和加這麼多 return; } if(tree[root].l==tree[root].r)return; adjust(root,tree[root].r-tree[root].l+1); long long m=(tree[root].l+tree[root].r)/2; if(r<=m)update(c,l,r,root*2); else if(l>m)update(c,l,r,root*2+1); else{ update(c,l,m,root*2); update(c,m+1,r,root*2+1); } tree[root].v+=tree[root*2].v+tree[root*2+1].v; } int main() { long long i,n,x,t,j; cin>>n>>t; build(1,n,1); for(i=1;i<=n;i++) { cin>>j; insert(j,i,1);; } char s; while(t--) { cin>>s; long long a,b,c; if(s=='C') { cin>>a>>b>>c; update(c,a,b,1); } else { cin>>a>>b; ans=0; query(a,b,1); cout<<ans<<endl; } } return 0; }
這是ac程式碼
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int maxn=111111;
LL add[maxn<<2],sum[maxn<<2];
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
add[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int rt,int m)
{
if(add[rt]){
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*(m-(m>>1));
sum[rt<<1|1]+=add[rt]*(m>>1);
add[rt]=0;
}
}
void update(int x,int y,int c,int l,int r,int rt)
{
if(x<=l&&y>=r)
{
add[rt]+=c;
sum[rt]+=(LL)c*(r-l+1);
return;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(x<=m)update(x,y,c,lson);
if(y>m)update(x,y,c,rson);
pushup(rt);
}
LL query(int x,int y,int l,int r,int rt)
{
if(x<=l&&y>=r)return sum[rt];
pushdown(rt,r-l+1);
int m=(l+r)>>1;
LL ans=0;
if(x<=m)ans+=query(x,y,lson);
if(y>m)ans+=query(x,y,rson);
return ans;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
char op[2];
int a,b,c;
scanf("%s",op);
if(op[0]=='Q'){
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
else {
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
}