QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#292677 | #2429. Conquer The World | Kevin5307 | AC ✓ | 6099ms | 159340kb | C++20 | 3.8kb | 2023-12-28 10:59:54 | 2023-12-28 10:59:55 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define int ll
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
vector<pii> G[300300];
int n;
int x[300300],y[300300];
int c[300300],f[300300];
int l[300300],r[300300];
int fval[300300];
namespace treap
{
#define ls(x) nd[x].ls
#define rs(x) nd[x].rs
#define siz(x) nd[x].siz
#define pri(x) nd[x].pri
#define tag(x) nd[x].tag
#define val(x) nd[x].val
mt19937_64 rnd(32183332);
struct node
{
int siz,val,tag;
ull pri;
int ls,rs;
node(){}
}nd[1001000];
set<int> pool;
void init()
{
for(int i=1;i<1001000;i++)
pool.insert(i);
}
void pushdown(int x)
{
tag(ls(x))+=tag(x);
tag(rs(x))+=tag(x);
val(x)+=tag(x);
tag(x)=0;
}
void pushup(int x)
{
pushdown(ls(x));
pushdown(rs(x));
siz(x)=siz(ls(x))+siz(rs(x))+1;
}
int merge(int x,int y)
{
if(!x||!y) return x+y;
pushdown(x);
pushdown(y);
if(pri(x)>pri(y))
{
rs(x)=merge(rs(x),y);
pushup(x);
return x;
}
else
{
ls(y)=merge(x,ls(y));
pushup(y);
return y;
}
}
void valSplit(int x,int v,int &a,int &b)
{
if(!x)
{
a=b=0;
return ;
}
pushdown(x);
if(val(x)<=v)
{
a=x;
valSplit(rs(x),v,rs(a),b);
pushup(a);
}
else
{
b=x;
valSplit(ls(x),v,a,ls(b));
pushup(b);
}
}
void sizSplit(int x,int s,int &a,int &b)
{
if(!x)
{
a=b=0;
return ;
}
pushdown(x);
if(siz(ls(x))>=s)
{
b=x;
sizSplit(ls(x),s,a,ls(b));
pushup(b);
}
else
{
a=x;
sizSplit(rs(x),s-siz(ls(x))-1,rs(a),b);
pushup(a);
}
}
int insert(int x,int val)
{
int a,b;
valSplit(x,val,a,b);
int tot=*pool.begin();
pool.erase(pool.begin());
val(tot)=val;
siz(tot)=1;
pri(tot)=rnd();
return merge(a,merge(tot,b));
}
void unfold(int x,vector<int> &vec)
{
if(!x) return ;
pushdown(x);
pool.insert(x);
vec.pb(val(x));
unfold(ls(x),vec);
unfold(rs(x),vec);
ls(x)=rs(x)=tag(x)=val(x)=siz(x)=pri(x)=0;
}
int unordered_merge(int x,int y)
{
if(siz(x)<siz(y))
swap(x,y);
vector<int> vec;
unfold(y,vec);
for(auto val:vec)
x=insert(x,val);
return x;
}
int change(int x,int len,int d)
{
int a,b;
sizSplit(x,len,a,b);
tag(a)-=d;
tag(b)+=d;
return merge(a,b);
}
pii front(int x)
{
int a,b;
sizSplit(x,1,a,b);
return mp(val(a),b);
}
}
int dfs(int u,int fa)
{
int ret=0;
for(auto pr:G[u])
if(pr.first!=fa)
{
c[pr.first]=pr.second;
ret=treap::unordered_merge(ret,dfs(pr.first,u));
l[u]+=l[pr.first];
r[u]+=r[pr.first];
fval[u]+=fval[pr.first];
}
for(int i=0;i<x[u];i++)
ret=treap::insert(ret,0);
l[u]+=y[u]-x[u];
r[u]+=y[u];
fval[u]+=abs(l[u])*c[u];
ret=treap::change(ret,max(0ll,-l[u]),c[u]);
return ret;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
treap::init();
cin>>n;
for(int i=1;i<n;i++)
{
int u,v,c;
cin>>u>>v>>c;
G[u].pb(v,c);
G[v].pb(u,c);
}
for(int i=1;i<=n;i++)
cin>>x[i]>>y[i];
int rt=dfs(1,0);
assert(l[1]<=0&&r[1]>=0);
ll ans=fval[1];
for(int i=0;i<-l[1];i++)
{
pii pr=treap::front(rt);
ans+=pr.first;
rt=pr.second;
}
cout<<ans<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 222ms
memory: 57472kb
Test #2:
score: 0
Accepted
time: 224ms
memory: 57476kb
Test #3:
score: 0
Accepted
time: 221ms
memory: 57512kb
Test #4:
score: 0
Accepted
time: 214ms
memory: 57488kb
Test #5:
score: 0
Accepted
time: 213ms
memory: 57476kb
Test #6:
score: 0
Accepted
time: 210ms
memory: 57944kb
Test #7:
score: 0
Accepted
time: 213ms
memory: 57744kb
Test #8:
score: 0
Accepted
time: 213ms
memory: 58024kb
Test #9:
score: 0
Accepted
time: 1034ms
memory: 85684kb
Test #10:
score: 0
Accepted
time: 1289ms
memory: 101688kb
Test #11:
score: 0
Accepted
time: 987ms
memory: 104312kb
Test #12:
score: 0
Accepted
time: 208ms
memory: 57640kb
Test #13:
score: 0
Accepted
time: 201ms
memory: 57512kb
Test #14:
score: 0
Accepted
time: 200ms
memory: 57680kb
Test #15:
score: 0
Accepted
time: 235ms
memory: 58052kb
Test #16:
score: 0
Accepted
time: 227ms
memory: 57864kb
Test #17:
score: 0
Accepted
time: 229ms
memory: 58232kb
Test #18:
score: 0
Accepted
time: 378ms
memory: 61228kb
Test #19:
score: 0
Accepted
time: 888ms
memory: 72096kb
Test #20:
score: 0
Accepted
time: 2805ms
memory: 104552kb
Test #21:
score: 0
Accepted
time: 359ms
memory: 79380kb
Test #22:
score: 0
Accepted
time: 342ms
memory: 79004kb
Test #23:
score: 0
Accepted
time: 389ms
memory: 82020kb
Test #24:
score: 0
Accepted
time: 306ms
memory: 70992kb
Test #25:
score: 0
Accepted
time: 321ms
memory: 73292kb
Test #26:
score: 0
Accepted
time: 399ms
memory: 82444kb
Test #27:
score: 0
Accepted
time: 4038ms
memory: 116260kb
Test #28:
score: 0
Accepted
time: 2439ms
memory: 101764kb
Test #29:
score: 0
Accepted
time: 4394ms
memory: 128892kb
Test #30:
score: 0
Accepted
time: 5405ms
memory: 130852kb
Test #31:
score: 0
Accepted
time: 5965ms
memory: 130952kb
Test #32:
score: 0
Accepted
time: 6099ms
memory: 130856kb
Test #33:
score: 0
Accepted
time: 5991ms
memory: 130844kb
Test #34:
score: 0
Accepted
time: 5621ms
memory: 130880kb
Test #35:
score: 0
Accepted
time: 903ms
memory: 130868kb
Test #36:
score: 0
Accepted
time: 896ms
memory: 130820kb
Test #37:
score: 0
Accepted
time: 5648ms
memory: 130848kb
Test #38:
score: 0
Accepted
time: 1535ms
memory: 158412kb
Test #39:
score: 0
Accepted
time: 759ms
memory: 148284kb
Test #40:
score: 0
Accepted
time: 1834ms
memory: 127580kb
Test #41:
score: 0
Accepted
time: 843ms
memory: 133452kb
Test #42:
score: 0
Accepted
time: 875ms
memory: 134132kb
Test #43:
score: 0
Accepted
time: 882ms
memory: 135528kb
Test #44:
score: 0
Accepted
time: 954ms
memory: 142164kb
Test #45:
score: 0
Accepted
time: 855ms
memory: 140244kb
Test #46:
score: 0
Accepted
time: 207ms
memory: 57496kb
Test #47:
score: 0
Accepted
time: 736ms
memory: 104288kb
Test #48:
score: 0
Accepted
time: 547ms
memory: 104420kb
Test #49:
score: 0
Accepted
time: 4326ms
memory: 128964kb
Test #50:
score: 0
Accepted
time: 351ms
memory: 81964kb
Test #51:
score: 0
Accepted
time: 206ms
memory: 57480kb
Test #52:
score: 0
Accepted
time: 223ms
memory: 57484kb
Test #53:
score: 0
Accepted
time: 199ms
memory: 57508kb
Test #54:
score: 0
Accepted
time: 219ms
memory: 57488kb
Test #55:
score: 0
Accepted
time: 195ms
memory: 57456kb
Test #56:
score: 0
Accepted
time: 219ms
memory: 57496kb
Test #57:
score: 0
Accepted
time: 202ms
memory: 57604kb
Test #58:
score: 0
Accepted
time: 226ms
memory: 58680kb
Test #59:
score: 0
Accepted
time: 658ms
memory: 75820kb
Test #60:
score: 0
Accepted
time: 3479ms
memory: 113864kb
Test #61:
score: 0
Accepted
time: 3400ms
memory: 123492kb
Test #62:
score: 0
Accepted
time: 4350ms
memory: 128904kb
Test #63:
score: 0
Accepted
time: 658ms
memory: 127484kb
Test #64:
score: 0
Accepted
time: 658ms
memory: 127424kb
Test #65:
score: 0
Accepted
time: 643ms
memory: 127528kb
Test #66:
score: 0
Accepted
time: 1741ms
memory: 127492kb
Test #67:
score: 0
Accepted
time: 790ms
memory: 148312kb
Test #68:
score: 0
Accepted
time: 1213ms
memory: 157624kb
Test #69:
score: 0
Accepted
time: 794ms
memory: 159340kb
Test #70:
score: 0
Accepted
time: 778ms
memory: 158436kb