//#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
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 |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
7 ms |
888 KB |
Output is correct |
4 |
Correct |
7 ms |
888 KB |
Output is correct |
5 |
Correct |
7 ms |
888 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
25976 KB |
Output is correct |
2 |
Correct |
158 ms |
26872 KB |
Output is correct |
3 |
Correct |
174 ms |
29816 KB |
Output is correct |
4 |
Correct |
188 ms |
30072 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
162 ms |
25336 KB |
Output is correct |
7 |
Correct |
90 ms |
18296 KB |
Output is correct |
8 |
Correct |
165 ms |
26232 KB |
Output is correct |
9 |
Correct |
118 ms |
27896 KB |
Output is correct |
10 |
Correct |
116 ms |
27000 KB |
Output is correct |
11 |
Correct |
125 ms |
29304 KB |
Output is correct |
12 |
Correct |
132 ms |
29432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
760 KB |
Output is correct |
3 |
Correct |
7 ms |
888 KB |
Output is correct |
4 |
Correct |
7 ms |
888 KB |
Output is correct |
5 |
Correct |
7 ms |
888 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
760 KB |
Output is correct |
10 |
Correct |
164 ms |
25976 KB |
Output is correct |
11 |
Correct |
158 ms |
26872 KB |
Output is correct |
12 |
Correct |
174 ms |
29816 KB |
Output is correct |
13 |
Correct |
188 ms |
30072 KB |
Output is correct |
14 |
Correct |
5 ms |
504 KB |
Output is correct |
15 |
Correct |
162 ms |
25336 KB |
Output is correct |
16 |
Correct |
90 ms |
18296 KB |
Output is correct |
17 |
Correct |
165 ms |
26232 KB |
Output is correct |
18 |
Correct |
118 ms |
27896 KB |
Output is correct |
19 |
Correct |
116 ms |
27000 KB |
Output is correct |
20 |
Correct |
125 ms |
29304 KB |
Output is correct |
21 |
Correct |
132 ms |
29432 KB |
Output is correct |
22 |
Correct |
160 ms |
25976 KB |
Output is correct |
23 |
Correct |
133 ms |
23032 KB |
Output is correct |
24 |
Correct |
163 ms |
25592 KB |
Output is correct |
25 |
Correct |
6 ms |
632 KB |
Output is correct |
26 |
Correct |
57 ms |
11896 KB |
Output is correct |
27 |
Correct |
128 ms |
22008 KB |
Output is correct |
28 |
Correct |
106 ms |
24568 KB |
Output is correct |
29 |
Correct |
129 ms |
29432 KB |
Output is correct |
30 |
Correct |
121 ms |
29048 KB |
Output is correct |
31 |
Correct |
6 ms |
760 KB |
Output is correct |