QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#165353 | #5410. 树特卡克林 | Crysfly | 0 | 0ms | 0kb | C++17 | 4.1kb | 2023-09-05 17:41:42 | 2023-09-05 17:41:43 |
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 200005
#define inf 0x3f3f3f3f
int n,m;
const int V=(1<<17);
struct bit{
int tr[1<<18|5],sum;
void ins(int x){
++sum;
int p=x|V;
while(p)++tr[p],p>>=1;
}
void del(int x){
--sum;
int p=x|V;
while(p)--tr[p],p>>=1;
}
void out(){
For(i,0,7)cout<<tr[i]<<" ";puts("");
}
int kth(int k){
int p=1;
For(i,1,17){
if(k<=tr[p<<1])p<<=1;
else k-=tr[p<<1],p=p<<1|1;
}
return p^V;
}
}T;
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
int fa[maxn],ch[maxn][2],sz[maxn],si[maxn],mx[maxn],val[maxn];
bool rev[maxn];
struct myset{
priority_queue<pii>q1,q2;
void insert(pii o){q1.push(o);}
void erase(pii o){q2.push(o);}
pii top(){
while(q2.size() && q1.top()==q2.top())q1.pop(),q2.pop();
return q1.top();
}
int size(){return q1.size()-q2.size();}
}s[maxn];
// (size,max)
bool nrt(int p){return ls(fa[p])==p||rs(fa[p])==p;}
bool dir(int p){return rs(fa[p])==p;}
void up(int p){
mx[p]=max(p,max(mx[ls(p)],mx[rs(p)]));
sz[p]=sz[ls(p)]+sz[rs(p)]+1+si[p];
val[p]=val[ls(p)]^val[rs(p)]^p;
}
void pr(int x){
rev[x]^=1;
swap(ls(x),rs(x));
}
void down(int x){
if(rev[x]){
if(ls(x))pr(ls(x));
if(rs(x))pr(rs(x));
rev[x]=0;
}
}
void downall(int p){
if(nrt(p))downall(fa[p]);
down(p);
}
void rot(int x){
int y=fa[x],z=fa[y],k=dir(x),s=ch[x][!k];
fa[x]=z; if(nrt(y)) ch[z][dir(y)]=x;
fa[y]=x; ch[x][!k]=y;
if(s)fa[s]=y; ch[y][k]=s; up(y),up(x);
}
void splay(int x){
if(!x)return;
downall(x);
while(nrt(x)){
int y=fa[x];
if(nrt(y))rot(dir(x)==dir(y)?y:x);
rot(x);
}
}
int findrt(int x){
splay(x);
if(!fa[x]){
down(x);
while(ls(x))down(x=ls(x));
return x;
}
return findrt(fa[x]);
}
int st[maxn],tp,lst;
void find(int x,int tmp){
// cout<<"find "<<x<<" "<<k<<" "<<tmp<<"\n";
down(x);
if(sz[rs(x)]+tmp>=lst*2) find(rs(x),tmp);
tmp+=sz[rs(x)]+si[x]+1;
// cout<<"tmp "<<x<<" "<<tmp<<"\n";
if(tmp>=lst*2){
st[++tp]=x;
lst=tmp;
}
if(sz[ls(x)]+tmp>=lst*2) find(ls(x),tmp);
}
void make(int x,int y){
// cout<<"make "<<x<<" "<<y<<"\n";
T.del(val[x]);
if(rs(x)){
si[x]+=sz[rs(x)];
s[x].insert(mkp(sz[rs(x)],mx[rs(x)]));
T.ins(val[rs(x)]);
}
splay(y);
if(rs(x)=y){
si[x]-=sz[y];
s[x].erase(mkp(sz[y],mx[y]));
T.del(val[y]);
}
up(x),T.ins(val[x]);
}
void check(int x){
// cout<<"check "<<x<<"\n";
splay(x);
if(s[x].size()){
pii it=s[x].top();
if(it>mkp(sz[rs(x)],mx[rs(x)])) make(x,it.se);
}
}
void acc(int x){
// cout<<"acc "<<x<<"\n";
tp=0;
for(int y=0;x;x=fa[y=x]) splay(x),st[++tp]=x;
Rep(i,tp,1){
splay(st[i]);
make(st[i],st[i-1]);
}
}
void calcall(int x){
// cout<<"calcall "<<x<<"\n";
tp=0,lst=0; find(x,0);
// For(i,1,tp)cout<<st[i]<<" ";puts(" st");
For(i,1,tp){
int u=st[i];
check(u);
}
}
void mkrt(int x){
acc(x),splay(x),pr(x);
calcall(x);
splay(x);
}
void cut(int x,int y){
splay(x); int szx=sz[rs(x)]+1+si[x];
splay(y); int szy=sz[rs(y)]+1+si[y];
if(szx<szy)swap(x,y),swap(szx,szy);
acc(x),splay(y);
si[x]-=sz[y],fa[y]=0,s[x].erase(mkp(sz[y],mx[y])),up(x);
calcall(x);
}
void link(int x,int y){
mkrt(y),acc(x);
fa[y]=x,si[x]+=sz[y],s[x].insert(mkp(sz[y],mx[y])),up(x);
// puts("linked");
calcall(x);
}
signed main()
{
n=read(),m=read();
For(i,1,n)T.ins(i),up(i);
// link(1,2);
// mkrt(2);
// T.out();
// For(i,1,3)cout<<fa[i]<<" "<<ls(i)<<" "<<rs(i)<<" "<<val[i]<<" "<<rev[i]<<"\n";
// exit(0);
For(_,1,m){
int op=read(),u=read(),v=read(),k=read();
if(op==1)link(u,v);
else cut(u,v);
// T.out();
printf("%d\n",T.kth((k-1)%T.sum+1));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Memory Limit Exceeded
Test #1:
score: 0
Memory Limit Exceeded
input:
10 50 1 4 5 8 1 2 10 6 1 7 10 1 1 2 5 7 1 1 5 1 1 8 3 1 1 5 3 3 1 1 9 3 0 10 2 8 1 10 1 6 0 1 10 1 0 4 5 9 1 8 4 9 0 3 5 3 0 10 7 6 1 1 4 1 0 4 8 3 1 5 6 2 1 10 7 1 1 2 7 1 0 2 7 8 0 4 1 10 1 7 2 6 1 2 4 2 1 2 3 4 0 5 6 7 1 8 6 1 0 2 7 7 1 10 3 1 0 3 8 5 0 2 4 7 1 4 10 9 0 1 9 8 0 2 3 1 1 10 8 4 1 1...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Memory Limit Exceeded
Test #4:
score: 0
Memory Limit Exceeded
input:
100000 99999 1 1 2 44734 1 2 3 61340 1 1 4 68331 1 2 5 86313 1 4 6 26991 1 4 7 72397 1 3 8 24098 1 5 9 31437 1 5 10 82367 1 5 11 20958 1 11 12 38919 1 12 13 87596 1 2 14 41335 1 1 15 78828 1 14 16 73861 1 9 17 81368 1 1 18 40205 1 9 19 60737 1 4 20 9347 1 5 21 84645 1 18 22 20063 1 15 23 98769 1 15 ...
output:
result:
Subtask #5:
score: 0
Memory Limit Exceeded
Test #5:
score: 0
Memory Limit Exceeded
input:
100000 99999 1 2 1 30000 1 3 2 76304 1 4 1 35053 1 5 4 38046 1 6 4 43843 1 7 1 64206 1 8 6 52957 1 9 3 42768 1 10 8 6520 1 11 5 43975 1 12 1 64810 1 13 12 64219 1 14 10 78432 1 15 4 62142 1 16 15 235 1 17 3 77806 1 18 17 58130 1 19 5 47941 1 20 14 19568 1 21 7 99780 1 22 17 39362 1 23 4 10525 1 24 1...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #3:
0%
Subtask #7:
score: 0
Skipped
Dependency #1:
0%