A Simple Problem with Integers poj3468(线段树区间更新,区间查询)
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 128832 Accepted: 39978
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
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 128832 Accepted: 39978
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
题意:线段树模版题,区间更新和区间查询。
线段树的原理,就是,将[1,n]分解成若干特定的子区间(数量不超过4*n),然后,将每个区间[L,R]都分解为
少量特定的子区间,通过对这些少量子区间的修改或者统计,来实现快速对[L,R]的修改或者统计。
少量特定的子区间,通过对这些少量子区间的修改或者统计,来实现快速对[L,R]的修改或者统计。
。
#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int maxn=100115;
LL sum[maxn<<2];
LL lazy[maxn<<2];
int n,m;
void PushUp(int rt)//向上更新求和
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int len)//用lazy数组标记,之后向下更新
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=(len-(len>>1))*lazy[rt];
sum[rt<<1|1]+=(len>>1)*lazy[rt];
lazy[rt]=0;
}
}
void Build(int l,int r,int rt)//建树
{
lazy[rt]=0;
if(l==r){
scanf("%I64d",&sum[rt]);
return ;
}
int mid=l+r>>1;
Build(lson);//建左儿子
Build(rson);//右儿子
PushUp(rt);//更新
}
void Update(int ll,int rr,int c,int l,int r,int rt)//区间更新
{
if(ll<=l&&rr>=r)//找到相应区间直接返回sum
{
sum[rt]+=(LL)(r-l+1)*c;
lazy[rt]+=c;
return ;
}
PushDown(rt,r-l+1);//向下更新sum
int mid=l+r>>1;
if(ll<=mid) Update(ll,rr,c,lson);//在左儿子中找区间
if(rr>mid) Update(ll,rr,c,rson);//在右儿子中找
PushUp(rt);//向上更新sum;
}
LL Query(int ll,int rr,int l,int r,int rt)//区间查询
{
if(ll<=l&&rr>=r)
return sum[rt];
PushDown(rt,r-l+1);//把延迟的标记更新到对应的位置
int mid=l+r>>1;
LL cnt=0;
if(ll<=mid) cnt+=Query(ll,rr,lson);
if(rr>mid) cnt+=Query(ll,rr,rson);
return cnt;//返回区间的和
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
Build(root);
for(int i=0;i<m;i++){
char s[3];int a,b,c;
scanf("%s",s);
if(s[0]=='Q'){scanf("%d%d",&a,&b);cout<<Query(a,b,root)<<endl;}
else {scanf("%d%d%d",&a,&b,&c);Update(a,b,c,root);}
}
}
}

浙公网安备 33010602011771号