您的位置:首页 > 产品设计 > UI/UE

JuQueen(线段树 lazy)

2015-10-10 19:56 441 查看

JuQueen

Time Limit: 5 Sec Memory Limit:
512 MB

Description



Input



Output



Sample Input

10 10 5
state 0
groupchange 2 9 7
state 9
groupchange 0 2 10
change 0 -5

Sample Output

0
7
7
3
-3

线段树lazy的巧用
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define LL long long

using namespace std;

const int Num = 5000000;

typedef struct Tree
{
int Min;//所表示区间的最大值

int Max;//所表示区间的最小值

int lazy;//区间所要变化的值
} Tree;

Tree Tr[Num*5];

char str[15];

int c,n,o;

void Pushup(int st)
{
Tr[st].Max = max(Tr[st<<1].Max,Tr[st<<1|1].Max);

Tr[st].Min = min(Tr[st<<1].Min,Tr[st<<1|1].Min);

Tr[st].Min += Tr[st].lazy;

Tr[st].Max += Tr[st].lazy;
}

void Build(int L,int R,int st)
{
Tr[st].Max=0;

Tr[st].Min=0;

Tr[st].lazy=0;

if(L==R)
{
return ;
}
int mid =(L+R)>>1;

Build(L,mid,st<<1);

Build(mid+1,R,st<<1|1);

Pushup(st);
}

void Update(int L,int R,int st,int l,int r,int s)
{
if(L>r||R<l)
{
return ;
}

if(L>=l&&R<=r)//区间更新
{
Tr[st].lazy+=s;

Tr[st].Max+=s;

Tr[st].Min+=s;

return ;
}
int mid = (L+R)>>1;

Update(L,mid,st<<1,l,r,s);

Update(mid+1,R,st<<1|1,l,r,s);

Pushup(st);
}

Tree Query(int L,int R,int st,int l,int r,int s)
{
Tree p,q;

p.Max=0;

p.Min=c;

if(L>r||R<l)
{
return p;
}

if(L>=l&&R<=r)
{
p.Max=Tr[st].Max+s;

p.Min=Tr[st].Min+s;

return p;
}
int mid = (L+R)>>1;

s+=Tr[st].lazy;
//将lazy所表示的逐层向下更新
if(l<=mid)
{
q=Query(L,mid,st<<1,l,r,s);

p.Max=max(q.Max,p.Max);

p.Min=min(q.Min,p.Min);
}
if(r>mid)
{
q=Query(mid+1,R,st<<1|1,l,r,s);

p.Max=max(q.Max,p.Max);

p.Min=min(q.Min,p.Min);
}
return p;
}

int main()
{
int l,r,s;

Tree p;

while(~scanf("%d %d %d",&n,&c,&o))
{
Build(1,n,1);

for(int i=0; i<o; i++)
{
scanf("%s",str);

if(!strcmp(str,"change"))
{
scanf("%d %d",&l,&s);

r=++l;

p=Query(1,n,1,l,r,0);

if(s<0)
{
if(p.Min+s>=0)
{
printf("%d\n",s);

Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",-p.Min);

Update(1,n,1,l,r,-p.Min);
}
}
else
{
if(p.Max+s<=c)
{
printf("%d\n",s);

Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",c-p.Max);

Update(1,n,1,l,r,c-p.Max);
}
}

}
else if(!strcmp(str,"groupchange"))
{
scanf("%d %d %d",&l,&r,&s);

l++,r++;

p=Query(1,n,1,l,r,0);

if(s<0)
{
if(p.Min+s>=0)
{
printf("%d\n",s);

Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",-p.Min);

Update(1,n,1,l,r,-p.Min);
}
}
else
{
if(p.Max+s<=c)
{
printf("%d\n",s);

Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",c-p.Max);

Update(1,n,1,l,r,c-p.Max);
}
}
}
else if(!strcmp(str,"state"))
{
scanf("%d",&l);

r=++l;

p=Query(1,n,1,l,r,0);

printf("%d\n",p.Max);
}
}
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: