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
相关文章推荐
- FZU Problem 2171 防守阵地 II (线段树区间更新模板题)
- FZU 2171 —— 防守阵地 II(线段树,区间求和+区间更新)
- FZU 2171 防守阵地 II(线段树区间更新【lazy标记】)
- fzu 2171 防守阵地 II(线段树 区间更新)
- FZU2171 - 防守阵地 II 线段树的区间更新
- FZU 2171 防守阵地 II(线段树区间修改)
- FOJ 2171 防守阵地 II【线段树+区间更新】
- FZU Problem 2171 防守阵地 II (线段树模板题)
- fzu 2171 防守阵地 II(线段树,成段更新,查询一段区间和)
- FZU2171 防守阵地 II(线段树)
- FZU2171:防守阵地 II(线段树)
- 【FZU】2171 防守阵地 II 线段树
- FZU Problem 2171 防守阵地 II (裸线段树 懒惰标记)
- FOJ 2171 防守阵地 II 区间求和区间查询 线段树
- FZU2171 防守阵地 II (线段树)
- FZU_Problem 2171 防守阵地 II
- Problem 2171 防守阵地 II
- FZU 2171 线段树 区间更新求和
- FZU 2171 防守阵地 II
- fzu 2171 防守阵地 II