bzoj4444 国旗计划
倍增,预处理出每个点往后$2^i$个应该选哪个人
我用的treap就是快
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 namespace treap 34 { 35 const int xxx=400040; 36 int w[xxx],id[xxx],r[xxx],ch[xxx][2],ed,root; 37 void rotate(int &k,int d) 38 { 39 int p=ch[k][d^1]; 40 ch[k][d^1]=ch[p][d]; 41 ch[p][d]=k;k=p; 42 } 43 void add(int &k,int x,int ID) 44 { 45 if(!k) 46 { 47 k=++ed;w[k]=x,id[k]=ID;r[k]=rand(); 48 return ; 49 } 50 int d=x>w[k]; 51 add(ch[k][d],x,ID); 52 if(r[ch[k][d]]<r[k])rotate(k,d^1); 53 } 54 int findhou(int x) 55 { 56 int k=root,ret=0; 57 while(k) 58 if(w[k]>x)ret=w[k],k=ch[k][0]; 59 else k=ch[k][1]; 60 return ret; 61 } 62 int findqian(int x) 63 { 64 if(!x)x=2147483647; 65 int k=root,ret=0; 66 while(k) 67 if(w[k]<x)ret=id[k],k=ch[k][1]; 68 else k=ch[k][0]; 69 return ret; 70 } 71 void add(int x,int ID){add(root,x,ID);} 72 }; 73 int n,m; 74 struct wocao 75 { 76 int l,r,id; 77 void in(int i){inin(l),inin(r),id=i;if(r<l)r+=m;} 78 bool op < (const wocao &a)const {return l<a.l;} 79 }a[400040]; 80 int fa[400040][20],ans[400040]; 81 void hh(int x) 82 { 83 int temp=a[x].id,R=m+a[x].l; 84 rre(i,18,0)if(fa[x][i]) 85 if(a[fa[x][i]].r<R) 86 x=fa[x][i],ans[temp]+=1<<i; 87 } 88 set<pair<int,int> >ss; 89 int main() 90 { 91 freopen("in.in","r",stdin); 92 freopen("out.out","w",stdout); 93 inin(n),inin(m); 94 re(i,1,n)a[i].in(i); 95 re(i,n+1,n<<1)a[i]=a[i-n],a[i].l+=m,a[i].r+=m,a[i].id=0; 96 n<<=1; 97 sort(a+1,a+n+1); 98 // for(int i=1;i<=n;i++)printf("%d %d\n",a[i].l,a[i].r); 99 treap::add(a[n].l,n); 100 // ss.insert(make_pair(a[n].l,n)); 101 rre(i,n-1,1) 102 { 103 fa[i][0]=treap::findqian(treap::findhou(a[i].r)); 104 // fa[i][0]=(--ss.upper_bound(make_pair(a[i].r,0x7FFFFFFF)))->second; 105 // printf("%d\n",fa[i][0]); 106 treap::add(a[i].l,i); 107 // ss.insert(make_pair(a[i].l,i)); 108 } 109 re(i,1,18)rre(j,n,1)fa[j][i]=fa[fa[j][i-1]][i-1]; 110 re(i,1,n)if(a[i].id) 111 hh(i); 112 re(i,1,n>>1)printf("%d ",ans[i]+2); 113 return 0; 114 }