poj 3468 A Simple Problem with Integers 线段树 成段更新
2017-07-22 16:14
441 查看
A Simple Problem with Integers
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
Sample Output
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct st
{
int l;
int r;
ll n;//记录区间的加的值
ll sum;//区间的求和值
}t[400000];
void build(int l,int r,int k)
{
t[k].l=l;
t[k].r=r;
t[k].n=0;
if(t[k].l==t[k].r)
{
scanf("%lld",&t[k].sum);
return ;
}
int mid=(t[k].l+t[k].r)>>1;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
void push(int k,int num)
{
if(t[k].n)
{
t[k<<1].n+=t[k].n;
t[k<<1|1].n+=t[k].n;
t[k<<1].sum+=t[k].n*(num-(num>>1));
t[k<<1|1].sum+=t[k].n*(num>>1);
t[k].n=0;
}
}
void insert(int n,int l,int r,int k)
{
if(t[k].l==l&&t[k].r==r)
{
t[k].n+=n;
t[k].sum+=(ll)n*(r-l+1);
return ;
}
if(t[k].l==t[k].r)
return ;
push(k,t[k].r-t[k].l+1);
int mid=(t[k].l+t[k].r)>>1;
if(r<=mid)
insert(n,l,r,2*k);
else
if(l>mid)
insert(n,l,r,2*k+1);
else
{
insert(n,l,mid,2*k);
insert(n,mid+1,r,2*k+1);
}
t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
ll search(int l,int r,int k)
{
if(t[k].l==l&&t[k].r==r)
{
return t[k].sum;
}
push(k,t[k].r-t[k].l+1);
ll res=0;
int mid=(t[k].l+t[k].r)>>1;
if(r<=mid)
res+=search(l,r,2*k);
else
if(l>mid)
res+=search(l,r,2*k+1);
else
{
res+=search(l,mid,2*k);
res+=search(mid+1,r,2*k+1);
}
return res;
}
int main()
{
int n,m;
int i,temp;
int a,b,c;
char s[5];
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
for(i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",search(a,b,1));
}
else
{
scanf("%d%d%d",&a,&b,&c);
insert(c,a,b,1);
}
}
}
return 0;
}
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 112353 | Accepted: 34930 | |
Case Time Limit: 2000MS |
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 线段树加lazy思想 lazy 思想:在a~b期间上加c,暂时没必要加到每个点(能偷懒就偷懒嘛),在表示区间的root结构体上增加一个inc域,将要加的值赋给这个inc域,然后就不要再往下了。在求区间和时,将root中的inc值赋给要求的区间,并且将该节点的root置零。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct st
{
int l;
int r;
ll n;//记录区间的加的值
ll sum;//区间的求和值
}t[400000];
void build(int l,int r,int k)
{
t[k].l=l;
t[k].r=r;
t[k].n=0;
if(t[k].l==t[k].r)
{
scanf("%lld",&t[k].sum);
return ;
}
int mid=(t[k].l+t[k].r)>>1;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
void push(int k,int num)
{
if(t[k].n)
{
t[k<<1].n+=t[k].n;
t[k<<1|1].n+=t[k].n;
t[k<<1].sum+=t[k].n*(num-(num>>1));
t[k<<1|1].sum+=t[k].n*(num>>1);
t[k].n=0;
}
}
void insert(int n,int l,int r,int k)
{
if(t[k].l==l&&t[k].r==r)
{
t[k].n+=n;
t[k].sum+=(ll)n*(r-l+1);
return ;
}
if(t[k].l==t[k].r)
return ;
push(k,t[k].r-t[k].l+1);
int mid=(t[k].l+t[k].r)>>1;
if(r<=mid)
insert(n,l,r,2*k);
else
if(l>mid)
insert(n,l,r,2*k+1);
else
{
insert(n,l,mid,2*k);
insert(n,mid+1,r,2*k+1);
}
t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
ll search(int l,int r,int k)
{
if(t[k].l==l&&t[k].r==r)
{
return t[k].sum;
}
push(k,t[k].r-t[k].l+1);
ll res=0;
int mid=(t[k].l+t[k].r)>>1;
if(r<=mid)
res+=search(l,r,2*k);
else
if(l>mid)
res+=search(l,r,2*k+1);
else
{
res+=search(l,mid,2*k);
res+=search(mid+1,r,2*k+1);
}
return res;
}
int main()
{
int n,m;
int i,temp;
int a,b,c;
char s[5];
while(~scanf("%d%d",&n,&m))
{
build(1,n,1);
for(i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",search(a,b,1));
}
else
{
scanf("%d%d%d",&a,&b,&c);
insert(c,a,b,1);
}
}
}
return 0;
}
相关文章推荐
- poj 3468 A Simple Problem with Integers 线段树成段更新模板
- poj 3468 A Simple Problem with Integers (线段树---成段更新)
- poj-3468 A Simple Problem with Integers(线段树成段更新)
- POJ 3468 A Simple Problem with Integers(线段树 成段更新)
- 线段树(成段更新) 之 poj 3468 A Simple Problem with Integers
- POJ 3468:A Simple Problem with Integers(线段树[成段更新])
- poj 3468 A Simple Problem with Integers(线段树成段更新,懒惰标记的使用)经典题目
- poj 3468 A Simple Problem with Integers(线段树,成段更新)
- poj 3468 A Simple Problem with Integers(线段树——成段更新问题)
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
- poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)
- POJ 3468 A Simple Problem with Integers 线段树的成段更新
- poj 3468 A Simple Problem with Integers(线段树)(第二部分 成段更新,区间求和)
- POJ 3468 A Simple Problem with Integers (线段树成段更新) 解题报告
- POJ 3468 A Simple Problem with Integers(线段树成段更新)
- poj 3468 -- A Simple Problem with Integers ( 线段树 , 段更新 , 段求和 )
- POJ - 3468 A Simple Problem with Integers(线段树成段更新,查询区间和)
- POJ 3468 A Simple Problem with Integers?(线段树成段更新求和)
- POJ 3468 A Simple Problem with Integers 线段树 (成段更新)
- POJ 3468 A Simple Problem with Integers 线段树成段更新+区间查询