spoj 1557 Can you answer these queries II (gss2)线段树
来源:互联网 发布:html与js的关系 编辑:程序博客网 时间:2024/05/17 21:07
从来木有想过update儿子呀,除了lazy~~
#define fi freopen("in.txt","r",stdin)#include <stdio.h> #include <iostream>#include <string.h> #include <cmath>#include <vector>#include <algorithm> using namespace std;#define sfint(x) scanf("%d",&x)#define sfint2(x,y) scanf("%d%d",&x,&y)#define sfint3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define sfstr(c) scanf("%s",c)#define pfint(x) printf("%d\n",x)#define fr(i,s,n) for(i=s;i<n;++i)#define _fr(i,n,s) for(i=n-1;i>=s;--i)#define cl(a) memset(a,0,sizeof(a))#define md ((troot.l+troot.r)>>1)#define lc (root<<1)#define rc (root<<1|1)#define troot tree[root]typedef long long ll;const int N = 100010;struct Seg{int nad,pad,nmx,pmx; //pad:preadd,pmx:premax;int l,r;void up(Seg *l,Seg *r){nmx = max(l->nmx+l->nad, r->nmx+r->nad);pmx = max(max(l->pmx,l->nmx+l->pad),max(r->pmx,r->nmx+r->pad) );}void down(Seg *l,Seg *r){l->pad = max(l->pad,l->nad + pad);r->pad = max(r->pad,r->nad + pad);l->nad += nad;r->nad += nad;nad = pad = 0;}}tree[N<<2];void build(int root,int l,int r){troot.l = l;troot.r = r;if (l == r) return ;int m = md;build(lc,l,m);build(rc,m+1,r);}void update(int root,int l,int r,int del){if (l <= troot.l&&troot.r <= r){troot.nad += del;troot.pad = max(troot.pad,troot.nad);return;}troot.down(&tree[lc],&tree[rc]);int m = md;if (l<=m) update(lc,l,r,del);if (r>m) update(rc,l,r,del);troot.up(&tree[lc],&tree[rc]);}int query(int root,int l,int r){if (l <= troot.l &&troot.r <= r) return max(troot.pmx,troot.nmx+troot.pad);troot.down(&tree[lc],&tree[rc]);int m = md;int ret = 0;if (l<=m) ret =max(ret , query(lc,l,r));if (r>m) ret = max(ret,query(rc,l,r));troot.up(&tree[lc],&tree[rc]);return ret;}int n,num[N],q,pos[N<<1];int ans[N];struct Que{int l,r;int id;}que[N];bool cmp(Que a,Que b){return a.r<b.r;}void solve(){int i,j =1;sort(que+1,que+q+1,cmp);build(1,1,n);fr(i , 1,n+1){update(1,pos[num[i]+N]+1,i,num[i]);pos[num[i]+N] = i;while(que[j].r == i){ans[que[j++].id] = query(1,que[j].l,que[j].r);if (j>q) return;}}}void inp(){int i;sfint(n);fr(i,1,n+1) sfint(num[i]);sfint(q);fr(i,1,q+1) sfint2(que[i].l,que[i].r),que[i].id = i;}void dsp(){for(int i = 1;i<q+1;i++) {printf("%d\n",ans[i]);}}int main(){#ifdef localfi;#endifinp();solve();dsp();return 0;}
- [SPOJ GSS2] Can you answer these queries II [线段树]
- 【线段树】spoj GSS2 Can you answer these queries II
- SPOJ 1557 Can you answer these queries II(GSS2 线段树)
- spoj 1557 Can you answer these queries II (gss2)线段树
- GSS2 - Can you answer these queries II(线段树)
- BZOJ 2482 || SPOJ GSS2 Can you answer these queries II(线段树 离线 后缀和)
- 【SPOJ】1557 Can you answer these queries II 线段树
- SPOJ GSS2 Can you answer these queries II
- spoj 1557. Can you answer these queries II(线段树)
- [SPOJ1557][GSS2][线段树]Can you answer these queries II[好题]
- SPOJ 1557 Can you answer these queries II(离线处理+线段树求历史最大)
- SPOJ/GSS3:Can you answer these queries III(线段树)
- spoj 1043. Can you answer these queries I (线段树)
- spoj 1716. Can you answer these queries III(线段树)
- spoj 2713. Can you answer these queries IV(线段树)
- spoj 2916. Can you answer these queries V(线段树)
- SPOJ GSS3 Can you answer these queries III (线段树)
- SPOJ GSS4 Can you answer these queries IV (线段树)
- 修复artdialog锁屏状态下双击解锁
- java异常处理Exception
- FL2440--2--自己写boot之nor和nand代码重定向
- POJ 1887 Testing the CATCHER
- unity3d 关键帧控制动画
- spoj 1557 Can you answer these queries II (gss2)线段树
- ubuntu下安装中文输入法
- Unity 3D 抛物线算法
- 地址映射与共享
- jsp中 param子标签的使用
- 有关GroupBox和RadioButton
- Unity 3D 自动寻路(简单)
- 《C和指针》操作符的优先级
- 微策略魔数题