This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#define NDEBUG
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define tiii tuple<int, int, int>
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) ((a) / (b) + !!((a) % (b)))
//#define TEST
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
using namespace __gnu_pbds;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
vector<vector<pair<int, pll>>> g;
vector<pll> up;
vector<vector<int>> anc;
vector<int> in, out;
int ts = 1;
void dfs(int now, int p){
in[now] = ts++;
anc[now][0] = p;
for(auto i : g[now]){
if(i.F == p) continue;
up[i.F] = i.S;
dfs(i.F, now);
}
out[now] = ts++;
}
bool isAnc(int a, int b){
return in[a] <= in[b] && out[a] >= out[b];
}
int lca(int a, int b){
if(isAnc(a, b)) return a;
if(isAnc(b, a)) return b;
for(int i = 19; i >= 0; i--){
if(!isAnc(anc[a][i], b)) a = anc[a][i];
}
return anc[a][0];
}
vector<ll> BIT;
int lowbit(int x){
return x & -x;
}
void modify(int x, int v){
for(; x < BIT.size(); x += lowbit(x)) BIT[x] += v;
}
int query(int x){
ll ans = 0;
for(; x > 0; x -= lowbit(x)) ans += BIT[x];
return ans;
}
int main(){
StarBurstStream
int n;
cin >> n;
g.resize(n + 1);
for(int i = 0; i < n - 1; i++){
int a, b, c1, c2;
cin >> a >> b >> c1 >> c2;
g[a].eb(mp(b, mp(c1, c2)));
g[b].eb(mp(a, mp(c1, c2)));
}
anc.resize(n + 1, vector<int>(20));
in.resize(n + 1);
out.resize(n + 1);
up.resize(n + 1);
dfs(1, 1);
for(int i = 1; i < 20; i++){
for(int j = 1; j <= n; j++){
anc[j][i] = anc[anc[j][i - 1]][i - 1];
}
}
BIT.resize(ts + 5);
for(int i = 1; i < n; i++){
int j = i + 1;
int l = lca(i, j);
// cerr << i << " " << j << " " << l << "\n";
modify(in[l] + 1, 1);
modify(in[i] + 1, -1);
modify(in[l] + 1, 1);
modify(in[j] + 1, -1);
// for(int j = 1; j < ts; j++) cerr << query(j) << " ";
// cerr << "\n";
}
// printv(in, cerr);
// printv(out, cerr);
ll ans = 0;
for(int i = 1; i <= n; i++){
ll tmp = query(in[i]) - query(out[i]);
// cerr << tmp << " " << query(in[i]) << " " << query(out[i]) <<"\n";
ans += min(up[i].F * tmp, up[i].S);
}
// cerr << "\n";
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
putovanje.cpp: In function 'void modify(int, int)':
putovanje.cpp:85:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; x < BIT.size(); x += lowbit(x)) BIT[x] += v;
~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |