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 += newVal;
newNode->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), vertexCost(100001);
vector<tuple<ll, ll, ll, ll, ll>> addOrder;
vl transPos(100001, -1), depth(100001);
vvl bl(100001, vl(25, 0));
void dfs(int u, int last = -1, int d = 0){
fo(i, vertexCost[u].size()) addOrder.pb({vertexCost[u][i].F, vertexCost[u][i].S, u, i, addOrder.size()});
transPos[u] = addOrder.size()-1;
depth[u] = d;
for(auto &[v, id] : adj[u]){
if(v == last) continue;
bl[v][0] = u;
vertexCost[v] = edgeCost[id];
dfs(v, u, d+1);
}
fo(i, vertexCost[u].size()) addOrder.pb({vertexCost[u][i].F, vertexCost[u][i].S, u, i, addOrder.size()});
}
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));
}
dfs(0);
sort(all(addOrder));
vector<Node*> history = {new Node(0, addOrder.size())};
history[0]->construct();
fo(i, addOrder.size()){
tie(silver, gold, from, skrap, pos) = addOrder[i];
history.pb(history[history.size()-1]->update(pos, pos+1, silver, gold));
i++;
tie(silver, gold, from, skrap, pos) = addOrder[i];
history[history.size()-1] = history[history.size()-1]->update(pos, 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;
if(history[mid]->query(transPos[a]+1, transPos[to]+1)+history[mid]->query(transPos[a]+1, transPos[from]+1)>silver) hi = mid;
else lo = mid;
}
int val = history[history.size()-1]->query2(transPos[a]+1, transPos[to]+1)+history[history.size()-1]->query2(transPos[a]+1, transPos[from]+1)-(history[lo]->query2(transPos[a]+1, transPos[to]+1)+history[lo]->query2(transPos[a]+1, transPos[from]+1));
cout << max(-1ll, gold-val) << "\n";
}
return 0;
}
# | 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... |