QOJ.ac
QOJ
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#900 | #580157 | #9371. Mountain Booking | Displace_ | Crysfly | Failed. | 2024-09-24 18:26:54 | 2024-09-24 18:26:55 |
Details
Extra Test:
Accepted
time: 5746ms
memory: 124020kb
input:
100000 199998 200000 200000 1 2 563292035 1 3 203860992 3 4 889346085 1 5 231433900 3 6 109229944 6 7 510295144 1 8 191098504 6 9 159028557 4 10 873322008 1 11 626997288 4 12 919355805 5 13 971070306 7 14 842448905 5 15 654728726 14 16 618071553 12 17 14377248 7 18 375429871 5 19 409831358 8 20 7847...
output:
109897847643 109898030797 109878840144 109765632229 109847724773 109925193044 109812192578 109901664640 109908915467 109755280997 109954575568 109884058971 109888777501 109695768511 109897965284 109814886991 109878840144 109885635838 109809662032 109887615851 109879348293 109855194980 109879333386 1...
result:
ok 200000 lines
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#580157 | #9371. Mountain Booking | Crysfly | AC ✓ | 5815ms | 134544kb | C++14 | 4.9kb | 2024-09-21 20:18:10 | 2024-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
*/