首页 > 代码库 > 可持续化线段树(例题Sign on Fence[Codeforces 484E])
可持续化线段树(例题Sign on Fence[Codeforces 484E])
刚刚学习的想记录一下:
第一次接触可持续化线段树,很懵。。。
题目:
题目描述
izon the Champion has recently finished painting his wood fence. The fence consists of a sequence of n panels of 1 meter width and of arbitrary height. The i-th panel‘s height is
hi meters. The adjacent planks follow without a gap between them.
After Bizon painted the fence he decided to put a "for sale" sign on it.The sign will be drawn on a rectangular
piece of paper and placed on the fence so that the sides of the sign are parallel to the fence panels and are
also aligned with the edges of some panels. Bizon the Champion introduced the following constraints for the sign position:
1 The width of the sign should be exactly w meters.
2 The sign must fit into the segment of the fence from the l-th to the r-th panels, inclusive
(also, it can‘t exceed the fence‘s bound in vertical direction).
The sign will be really pretty, So Bizon the Champion wants the sign‘s height to be as large as possible.
You are given the description of the fence and several queries for placing sign. For each query print the maximum
possible height of the sign that can be placed on the corresponding segment of the fence with the given fixed width of the sign.
输入
The first line of the input contains integer n — the number of panels in the fence (1?≤?n?≤?105).
The second line contains n space-separated integers hi, — the heights of the panels (1?≤?hi?≤?109).
The third line contains an integer m — the number of the queries (1?≤?m?≤?105).
The next m lines contain the descriptions of the queries,
each query is represented by three integers l, r and w (1?≤?l?≤?r?≤?n,1?≤?w?≤?r?-?l?+?1)
— the segment of the fence and the width of the sign respectively.
输出
For each query print the answer on a separate line — the maximum height of the sign that can be put in the corresponding segment of the fence with all the conditions being satisfied.
样例输入
样例输出
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct ding{ int h,node; }a[100009]; struct ding2{ int l,r,mx,ls,rs,lx,rx; }f[2000000]; int num,root[100009],n,m; bool cmp(ding x,ding y){return (x.h==y.h?x.node>y.node:x.h>y.h);} void build(int& roo,int lef,int righ) { if (roo==0) roo=++num; f[roo].l=lef;f[roo].r=righ; if (lef==righ) return; int mid=(lef+righ)/2; build(f[roo].ls,lef,mid); build(f[roo].rs,mid+1,righ); } void update(int x) { int lson=f[x].ls,rson=f[x].rs; f[x].lx=f[lson].r-f[lson].l+1==f[lson].mx?f[lson].lx+f[rson].lx:f[lson].lx; f[x].rx=f[rson].r-f[rson].l+1==f[rson].mx?f[rson].rx+f[lson].rx:f[rson].rx; f[x].mx=max(max(f[lson].mx,f[rson].mx),f[lson].rx+f[rson].lx); } int insert(int las,int w) { int now=++num; f[now]=f[las]; if ((f[now].l==w)&&(f[now].r==w)){f[now].mx=f[now].lx=f[now].rx=1;return now;} int mid=(f[now].l+f[now].r)/2; if (w<=mid) f[now].ls=insert(f[las].ls,w); else f[now].rs=insert(f[las].rs,w); update(now); return now; } ding2 query(int now,int lef,int righ) { if ((lef<=f[now].l)&&(f[now].r<=righ)) return f[now]; int mid=(f[now].l+f[now].r)/2; if (righ<=mid) return query(f[now].ls,lef,righ); else if (lef>mid) return query(f[now].rs,lef,righ); else { ding2 t1=query(f[now].ls,lef,righ),t2=query(f[now].rs,lef,righ),t3; t3.l=t1.l; t3.r=t2.r; t3.mx=max(t1.rx+t2.lx,max(t1.mx,t2.mx)); t3.lx=t1.mx==t1.r-t1.l+1?t1.mx+t2.lx:t1.lx; t3.rx=t2.mx==t2.r-t2.l+1?t2.mx+t1.rx:t2.rx; return t3; } } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&a[i].h); a[i].node=i; } sort(a+1,a+1+n,cmp); num=1; build(root[0],1,n); for (int i=1;i<=n;i++) root[i]=insert(root[i-1],a[i].node); scanf("%d",&m); for (int i=1;i<=m;i++) { int pl,pr,w; scanf("%d%d%d",&pl,&pr,&w); int nl=1,nr=n; while (nl<nr) { int mid=(nl+nr)/2; if (query(root[mid],pl,pr).mx>=w) nr=mid; else nl=mid+1; } printf("%d\n",a[nr].h); } return 0; }
可持续化线段树(例题Sign on Fence[Codeforces 484E])