#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int par[maxn],link[maxn],n,dpnah[maxn],inf=1e7,res=0,nt[maxn];
pair<int,int>all[maxn],dov[maxn];
struct lev{
vector<int>ind;
vector<int>x;
int sz=0,kaf;
struct segmentmaxa{
int kaf;
vector<int>seg;
void ins(int i,int w){
if(i==0){
return ;
}
seg[i]=max(seg[i],w);
return ins((i>>1),w);
}
void erase(int i){
if(i==0){
return ;
}
if(i>=kaf){
seg[i]=0;
}
else{
seg[i]=max(seg[(i<<1)],seg[(i<<1)^1]);
}
return erase((i>>1));
}
int pors(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr)
{
return -1;
}
if(seg[i]<=w){
return -1;
}
if(l==r){
return l;
}
int m=(l+r)>>1;
int ret=pors((i<<1),l,m,tl,tr,w);
if(ret!=-1){
return ret;
}
return pors((i<<1)^1,m+1,r,tl,tr,w);
}
}maxa;
struct segmentmina{
int kaf;
vector<int>seg;
void ins(int i,int w){
if(i==0){
return ;
}
seg[i]=min(seg[i],w);
return ins((i>>1),w);
}
void erase(int i){
if(i==0){
return ;
}
if(i>=kaf){
seg[i]=inf;
}
else{
seg[i]=min(seg[(i<<1)],seg[(i<<1)^1]);
}
return erase((i>>1));
}
int pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr)
{
return inf;
}
if(l>=tl&&r<=tr){
return seg[i];
}
int m=(l+r)>>1;
return min(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}mina;
void cre(){
sz=(int)ind.size();
kaf=nt[sz];
maxa.seg.resize(kaf*2);
mina.seg.resize(kaf*2,inf);
maxa.kaf=kaf;
mina.kaf=kaf;
}
int find(int r){
int p=upper_bound(ind.begin(),ind.end(),r)-ind.begin();
p--;
if(p<0){
return inf;
}
return mina.pors(1,0,kaf-1,0,p);
}
}lev[maxn];
void updlev(int u,int lv){
res=max(res,lv);
int p=lower_bound(lev[lv].ind.begin(),lev[lv].ind.end(),u)-lev[lv].ind.begin();
lev[lv].maxa.ins(lev[lv].kaf+p,lev[lv].x[p]);
lev[lv].mina.ins(lev[lv].kaf+p,lev[lv].x[p]);
while(true){
int z=lev[lv].maxa.pors(1,0,lev[lv].kaf-1,p+1,lev[lv].sz,lev[lv].x[p]);
if(z==-1){
break;
}
lev[lv].maxa.erase(lev[lv].kaf+z);
lev[lv].mina.erase(lev[lv].kaf+z);
updlev(lev[lv].ind[z],lv+1);
}
}
void aval(){
for(int i=0;i<maxn;i++){
par[i]=i;
}
nt[1]=2;
for(int i=2;i<maxn;i++){
nt[i]=nt[i/2]*2;
}
}
int root(int c){
if(c==par[c]){
return c;
}
return par[c]=root(par[c]);
}
void merge(int u,int v){
int rootu=root(u),rootv=root(v);
if(rootu!=rootv){
par[rootu]=rootv;
}
}
int kafa=(1<<20);
struct fc{
int seg[(1<<21)],lazy[(1<<21)];
fc(){
for(int i=0;i<(1<<21);i++){
seg[i]=inf;
}
}
void upd(int i,int w){
if(i==0){
return ;
}
seg[i]=min(seg[i],w);
return upd((i>>1),w);
}
void calc(int i){
if(i>=kafa){
seg[i]+=lazy[i];
lazy[i]=0;
return ;
}
seg[i]=min(seg[(i<<1)]+lazy[(i<<1)],seg[(i<<1)^1]+lazy[(i<<1)^1]);
}
void shift(int i){
if(lazy[i]==0){
return ;
}
if(i>=kafa){
return calc(i);
}
lazy[(i<<1)]+=lazy[i];
lazy[(i<<1)^1]+=lazy[i];
lazy[i]=0;
calc(i);
}
void updsuma(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
shift(i);
if(l>=tl&&r<=tr){
lazy[i]+=w;
return ;
}
int m=(l+r)>>1;
updsuma((i<<1),l,m,tl,tr,w);
updsuma((i<<1)^1,m+1,r,tl,tr,w);
calc(i);
return ;
}
int porsm(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return inf;
}
shift(i);
if(l>=tl&&r<=tr){
// cout<<l<<" "<<r<<" "<<seg[i]<<'\n';
return seg[i];
}
int m=(l+r)>>1;
return min(porsm((i<<1),l,m,tl,tr),porsm((i<<1)^1,m+1,r,tl,tr));
}
int porsw(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return -1;
}
shift(i);
if(seg[i]>w){
return -1;
}
if(l==r){
return l;
}
int m=(l+r)>>1;
int ret=porsw((i<<1)^1,m+1,r,tl,tr,w);
if(ret==-1){
ret=porsw((i<<1),l,m,tl,tr,w);
}
return ret;
}
int pors(){
int mina=porsm(1,0,kafa-1,0,maxn);
//cout<<mina<<endl;
return porsw(1,0,kafa-1,0,maxn,mina);
}
}fc;
vector<int>allv[maxn];
void createall(){
for(int i=1;i<=n;i++){
fc.upd(kafa+i,all[i].first);
}
for(int i=1;i<=n;i++)
{
int w=fc.pors();
//cout<<i<<" "<<w<<"\n";
fc.updsuma(1,0,kafa-1,w,w,inf);
dov[i]=make_pair(all[w].second,w);
link[w]=i;
fc.updsuma(1,0,kafa-1,0,w,1);
}
}
void createlis(){
vector<int>fake;
for(int i=1;i<=n;i++){
int p=lower_bound(fake.begin(),fake.end(),dov[i].first)-fake.begin();
if(p==(int)fake.size()){
fake.push_back(dov[i].first);
}
else{
fake[p]=dov[i].first;
}
dpnah[i]=p+1;
for(int j=1;j<=p+1;j++){
lev[j].ind.push_back(i);
lev[j].x.push_back(dov[i].first);
}
}
}
int wtf(int u){
for(int i=dov[u].first;i>=1;i--){
if(lev[i].find(u)<dov[u].first){
return i;
}
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
aval();
cin>>n;
for(int i=1;i<=n;i++){
cin>>all[i].first>>all[i].second;
}
createall();
createlis();
for(int i=0;i<maxn;i++){
lev[i].cre();
}
for(int i=1;i<=n;i++){
int ln=wtf(link[i])+1;
// cout<<i<<" "<<ln<<" "<<link[i]<<endl;
updlev(link[i],ln);
cout<<res<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
157272 KB |
Output is correct |
2 |
Correct |
83 ms |
157280 KB |
Output is correct |
3 |
Correct |
91 ms |
159276 KB |
Output is correct |
4 |
Correct |
93 ms |
159004 KB |
Output is correct |
5 |
Correct |
113 ms |
159804 KB |
Output is correct |
6 |
Correct |
104 ms |
158412 KB |
Output is correct |
7 |
Correct |
96 ms |
160136 KB |
Output is correct |
8 |
Correct |
99 ms |
160072 KB |
Output is correct |
9 |
Correct |
94 ms |
161124 KB |
Output is correct |
10 |
Correct |
100 ms |
159692 KB |
Output is correct |
11 |
Correct |
95 ms |
160864 KB |
Output is correct |
12 |
Correct |
96 ms |
158368 KB |
Output is correct |
13 |
Correct |
95 ms |
158904 KB |
Output is correct |
14 |
Correct |
97 ms |
158880 KB |
Output is correct |
15 |
Correct |
103 ms |
158452 KB |
Output is correct |
16 |
Correct |
102 ms |
158956 KB |
Output is correct |
17 |
Correct |
96 ms |
159088 KB |
Output is correct |
18 |
Correct |
99 ms |
158876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
157272 KB |
Output is correct |
2 |
Correct |
83 ms |
157280 KB |
Output is correct |
3 |
Correct |
91 ms |
159276 KB |
Output is correct |
4 |
Correct |
93 ms |
159004 KB |
Output is correct |
5 |
Correct |
113 ms |
159804 KB |
Output is correct |
6 |
Correct |
104 ms |
158412 KB |
Output is correct |
7 |
Correct |
96 ms |
160136 KB |
Output is correct |
8 |
Correct |
99 ms |
160072 KB |
Output is correct |
9 |
Correct |
94 ms |
161124 KB |
Output is correct |
10 |
Correct |
100 ms |
159692 KB |
Output is correct |
11 |
Correct |
95 ms |
160864 KB |
Output is correct |
12 |
Correct |
96 ms |
158368 KB |
Output is correct |
13 |
Correct |
95 ms |
158904 KB |
Output is correct |
14 |
Correct |
97 ms |
158880 KB |
Output is correct |
15 |
Correct |
103 ms |
158452 KB |
Output is correct |
16 |
Correct |
102 ms |
158956 KB |
Output is correct |
17 |
Correct |
96 ms |
159088 KB |
Output is correct |
18 |
Correct |
99 ms |
158876 KB |
Output is correct |
19 |
Correct |
318 ms |
195076 KB |
Output is correct |
20 |
Correct |
338 ms |
195336 KB |
Output is correct |
21 |
Correct |
625 ms |
195056 KB |
Output is correct |
22 |
Correct |
752 ms |
195440 KB |
Output is correct |
23 |
Correct |
433 ms |
195220 KB |
Output is correct |
24 |
Correct |
483 ms |
195280 KB |
Output is correct |
25 |
Correct |
450 ms |
195224 KB |
Output is correct |
26 |
Correct |
471 ms |
195168 KB |
Output is correct |
27 |
Correct |
513 ms |
195172 KB |
Output is correct |
28 |
Correct |
427 ms |
195272 KB |
Output is correct |
29 |
Correct |
452 ms |
195288 KB |
Output is correct |
30 |
Correct |
438 ms |
195200 KB |
Output is correct |
31 |
Correct |
517 ms |
195420 KB |
Output is correct |
32 |
Correct |
559 ms |
195448 KB |
Output is correct |
33 |
Correct |
512 ms |
195640 KB |
Output is correct |
34 |
Correct |
503 ms |
195452 KB |
Output is correct |
35 |
Correct |
537 ms |
195620 KB |
Output is correct |
36 |
Correct |
527 ms |
195348 KB |
Output is correct |
37 |
Correct |
512 ms |
195312 KB |
Output is correct |
38 |
Correct |
530 ms |
195340 KB |
Output is correct |
39 |
Correct |
520 ms |
195380 KB |
Output is correct |
40 |
Correct |
508 ms |
195416 KB |
Output is correct |
41 |
Correct |
262 ms |
186040 KB |
Output is correct |
42 |
Correct |
121 ms |
163448 KB |
Output is correct |
43 |
Correct |
495 ms |
185928 KB |
Output is correct |
44 |
Correct |
405 ms |
177940 KB |
Output is correct |
45 |
Correct |
122 ms |
161156 KB |
Output is correct |
46 |
Correct |
94 ms |
158436 KB |
Output is correct |
47 |
Correct |
213 ms |
172048 KB |
Output is correct |
48 |
Correct |
172 ms |
168136 KB |
Output is correct |
49 |
Correct |
101 ms |
160608 KB |
Output is correct |
50 |
Correct |
91 ms |
158792 KB |
Output is correct |
51 |
Correct |
170 ms |
167404 KB |
Output is correct |
52 |
Correct |
304 ms |
181512 KB |
Output is correct |
53 |
Correct |
272 ms |
178352 KB |
Output is correct |
54 |
Correct |
250 ms |
176516 KB |
Output is correct |
55 |
Correct |
124 ms |
162712 KB |
Output is correct |
56 |
Correct |
223 ms |
171820 KB |
Output is correct |
57 |
Correct |
141 ms |
163736 KB |
Output is correct |
58 |
Correct |
133 ms |
163500 KB |
Output is correct |
59 |
Correct |
101 ms |
159100 KB |
Output is correct |
60 |
Correct |
335 ms |
178772 KB |
Output is correct |
61 |
Correct |
89 ms |
157824 KB |
Output is correct |
62 |
Correct |
181 ms |
167984 KB |
Output is correct |
63 |
Correct |
141 ms |
163512 KB |
Output is correct |
64 |
Correct |
244 ms |
173308 KB |
Output is correct |
65 |
Correct |
208 ms |
171532 KB |
Output is correct |
66 |
Correct |
129 ms |
161572 KB |
Output is correct |
67 |
Correct |
193 ms |
168120 KB |
Output is correct |
68 |
Correct |
157 ms |
164904 KB |
Output is correct |
69 |
Correct |
213 ms |
171552 KB |
Output is correct |
70 |
Correct |
111 ms |
160716 KB |
Output is correct |
71 |
Correct |
2525 ms |
215108 KB |
Output is correct |
72 |
Correct |
2498 ms |
215248 KB |
Output is correct |
73 |
Correct |
2477 ms |
215196 KB |
Output is correct |
74 |
Correct |
2479 ms |
215080 KB |
Output is correct |
75 |
Correct |
2475 ms |
214972 KB |
Output is correct |
76 |
Correct |
2487 ms |
215072 KB |
Output is correct |