QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#374991 | #4894. 学姐买瓜 | marher | 0 | 8ms | 9904kb | C++14 | 2.6kb | 2024-04-02 20:29:32 | 2024-04-02 20:29:36 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+500;
int v[N],s[N],mi[N],ch[N][2],rev[N],f[N];
#define ls ch[x][0]
#define rs ch[x][1]
int isroot(int x){return ch[f[x]][0]==x|ch[f[x]][1]==x;}//rev 0,1
void up(int x){s[x]=s[ls]+s[rs]+v[x];mi[x]=min(x,min(mi[ls],mi[rs]));}
void reversal(int x){swap(ls,rs);rev[x]^=1;}
void down(int x){if(!rev[x])return;reversal(ls),reversal(rs);rev[x]=0;}
void rotate(int x)
{
int y=f[x],z=f[y],k=(ch[y][1]==x),w=(ch[z][1]==y),an=ch[x][k^1];
if(isroot(y))ch[z][w]=x;ch[x][k^1]=y;ch[y][k]=an;
if(an)f[an]=y;f[y]=x,f[x]=z;
up(y);up(x);
}
int st[N];
void splay(int x)
{
int y=x,z=0;
st[++z]=x;
while(isroot(y))st[++z]=y=f[y];
while(z)down(st[z--]);
while(isroot(x))
{
y=f[x],z=f[y];
if(isroot(y))rotate((ch[y][1]==x)^(ch[z][1]==y)?x:y);
rotate(x);
}
up(x);
}
void access(int x){for(int y=0;x;x=f[y=x])splay(x),rs=y,up(x);}
void make_root(int x){access(x);splay(x);reversal(x);}
int find_root(int x){access(x);splay(x);while(ls)down(x),x=ls;splay(x);return x;}
void split(int x,int y){make_root(x);access(y);splay(y);}
void link(int x,int y){make_root(x);if(find_root(y)!=x)f[x]=y;}
void cut(int x,int y){make_root(x);if(find_root(y)==x&&f[y]==x&&!ch[y][0])f[y]=rs=0,up(x);}
int n,m;
char cch,B0[1<<15],*S=B0,*T=B0;
#define getc() (S==T&&(T=(S=B0)+fread(B0,1,1<<15,stdin),S==T)?0:*S++)
int aa;int read(){
while(cch=getc(),cch<'0'||cch>'9');aa=cch-'0';
while(cch=getc(),cch>='0'&&cch<='9')aa=((aa<<3)+(aa<<1))+cch-'0';return aa;
}
set<int>t;
int to[N];
void change(int x,int y,int w)
{
// cout<<"change "<<x<<' '<<y<<' '<<w<<'\n';
splay(x);v[x]=w;s[x]=s[ls]+s[rs]+w;cut(x,to[x]);link(x,to[x]=y);
}
void merge(int l,int p)
{
auto it=lower_bound(t.begin(),t.end(),l);
if(to[*it]<=p)return;
auto ed=it;
while(it!=t.begin())
{
--it;
if(to[*it]<=p)break;
change(*it,(*it)+1,0);
}
it++;t.erase(it,ed);change(l,p,1);t.insert(l);
}
void dfs(int x)
{
down(x);
if(ls)dfs(ls);
if(rs)dfs(rs);
}
int find(int x,int r)
{
if(!x)return 0;
down(x);
// cout<<"find "<<x<<' '<<ls<<' '<<rs<<' '<<mi[ls]<<' '<<mi[rs]<<' '<<s[x]<<' '<<v[x]<<'\n';
if(!ls&&!rs)return 0;
if(mi[rs]<=r)return s[ls]+v[x]+find(rs,r);
if(x<=r)return s[ls];
return find(ls,r);
}
int main()
{
// freopen("in","r",stdin);
m=read(),n=read();mi[0]=1e9;
for(int i=1;i<=n;i++)f[i]=to[i]=i+1;make_root(n+1);
t.insert(n);to[n]=n+1;t.insert(0);
while(m--)
{
int opt=read(),l=read(),r=read();
if(opt==1)merge(l,r+1);
else
{
split(l,n+1);
// dfs(n+1);
cout<<find(n+1,r+1)<<'\n';
// if(m>10000)return 0;m=19999999;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 20
Accepted
time: 2ms
memory: 9856kb
input:
11 13 2 4 4 1 11 12 1 1 5 1 2 3 1 2 10 2 2 8 1 6 6 2 2 10 1 6 11 2 2 3 2 2 13
output:
0 1 2 1 3
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 9904kb
input:
2000 2000 2 66 273 1 475 1570 2 51 958 2 731 1771 1 1286 1627 1 37 892 1 529 890 2 155 1486 1 87 1815 1 576 1872 2 1269 1515 2 1521 1794 2 634 1887 2 204 1668 1 351 1679 2 1571 1599 1 243 681 2 1 2000 2 1 2000 2 564 648 2 1215 1807 2 466 1617 1 1119 1348 1 497 886 2 1358 1487 2 173 1974 1 401 1294 2...
output:
0 0 0 1 0 0 1 2 0 2 2 0 1 1 0 2 1 1 0 1 0 1 0 0 1 2 1 1 1 2 1 1 0 0 2 2 0 2 2 0 1 3 0 0 4 0 0 2 2 5 2 0 4 0 2 0 2 3 3 0 0 1 3 2 0 3 6 1 0 1 1 4 0 8 0 8 1 3 1 8 1 4 9 2 2 0 4 5 2 9 3 0 9 1 3 8 9 1 0 7 0 8 5 7 0 1 0 6 10 2 6 0 1 0 6 4 6 5 4 4 4 0 10 0 6 2 8 9 1 10 5 7 8 10 1 10 8 5 2 6 1 5 10 8 10 5 3...
result:
ok 1020 lines
Test #3:
score: 0
Accepted
time: 2ms
memory: 9872kb
input:
2000 2000 2 66 273 1 475 1570 2 51 958 2 731 1771 1 1286 1627 1 37 892 1 529 890 2 155 1486 1 87 1815 1 576 1872 2 1269 1515 2 1521 1794 2 634 1887 2 204 1668 1 351 1679 2 1571 1599 1 243 681 2 1 2000 2 1 2000 2 564 648 2 1215 1807 2 466 1617 1 1119 1348 1 497 886 2 1358 1487 2 173 1974 1 401 1294 2...
output:
0 0 0 1 0 0 1 2 0 2 2 0 1 1 0 2 1 1 0 1 0 1 0 0 1 2 1 1 1 2 1 1 0 0 2 2 0 2 2 0 1 3 0 0 4 0 0 2 2 5 2 0 4 0 2 0 2 3 3 0 0 1 3 2 0 3 6 1 0 1 1 4 0 8 0 8 1 3 1 8 1 4 9 2 2 0 4 5 2 9 3 0 9 1 3 8 9 1 0 7 0 8 5 7 0 1 0 6 10 2 6 0 1 0 6 4 6 5 4 4 4 0 10 0 6 2 8 9 1 10 5 7 8 10 1 10 8 5 2 6 1 5 10 8 10 5 3...
result:
ok 1020 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 9800kb
input:
14 11 1 1 8 1 4 11 2 4 8 1 2 7 1 7 11 2 2 9 1 6 10 1 2 6 1 8 10 1 2 6 2 9 10 1 9 9 1 3 10 1 2 4
output:
0 1 0
result:
ok 3 lines
Test #5:
score: -20
Wrong Answer
time: 8ms
memory: 9896kb
input:
2000 2000 1 1589 1640 1 1741 1765 2 191 1596 1 426 493 2 1434 1606 1 925 955 2 589 1148 2 1347 1608 2 686 1516 1 1535 1563 1 1835 1841 1 1513 1537 2 30 1710 2 123 171 2 1 2000 2 128 1310 2 270 879 1 1918 1941 2 965 1951 2 176 1452 1 1391 1421 1 614 664 2 1 2000 1 296 328 1 1378 1402 1 29 47 1 92 123...
output:
0 0 1 0 1 4 0 6 2 1 5 2 9 12 4 0 6 14 3 3 0 1 13 3 6 19 13 20 1 4 2 10 1 5 4 8 3 5 24 18 9 17 13 0 28 22 4 6 13 1 13 4 15 5 2 16 1 33 25 16 18 17 8 17 23 36 22 27 9 23 9 7 17 2 12 16 39 11 32 40 4 10 15 23 21 14 10 15 6 43 17 3 17 0 1 15 14 29 33 8 44 44 5 10 27 22 11 6 23 0 7 24 14 24 1 9 36 15 39 ...
result:
wrong answer 684th lines differ - expected: '78', found: '77'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%