codeforces #222 div 1 D Developing Game
很神奇的线段树
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
using namespace std;
#define maxn 400000
int n;
struct P{
int l,r,v,num;
}a[maxn];
struct NODE{
int l,r,maxx,e,ma;
}tree[maxn*2];
int xx[maxn];
void down(int x){
int y=x<<1,z=(x<<1)+1;
if(tree[x].e){
int u=tree[x].e;
tree[y].e+=u;
tree[y].maxx+=u;
tree[z].e+=u;
tree[z].maxx+=u;
tree[x].e=0;
}
}
int nn,e[maxn],ne[maxn],v[maxn];
void add(int x,int y){
ne[++nn]=e[x],e[x]=nn,v[nn]=y;
}
void up(int x){
int y=x<<1,z=(x<<1)+1;
if(tree[y].maxx>tree[z].maxx)tree[x].maxx=tree[y].maxx,tree[x].ma=tree[y].ma;
else tree[x].maxx=tree[z].maxx,tree[x].ma=tree[z].ma;
}
void build(int l,int r,int x){
tree[x].l=l,tree[x].r=r,tree[x].e=0;
if(l==r){
tree[x].ma=l,tree[x].maxx=0;
return;
}
int mid=(l+r)>>1;
build(l,mid,x<<1);
build(mid+1,r,(x<<1)+1);
up(x);
}
void change(int l,int r,int x,int w){
if(tree[x].l>=l&&tree[x].r<=r){
tree[x].maxx+=w;
tree[x].e+=w;
return;
}
down(x);
int y=x<<1,z=(x<<1)+1;
if(tree[y].l<=r&&tree[y].r>=l)change(l,r,y,w);
if(tree[z].l<=r&&tree[z].r>=l)change(l,r,z,w);
up(x);
}
bool cmp(P x,P y){
return x.v<y.v;
}
int fen(int x){
int le=0,ri=n+1;
while(le+1!=ri){
int mid=(le+ri)>>1;
if(a[mid].v>a[x].r)ri=mid;
else le=mid;
}
return ri;
}
int fen2(int x){
int le=0,ri=n;
while(le+1!=ri){
int mid=(le+ri)>>1;
if(a[mid].v>=a[x].l)ri=mid;
else le=mid;
}
return ri;
}
void ask(int l,int x){
if(tree[x].l==tree[x].r){
cout<<tree[x].maxx;
return;
}
down(x);
if(tree[x<<1].l>=l)ask(l,x<<1);
else ask(l,(x<<1)+1);
}
int ans;
int a1[maxn],an;
bool been[maxn];
void ins(int x){change(xx[x],x,1,1);}
void dele(int x){change(xx[x],x,1,-1);}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d%d%d",&a[i].l,&a[i].v,&a[i].r),a[i].num=i;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)add(fen(i),i);
for(int i=1;i<=n;i++)xx[i]=fen2(i);
build(1,n,1);
for(int i=1;i<=n;i++){
for(int j=e[i];j;j=ne[j])dele(v[j]);
ins(i);
ans=max(ans,tree[1].maxx);
}
build(1,n,1);
for(int i=1;i<=n;i++){
for(int j=e[i];j;j=ne[j])been[v[j]]=0,dele(v[j]);
ins(i);
been[i]=1;
if(ans==tree[1].maxx){
printf("%d\n",ans);
for(int j=tree[1].ma;j<=i;j++){
if(been[j]&&a[j].l<=a[tree[1].ma].v){
a1[++an]=a[j].num;
}
}
sort(a1+1,a1+an+1);
for(int k=1;k<=an;k++)printf("%d ",a1[k]);
exit(0);
}
}
}