BZOJ 4552
题意
给出一个1到n的全排列,现在对这个全排列序列进行m次局部排序,排序分为两种:
1:(0,l,r)表示将区间[l,r]的数字升序排序。
2:(1,l,r)表示将区间[l,r]的数字降序排序。
最后询问第q位置上的数字。
直接做是比较难做的
联系到我们之前做的那道 0 1 排序
如果我们二分答案
把小于等mid的都变成0 大于mid的都变成 1 最后看q位是不是 1来check
那么这道题就解决了
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 100025;
int s[MAX_N<<2],col[MAX_N<<2],opt[MAX_N],x[MAX_N],y[MAX_N],arr[MAX_N],q,ck,n,Q;
void up(int rt)
{
s[rt] = s[rt<<1] + s[rt<<1|1];
}
void down(int rt,int l,int r)
{
if(col[rt]!=-1)
{
col[rt<<1] = col[rt];
col[rt<<1|1] = col[rt];
s[rt<<1] = (mid-l+1)*col[rt];
s[rt<<1|1] = (r-mid)*col[rt];
col[rt] = -1;
}
}
void build(int rt,int l,int r)
{
s[rt] = 0;col[rt] = -1;
if(l==r)
{
if(arr[l]>=ck) s[rt] = 1;
else s[rt] = 0;
return ;
}
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
up(rt);
}
void update(int rt,int l,int r,int x,int y,int v)
{
if(x>r||y<l) return ;
if(x<=l&&r<=y)
{
col[rt] = v;
s[rt] = (r-l+1)*v;
return ;
}
down(rt,l,r);
if(y<=mid) update(rt<<1,l,mid,x,y,v);
else if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
else update(rt<<1,l,mid,x,y,v),update(rt<<1|1,mid+1,r,x,y,v);
up(rt);
}
int query(int rt,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
return s[rt];
}
down(rt,l,r);
if(y<=mid) return query(rt<<1,l,mid,x,y);
else if(x>mid) return query(rt<<1|1,mid+1,r,x,y);
else return query(rt<<1,l,mid,x,y) + query(rt<<1|1,mid+1,r,x,y);
}
bool check(int T)
{
ck = T;
build(1,1,n);
for(int i = 1;i<=Q;++i)
{
if(opt[i]==0)
{
int tmp = query(1,1,n,x[i],y[i]),tmp_ = y[i]-x[i]+1-tmp;
update(1,1,n,x[i],x[i]+tmp_-1,0);
update(1,1,n,x[i]+tmp_,y[i],1);
}
else
{
int tmp = query(1,1,n,x[i],y[i]),tmp_ = y[i]-x[i]+1-tmp;
update(1,1,n,x[i],x[i]+tmp-1,1);
update(1,1,n,x[i]+tmp,y[i],0);
}
}
if(query(1,1,n,q,q)==1) return true;
return false;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
scanf("%d%d",&n,&Q);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
for(int i = 1;i<=Q;++i)
{
scanf("%d%d%d",&opt[i],&x[i],&y[i]);
}
scanf("%d",&q);
int l = 1,r = n;
while(l<=r)
{
if(check(mid)) l = mid + 1;
else r = mid - 1;
}
if(check(r)) printf("%d\n",r);
else printf("%d\n",l);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}