QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#292674 | #2429. Conquer The World | Kevin5307 | RE | 453ms | 115924kb | C++20 | 3.8kb | 2023-12-28 10:58:32 | 2023-12-28 10:58:32 |
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<2002000;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);
}
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: 453ms
memory: 115924kb
Test #2:
score: -100
Runtime Error