1、http://poj.org/problem?id=3468
这道题目就是错在懒惰标记没有处理好,而且没有注意long long
懒惰标记实际上就是让子节点暂时处于不更新状态,用到的时候再更新,如本题中的visit[]就是懒惰标记,例如总长度是1-10,我们现在要想更新1-6,(将1-6的值都加3)那么update()会先找1-10,发现不合适,再找他的左右孩子,发现1<5,说明1-6的区间在1-10的左孩子中,同时6>5,1-6也在1-10的右孩子中,这样依次去找1-6在的区间。但是找到1-5的时候,我们发现整个1-5都在1-6中间,也就是说这一段都要更新,那么我们将1-5的sum值更新了,同时用visit[rt]+=3记录下来1-5中的数字现在每个都 要加的数字,但是1-5下边还有1-3,4-5,3-3,4-4,5-5,这些我们就可以不用更新,因为这些我们暂时还用不到,假如现在又要将1-5区间的值都加5,那么visit[rt]+=5,此时就是8了,但是还是不用更新他的子节点,假如我们现在要用到1-3区间了,我们就可以一次性给1-3区间加上8,而不用先加3,再加5,这样懒惰标记就使得每次的递归都少了好多,
2、题目:
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 52972 | Accepted: 15828 | |
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
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
ll sum[N*4];
int visit[N*4];
void pushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
visit[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 d)
{
if(visit[rt]!=0)
{
visit[rt<<1]+=visit[rt];
visit[rt<<1|1]+=visit[rt];
sum[rt<<1|1]+=(ll)(d>>1)*visit[rt];//注意后边乘可能超整形,强制类型转换(ll),即AC
sum[rt<<1]+=(ll)(d-(d>>1))*visit[rt];
visit[rt]=0;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l && R>=r)
{
visit[rt]+=c;
sum[rt]+=(r-l+1)*c;
return ;
}
pushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(L,R,c,lson);
if(R>m)
update(L,R,c,rson);
pushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l && R>=r)
{
return sum[rt];
}
pushDown(rt,r-l+1);
int m=(l+r)>>1;
ll ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>m)
ret+=query(L,R,rson);
return ret;
}
int main()
{
int n,q,a,b,c;
char s[3];
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
scanf("%s",s);
if(s[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
else if(s[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
}
return 0;
}
/*
10 100
1 2 3 4 5 6 7 8 9 10
C 1 6 3
Q 1 1
Q 1 2
Q 4 4
Q 1 10
Q 1 5
C 3 6 3
Q 1 5
*/
4、wrong answer 代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
ll sum[N*4];
int visit[N*4];
void pushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
visit[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushUp(rt);
}
void pushDown(int L,int R,int c,int l,int r,int rt)
{
int m=r-l+1;
if(visit[rt]!=0)
{
//printf("%d %d %d %d %d %d\n",l,r,sum[rt<<1|1],sum[rt<<1],m,c);
sum[rt<<1|1]+=(m>>1)*c;
sum[rt<<1]+=(m-(m>>1))*c;
// printf("&&&&&&%d %d\n",sum[rt<<1|1],sum[rt<<1]);
visit[rt]=0;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
//printf("*%d %d %d \n",l,r,rt);
if(L<=l && R>=r)
{
sum[rt]+=(r-l+1)*c;
visit[rt]=1;
pushDown(L,R,c,l,r,rt);
return ;
}
pushDown(L,R,c,l,r,rt);
int m=(l+r)>>1;
if(L<=m)
update(L,R,c,lson);
if(R>m)
update(L,R,c,rson);
pushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l && R>=r)
{
return sum[rt];
}
int m=(l+r)>>1;
ll ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>m)
ret+=query(L,R,rson);
return ret;
}
int main()
{
int n,q,a,b,c;
char s[3];
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
scanf("%s",s);
if(s[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
else if(s[0]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
}
return 0;
}
/*
10 100
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 1 5
C 3 6 3
Q 1 5
*/