This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#undef _GLIBCXX_DEBUG // disable run-time bound checking, etc
#pragma GCC optimize("Ofast,inline") // Ofast = O3,fast-math,allow-store-data-races,no-protect-parens
#pragma GCC optimize ("unroll-loops")
// #pragma GCC target("bmi,bmi2,lzcnt,popcnt") // bit manipulation
// #pragma GCC target("movbe") // byte swap
// #pragma GCC target("aes,pclmul,rdrnd") // encryption
// #pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") // SIMD
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
// template<class T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
// template<class T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
#define INF (ll)1e9+7
#define fo(i, n) for(ll i=0;i<((ll)n);i++)
#define deb(x) cout << #x << " = " << x << endl;
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LSOne(S) ((S) & (-S))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
inline int readint(){ int v = 0; char c; while((c = getchar()) != EOF && c != ' ' && c != '\n'){ v *= 10; v += c - '0'; } return v; }
inline int readintsigned() { int v = 0; int sign = 1; char c = getchar(); if (c == '-') { sign = -1; } else { v += c - '0'; } while ((c = getchar()) != EOF && c != ' ' && c != '\n') { v *= 10; v += c - '0'; } return v * sign; }
inline string readstring() { string s; char c; while ((c = getchar()) != EOF && c != '\n' && c != ' ') { s.push_back(c); } return s; }
template <class result_t=std::chrono::milliseconds,class clock_t=std::chrono::steady_clock,class duration_t = std::chrono::milliseconds>
auto since(std::chrono::time_point<clock_t, duration_t> const& start){return std::chrono::duration_cast<result_t>(clock_t::now() - start);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vpii> vvpii;
typedef vector<vpl> vvpl;
struct Node{
Node *lnode, *rnode;
ll l, r, val = 0, val2 = 0;
Node(int l, int r) : l(l), r(r){}
void construct(){
if(r-l > 1){
int mid = (r+l) / 2;
lnode = new Node(l, mid);
rnode = new Node(mid, r);
lnode->construct();
rnode->construct();
}
}
Node* update(int lo, int hi, ll newVal, ll newVal2){
if(r <= lo || hi <= l) return 0;
if(r <= hi && lo <= l){
Node *newNode = new Node(l, r);
newNode->val = val+newVal;
newNode->val2 = val2+newVal2;
return newNode;
}
Node *current = new Node(l, r);
Node *next = lnode->update(lo, hi, newVal, newVal2);
if(next){
current->lnode = next;
current->rnode = rnode;
}else{
current->lnode = lnode;
current->rnode = rnode->update(lo, hi, newVal, newVal2);
}
current->val = current->lnode->val+current->rnode->val;
current->val2 = current->lnode->val2+current->rnode->val2;
return current;
}
ll query(int lo, int hi){
if(r <= lo || hi <= l) return 0;
if(r <= hi && lo <= l) return val;
return rnode->query(lo, hi)+lnode->query(lo, hi);
}
ll query2(int lo, int hi){
if(r <= lo || hi <= l) return 0;
if(r <= hi && lo <= l) return val2;
return rnode->query2(lo, hi)+lnode->query2(lo, hi);
}
};
vvpl adj(100001), edgeCost(100001);
vector<tuple<ll, ll, ll>> addOrder;
vl depth(100001);
vvl bl(100001, vl(25, 0));
// heavy light decomp
vl heavychild(100001, 0), heavyparent(100001, 0), toSt, heavyConvert(100001, 0);
int heavydfs(int u, int last = -1, int d = 0){
pl ans = {0, -1};
depth[u] = d;
for(auto &[v, id] : adj[u]){
if(v == last) continue;
bl[v][0] = u;
ans = max(ans, {heavydfs(v, u, d+1)+1, v});
fo(i, edgeCost[id].size()) addOrder.pb({edgeCost[id][i].F, edgeCost[id][i].S, v});
}
heavychild[u] = ans.S;
return ans.F;
}
void heavydfs2(int u, int last = -1, int hvPar = 0){
heavyConvert[u] = toSt.size();
heavyparent[u] = hvPar;
toSt.pb(u);
if(heavychild[u] != -1) heavydfs2(heavychild[u], u, hvPar);
for(auto &[v, id] : adj[u]){
if(v == last || v == heavychild[u]) continue;
heavydfs2(v, u, v);
}
}
int main() {
// cout << fixed << setprecision(20);
// auto start = std::chrono::steady_clock::now(); // since(start).count()
cin.tie(0)->sync_with_stdio(0);
ll n, m, q, from, to, pos, cost, silver, gold, skrap;
cin >> n >> m >> q;
fo(i, n-1){
cin >> from >> to;
adj[--from].pb({--to, i});
adj[to].pb({from, i});
}
fo(i, m){
cin >> pos >> cost;
edgeCost[pos-1].pb(mp(cost, 1));
}
heavydfs(0);
heavydfs2(0);
sort(all(addOrder));
vector<Node*> history = {new Node(0, n)};
history[0]->construct();
fo(i, addOrder.size()){
tie(silver, gold, pos) = addOrder[i];
history.pb(history[history.size()-1]->update(heavyConvert[pos], heavyConvert[pos]+1, silver, gold));
}
fo(i, 22){
fo(j, n){
bl[j][i+1] = bl[bl[j][i]][i];
}
}
while(q--){
cin >> from >> to >> gold >> silver;
from--;to--;
int a = from, b = to;
if(depth[a] < depth[b]) swap(a,b);
for(int m = depth[a] - depth[b]; m; m-=LSOne(m)){
a = bl[a][__builtin_ctz(LSOne(m))];
}
for(int i = 20; i>=0; i--){
if(bl[a][i] == bl[b][i]) continue;
a = bl[a][i];
b = bl[b][i];
}
if(a != b) a = bl[a][0];
int lo = 0;
int hi = history.size();
while(hi-lo>1){
int mid = (hi+lo) / 2;
ll sum = 0;
int temp = to;
fo(i, 2){
while(a != temp){
if(depth[a] < depth[heavyparent[temp]]){
sum += history[mid]->query(heavyConvert[heavyparent[temp]], heavyConvert[temp]+1);
temp = bl[heavyparent[temp]][0];
}else{
sum += history[mid]->query(heavyConvert[temp]-depth[temp]+depth[a]+1, heavyConvert[temp]+1);
break;
}
}
temp = from;
}
if(sum>silver) hi = mid;
else lo = mid;
}
ll sum = 0;
int temp = to;
fo(i, 2){
while(a != temp){
if(depth[a] < depth[heavyparent[temp]]){
sum += history[history.size()-1]->query2(heavyConvert[heavyparent[temp]], heavyConvert[temp]+1) - history[lo]->query2(heavyConvert[heavyparent[temp]], heavyConvert[temp]+1);
temp = bl[heavyparent[temp]][0];
}else{
sum += history[history.size()-1]->query2(heavyConvert[temp]-depth[temp]+depth[a]+1, heavyConvert[temp]+1) - history[lo]->query2(heavyConvert[temp]-depth[temp]+depth[a]+1, heavyConvert[temp]+1);
break;
}
}
temp = from;
}
cout << max(-1ll, gold-sum) << "\n";
}
return 0;
}
Compilation message (stderr)
currencies.cpp: In function 'int main()':
currencies.cpp:135:52: warning: unused variable 'skrap' [-Wunused-variable]
135 | ll n, m, q, from, to, pos, cost, silver, gold, skrap;
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |