bzoj4552: [Tjoi2016&Heoi2016]排序(二分+線段樹)
題目傳送門
神!
解法:
直接做難做。考慮線段樹維護。
二分答案。大於等於答案的設為1。比答案小的設為0。
可以用線段樹快速統計出區間1和0的個數。
如果是升序的話就把所有0排在前面。
降序則反之。
最後看下q這個位置是否為1即可。
程式碼實現:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
struct node {int l,r,lc,rc,c[2],lazy;}tr[410000];int len;
void bt(int l,int r) {
len++;int now=len;
tr[now].l=l;tr[now].r=r;tr[now].c[1]=tr[now].c[0]=0;tr[now].lc=tr[now].rc=-1;tr[now].lazy=-1;
if(l<r) {int mid=(l+r)/2;tr[now].lc=len+1;bt(l,mid);tr[now].rc=len+1;bt(mid+1,r);}
}
void update(int now) {
int lc=tr[now].lc,rc=tr[now].rc;
if(tr[now].lc==-1)return ;
tr[lc].c[tr[now].lazy]=tr[lc].r-tr[lc].l+1;tr[lc].c[1-tr[now].lazy]=0;
tr[rc].c[tr[now].lazy]=tr[rc].r-tr[rc].l+1;tr[rc].c[1-tr[now].lazy]=0;
tr[lc].lazy=tr[rc].lazy=tr[now].lazy;tr[now].lazy=-1;
}
int find_sum(int now,int l,int r,int k) {
if(tr[now].lazy!=-1)update(now);
if(tr[now].l==l&&tr[now].r==r)return tr[now].c[k];
int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
if(r<=mid)return find_sum(lc,l,r,k);else if(l>mid)return find_sum(rc,l,r,k);
else return find_sum(lc,l,mid,k)+find_sum(rc,mid+1,r,k);
}
void change(int now,int l,int r,int k) {
if(tr[now].lazy!=-1)update(now);
if(tr[now].l==l&&tr[now].r==r) {tr[now].c[k]=r-l+1;tr[now].c[1-k]=0;tr[now].lazy=k;return ;}
int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
if(r<=mid)change(lc,l,r,k);else if(l>mid)change(rc,l,r,k);
else {change(lc,l,mid,k);change(rc,mid+1,r,k);}
for(int i=0;i<=1;i++)tr[now].c[i]=tr[lc].c[i]+tr[rc].c[i];
}
int n,m,q,a[210000];
struct Q {int t,l,r;}s[210000];
int main() {
scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)scanf("%d%d%d",&s[i].t,&s[i].l,&s[i].r);
int l=1,r=n,mid,ans=0;len=0;bt(1,n);scanf("%d",&q);
while(l<=r) {
mid=(l+r)/2;
for(int i=1;i<=n;i++) {if(a[i]>=mid)change(1,i,i,1);else change(1,i,i,0);}
for(int i=1;i<=m;i++) {
if(s[i].t==0) {
int c=find_sum(1,s[i].l,s[i].r,0);
if(c!=0)change(1,s[i].l,s[i].l+c-1,0);
if(c!=s[i].r-s[i].l+1)change(1,s[i].l+c,s[i].r,1);
}
else {
int c=find_sum(1,s[i].l,s[i].r,1);
if(c!=0)change(1,s[i].l,s[i].l+c-1,1);
if(c!=s[i].r-s[i].l+1)change(1,s[i].l+c,s[i].r,0);
}
}int c=find_sum(1,q,q,1);
if(c==1) {ans=mid;l=mid+1;}else r=mid-1;
}printf("%d\n",ans);
return 0;
}
相關文章
- 【TJOI2016】【bzoj4552】排序(二分答案+線段樹01排序)排序
- 線~段~樹
- 線段樹 hate it
- 01 線段樹
- 線段樹--RMQMQ
- 線段樹模板
- CF798E. Mike and code of a permutation [拓撲排序 線段樹]排序
- NOIP2012借教室[線段樹|離線 差分 二分答案]
- 線段樹筆記筆記
- 線段樹入門
- 權值線段樹
- 線段樹(毒瘤)總結
- 線段樹模板總結
- 深入理解線段樹
- 線段樹入門理解
- 第二課——線段樹
- 線段樹簡單思路
- 線段樹 - 多組圖帶你從頭到尾徹底理解線段樹
- 資料結構之樹( 線段樹,字典樹)資料結構
- 資料結構-線段樹資料結構
- 關於線段樹基礎
- 淺談線段樹(Segment Tree)
- hdu 1754 I Hate It (線段樹)
- [18/03/24] 線段樹模板
- 動態開點線段樹
- hihocoder 1078 線段樹的區間修改 (線段樹 區間更新 模板)
- hdu4288 離線處理線段樹
- 線段樹+差分——【模板】樹狀陣列2陣列
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- 一些線段樹典(求求了區域賽遇到線段樹不要被卡了)
- hdu 2665 可持久化線段樹求區間第K大值(函式式線段樹||主席樹)持久化函式
- 線段樹差分及其應用
- 數列求和【線段樹基礎】
- Transformation HDU - 4578線段樹綜合操作ORM
- C++演算法 線段樹C++演算法
- C語言 編寫線段樹C語言
- Lintcode---線段樹修改
- 線段樹經驗及總結