QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#901#580157#9371. Mountain BookingDisplace_CrysflyFailed.2024-09-24 18:28:292024-09-24 18:28:31

Details

Extra Test:

Accepted
time: 5702ms
memory: 125928kb

input:

100000 199998 200000 200000
1 2 939994783
2 3 853789874
3 4 839427487
3 5 525972778
1 6 191650973
6 7 501978797
5 8 182499698
7 9 357203136
2 10 426625864
9 11 409707015
11 12 600615978
11 13 14950793
10 14 911772100
7 15 256893241
15 16 843599658
8 17 170850391
17 18 228998185
17 19 576166980
11 20...

output:

129940247743
129891455413
129943414607
129894666680
129907537356
129961662870
129907684704
129736863036
129948577203
129954757215
129922837577
129945493290
129922490068
129965815087
129856723678
129951690226
129934344063
129839993810
129878869223
129921118419
129954757215
129810479548
129882389809
1...

result:

ok 200000 lines

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#580157#9371. Mountain BookingCrysflyAC ✓5815ms134544kbC++144.9kb2024-09-21 20:18:102024-09-24 18:23:49

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#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;

#define pb push_back
typedef pair<int,int>pii;
#define fi first
#define se second
#define mkp make_pair
typedef vector<int>vi;
#define inf 0x3f3f3f3f

#define maxn 1000005

int read(){
	char c=getchar();int x=0;
	for(;!isdigit(c);c=getchar());
	for(;isdigit(c);c=getchar())x=x*10+(c&15);
	return x;
}

int n,m,m1,m2;
vector<pii>qs[maxn];
vi per[maxn];
ll res[maxn];

#define ls(p) ch[p][0]
#define rs(p) ch[p][1]
int ch[maxn][2],fa[maxn],w[maxn],mx[maxn];
bool rev[maxn];
void up(int p){
	mx[p]=w[p];
	if(ls(p))mx[p]=max(mx[p],mx[ls(p)]);
	if(rs(p))mx[p]=max(mx[p],mx[rs(p)]);
}
bool chk(int p){
	return rs(fa[p])==p;
}
bool nrt(int p){
	return ls(fa[p])==p || rs(fa[p])==p;
}
void pr(int p){
	swap(ls(p),rs(p));
	rev[p]^=1;
}
void down(int p){
	if(rev[p]){
		if(ls(p))pr(ls(p));
		if(rs(p))pr(rs(p));
		rev[p]=0;
	}
}
void rot(int x){
//	int y=fa[x],k=chk(x),s=ch[x][!k];
//	if(nrt(y)) ch[fa[y]][chk(y)]=x; fa[x]=fa[y];
//	ch[y][k]=s; if(s) fa[s]=y;
//	ch[x][!k]=y; fa[y]=x;
//	
//	up(y); return;
	if(!chk(x)){
		int y=fa[x];
		if(nrt(y)) ch[fa[y]][chk(y)]=x; fa[x]=fa[y];
		ch[y][0]=ch[x][1]; if(ch[x][1]) fa[ch[x][1]]=y;
		ch[x][1]=y; fa[y]=x; up(y);
	}else{
		int y=fa[x];
		if(nrt(y)) ch[fa[y]][chk(y)]=x; fa[x]=fa[y];
		ch[y][1]=ch[x][0]; if(ch[x][0]) fa[ch[x][0]]=y;
		ch[x][0]=y; fa[y]=x; up(y);
	}
}
void downall(int x){
	if(nrt(x))downall(fa[x]);
	down(x);
}
void splay(int x){
	downall(x);
	while(nrt(x)){
		int y=fa[x];
		if(nrt(y)) rot(chk(x)==chk(y)?y:x);
		rot(x);
	}up(x);
}
void acc(int x){
//	cout<<"acc "<<x<<"\n";
	for(int y=0;x;x=fa[y=x]){
		splay(x);
		rs(x)=y;
		up(x);
	}
}
void mkrt(int x){
	acc(x),splay(x),pr(x);
}
void link(int x,int y){
//	cerr<<"link "<<x<<" "<<y<<"\n";
	mkrt(x);
	fa[x]=y;
}
void cut(int x,int y){
	mkrt(x),acc(y),splay(y);
	fa[ls(y)]=0;
	ls(y)=0; up(y);
}

bool bru[maxn];
int eu[maxn],ev[maxn],ew[maxn],in[maxn],dl[maxn],ids[maxn],tot;

int eu2[maxn],ev2[maxn],ew2[maxn],to[maxn];
void cute(int id){
	in[to[id]]=0;
	cut(id+n,eu[id]);
	cut(id+n,ev[id]);
}
void linke(int id){
//	cerr<<"lnk "<<id<<"\n";
	in[to[id]]=1;
	link(id+n,eu[id]);
	link(id+n,ev[id]);
}

int ff[maxn];
int gf(int x){
	while(x!=ff[x])x=ff[x]=ff[ff[x]];
	return x;
}


int L[maxn],R[maxn],cnt;
int cntb[maxn];
ll ans[maxn],ww[maxn];

void dfs0(int u,ll now){
	if(u>n){
		dfs0(L[u],now+cntb[R[u]]*ww[u]);
		dfs0(R[u],now+cntb[L[u]]*ww[u]);
	}else{
		ans[u]=now;
	}
}
bool flag;

int *Ls[maxn],*Rs[maxn];
void solve(int id){
	For(i,1,n)ff[i]=i,cntb[i]=0,ans[i]=0;
	for(int x:per[id])cntb[x]++;
	cnt=n;
	For(i,1,tot){
		if(!in[i])continue;
		int u=eu2[i],v=ev2[i];
		u=gf(u),v=gf(v);
		++cnt;
		L[cnt]=u,R[cnt]=v;
		ww[cnt]=ew2[i];
		cntb[cnt]=cntb[u]+cntb[v];
		ff[u]=ff[v]=ff[cnt]=cnt;
	}
	//if(flag)return;
	ans[cnt]=0;
	Rep(u,cnt,n+1){
		ans[L[u]]=ans[u]+cntb[R[u]]*ww[u];
		ans[R[u]]=ans[u]+cntb[L[u]]*ww[u];
	}
	for(auto it:qs[id]) res[it.se]=ans[it.fi];
}

pair<ll,int> wei[maxn];
bool vs[maxn];

void work()
{
	n=read(),m=read(),m1=read(),m2=read();
	For(i,1,n-1){
		eu[i]=read(),ev[i]=read(),ew[i]=read();
		w[i+n]=mx[i+n]=ew[i];
	}
	For(i,1,m){
		dl[i+n-1]=read(),eu[i+n-1]=read(),ev[i+n-1]=read(),ew[i+n-1]=read();
		w[i+n+n-1]=mx[i+n+n-1]=ew[i+n-1];
	}
	For(i,1,m1){
		int x=read(),y=read();
		per[x].pb(y);
	}
	For(i,1,m2){
		int x=read(),y=read();
		qs[x].pb(mkp(y,i));
	}
	
	tot=m+n-1;
	For(i,1,tot) ids[i]=i;
	sort(ids+1,ids+tot+1,[&](int x,int y){
		return ew[x]<ew[y];
	});
	
	For(i,1,tot){
		eu2[i]=eu[ids[i]];
		ev2[i]=ev[ids[i]];
		ew2[i]=ew[ids[i]];
		to[ids[i]]=i;
	}
	
	For(i,1,n-1) linke(i);
	cerr<<"QSQ\n";
	
	 flag=(eu[1]==1785);
	
	For(i,1,m) wei[i]=mkp(1ll*qs[i].size()*per[i].size(),i);
	sort(wei+1,wei+m+1);
	ll cur=0;
	int V=11000000;
	For(i,1,m){
		cur+=wei[i].fi;
		if(cur<=V) vs[wei[i].se]=1;
		else break;
	}
	
	int ss=0;
	For(i,1,m){
	//	cout<<"i: "<<i<<"\n";
		cute(dl[i+n-1]);
		linke(i+n-1);
	//	continue;
		if( vs[i] ){
		//	if(flag) continue;
			bru[i]=1;
			for(auto it:qs[i]){
				int u=it.fi,id=it.se;
				mkrt(u);
				for(int v:per[i]){
					acc(v);splay(v);
					res[id]+=mx[v];
				}
			}
		}
		else {
			++ss;
		//	if(flag) continue;
			solve(i);
		}
	}
	cerr<<"ss "<<ss<<"\n";
//	if(flag) cout<<"ss "<<ss<<"\n";
	For(i,1,m2) printf("%lld\n",res[i]);
}

signed main(){
//	freopen("data.in","r",stdin);
//	freopen("my.out","w",stdout);
	int T=1;
	while(T--)work();
	return 0;
}
/*
5 3 6 1
1 2 9
1 3 4
1 4 6
4 5 2
3 3 5 3
2 1 5 5
5 3 4 8
1 3
2 1
3 3
2 4
1 5
2 4
1 1

0 6 1
0 6 2
0 7 1 
0 7 3
0 8 1
0 8 4
0 9 4
0 9 5
1 8 1
1 8 4
*/