您的位置:首页 > 其它

FZU Problem 2171 防守阵地 II (线段树,区间更新)

2014-08-10 00:45 309 查看

Problem 2171 防守阵地 II

Accept: 143 Submit: 565
Time Limit: 3000 mSec Memory Limit : 32768 KB

#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int MAXN=100000+10;
int b[MAXN*4];
struct node
{
int l,r;
int num;
int col;
int mid()
{
return (l+r)/2;
}
}a[MAXN*5];

void pushup(int step)
{
a[step].num=a[step*2].num+a[step*2+1].num;
}

void pushdown(int x,int step)
{
if(a[step].col!=0)
{
a[step*2+1].col+=a[step].col;
a[step*2].col+=a[step].col;
a[step*2].num+=a[step].col*(x-(x/2));
a[step*2+1].num+=a[step].col*((x/2));
a[step].col=0;
}
}

void btree(int l,int r,int step)
{
a[step].l=l;
a[step].r=r;
a[step].col=0;
if(l==r)
{
a[step].num=b[l];
return ;
}
int mid=a[step].mid();
btree(l,mid,step*2);
btree(mid+1,r,step*2+1);
pushup(step);
}

void ptree(int l,int r,int val,int step)
{
if(l<=a[step].l&&a[step].r<=r)
{
a[step].col+=val;
a[step].num+=val*(a[step].r-a[step].l+1);
return ;
}
pushdown(a[step].r-a[step].l+1,step);
int mid=a[step].mid();
if(l>mid)
ptree(l,r,val,step*2+1);
else if(r<=mid)
ptree(l,r,val,step*2);
else
{
ptree(l,r,val,step*2);
ptree(l,r,val,step*2+1);
}
pushup(step);
}

int fintree(int l,int r,int step)
{
if(l<=a[step].l&&a[step].r<=r)
return a[step].num;
pushdown(a[step].r-a[step].l+1,step);
int mid=a[step].mid();
if(l>mid)
return fintree(l,r,step*2+1);
else if(r<=mid)
return fintree(l,r,step*2);
else
return fintree(l,r,step*2)+fintree(l,r,step*2+1);
}

int main()
{
int n,m,kase,num;
while(scanf("%d %d %d",&n,&m,&kase)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
btree(1,n,1);
while(kase--)
{
scanf("%d",&num);
int ans=fintree(num,num+m-1,1);
printf("%d\n",ans);
ptree(num,num+m-1,-1,1);
}
}
return 0;
}


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