http://www.lydsy.com/JudgeOnline/problem.php?id=4552
今天七道题(6/7)。(看来是做不完了)
二分答案 mid ,问题就成了判断第Q个数是不是大于等于 mid ,把小于 mid 的变成0,大于 mid 的变成1,排序过程就简化了,直接用线段树的区间赋值操作模拟就好了。最后check的结果就是Q这个位置上的数是否是1。
//线段树
#include
#include
#define maxn 100010
using namespace std;
int N, M, opt[maxn][3], ndtot, num[maxn], c[maxn], Q;
struct segtree
{
int l, r, set, c0, c1;
segtree *ch[2];
}pool[maxn<<2], *root;
inline void pushdown(segtree *p)
{
if(p->set==0)p->c0=p->r-p->l+1, p->c1=0;
else if(p->set==1)p->c0=0,p->c1=p->r-p->l+1;
else return;
if(p->ch[0])p->ch[0]->set=p->ch[1]->set=p->set;
p->set=-1;
}
inline void pushup(segtree *p)
{
if(!p->ch[0])return;
pushdown(p->ch[0]),pushdown(p->ch[1]);
p->c0=p->ch[0]->c0+p->ch[1]->c0;
p->c1=p->ch[0]->c1+p->ch[1]->c1;
}
void segset(segtree *p, int l, int r, int c)
{
pushdown(p);
int mid=(p->l+p->r)>>1;
if(l<=p->l and r>=p->r){p->set=c;return;}
if(l<=mid)segset(p->ch[0],l,r,c);
if(r>mid)segset(p->ch[1],l,r,c);
pushup(p);
}
int segcnt(segtree *p, int l, int r, int c)
{
pushdown(p);
int mid=(p->l+p->r)>>1, ans=0;
if(l<=p->l and r>=p->r)return c?p->c1:p->c0;
if(l<=mid)ans+=segcnt(p->ch[0],l,r,c);
if(r>mid)ans+=segcnt(p->ch[1],l,r,c);
return ans;
}
void build(segtree *p, int l, int r)
{
int mid=(l+r)>>1;
p->l=l,p->r=r;p->set=-1;
if(l==r)
{
if(c[l]==0)p->c0=1,p->c1=0;
else p->c0=0,p->c1=1;
return;
}
build(p->ch[0]=pool+ ++ndtot,l,mid);
build(p->ch[1]=pool+ ++ndtot,mid+1,r);
pushup(p);p->set=-1;
}
inline int read(int x=0)
{
char c=getchar();
while(c<48 or c>57)c=getchar();
while(c>=48 and c<=57)x=(x<<1)+(x<<3)+c-48,c=getchar();
return x;
}
inline void input()
{
int i;
N=read(), M=read();
for(i=1;i<=N;i++)num[i]=read();
for(i=1;i<=M;i++)opt[i][0]=read(), opt[i][1]=read(), opt[i][2]=read();
Q=read();
}
inline bool check(int x)
{
int i, l, r, type, a, b;
for(i=1;i<=N;i++)c[i]=num[i]>=x;
build(root=pool+(ndtot=1),1,N);
for(i=1;i<=M;i++)
{
type=opt[i][0], l=opt[i][1], r=opt[i][2];
a=segcnt(root,l,r,0), b=segcnt(root,l,r,1);
if(type==0)
{
if(a)segset(root,l,l+a-1,0);if(b)segset(root,r-b+1,r,1);
}
else
{
if(b)segset(root,l,l+b-1,1);if(a)segset(root,r-a+1,r,0);
}
}
return (bool)segcnt(root,Q,Q,1);
}
int main()
{
int l, r, mid;
input();
for(l=1,r=N;l1)>>1;
if(check(mid))l=mid;
else r=mid-1;
}
printf("%d",l);
return 0;
}