제출 #966816

#제출 시각아이디문제언어결과실행 시간메모리
966816mychecksedadTwo Currencies (JOI23_currencies)C++17
100 / 100
698 ms192196 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' const int N = 1e6+100, M = 1e5+10, K = 20, MX = 30; struct Node{ Node *L, *R; ll sum; int num; Node(){ L = R = nullptr; sum = num = 0; } Node(ll x){ L = R = nullptr; sum = x; num = 1; } Node(Node *l, Node *r){ L = l, R = r; sum = L->sum + R->sum; num = L->num + R->num; } void extend(){ if(L == nullptr) L = new Node(); if(R == nullptr) R = new Node(); } }; int n, mi, q, s[N], tin[N], tout[N], up[N][K], heavy[N], dep[N], par[N], ESZ[N], ttin[N], toutt[N], timerr = 0; vector<pair<int, int>> g[N], euler; vector<int> E[N]; vector<Node*> roots; Node* build(int l, int r){ if(l == r){ return new Node(); } int m = l+r>>1; return new Node(build(l, m), build(m+1, r)); } Node *update(Node *v, int l, int r, int p, ll val){ if(l == r){ return new Node(val); } int m = l+r>>1; if(p <= m){ return new Node(update(v->L, l, m, p, val), v->R); } return new Node(v->L, update(v->R, m+1, r, p, val)); } int f(int l, int r, vector<pair<Node*, Node*>> &TV, ll rsilver){ if(l == r){ ll val = 0; int nm = 0; for(auto &pt: TV){ val += pt.second->sum - pt.first->sum; nm += pt.second->num - pt.first->num; } if(val <= rsilver) return nm; return 0; } int m = l+r>>1; ll val = 0; int nm = 0; for(auto &pt: TV){ val += pt.second->L->sum - pt.first->L->sum; nm += pt.second->L->num - pt.first->L->num; } if(val <= rsilver){ for(auto &pt: TV) pt.first = pt.first->R, pt.second = pt.second->R; return nm + f(m+1, r, TV, rsilver - val); } for(auto &pt: TV) pt.first = pt.first->L, pt.second = pt.second->L; return f(l, m, TV, rsilver); } void dfs(int v, int p){ s[v] = 1; par[v] = p; dep[v] = dep[p] + 1; for(auto &U: g[v]){ int u = U.first; if(u != p){ dfs(u, v); up[u][0] = v; s[v] += s[u]; if(s[u] > s[g[v][0].first] || g[v][0].first == p) swap(U, g[v][0]); } } } void dfs2(int v, int p, int &timer){ tin[v] = timer - 1; ttin[v] = timerr++; for(auto &U: g[v]){ int u = U.first; if(u != p){ if(u == g[v][0].first) heavy[u] = heavy[v]; else heavy[u] = u; timer += E[U.second].size(); ESZ[u] = E[U.second].size(); for(auto x: E[U.second]) euler.pb({x, euler.size() + 1}); dfs2(u, v, timer); } } tout[v] = timer - 1; toutt[v] = timerr++; } bool is_parent(int u, int v){ return ttin[u] <= ttin[v] && toutt[v] <= toutt[u]; } int _lca(int u, int v){ if(is_parent(u, v)) return u; if(is_parent(v, u)) return v; for(int j = K-1; j >= 0 ; --j){ if(!is_parent(up[u][j], v)) u = up[u][j]; } return up[u][0]; } void solve(){ cin >> n >> mi >> q; for(int i = 0; i < n - 1; ++i){ int u, v; cin >> u >> v; g[u].pb({v, i+1}); g[v].pb({u, i+1}); } for(int i = 0; i < mi; ++i){ int p, c; cin >> p >> c; E[p].pb(c); } dep[1] = 1; dfs(1, 1); int tm = 1; heavy[1] = 1; dfs2(1, 1, tm); up[1][0] = 1; for(int j = 1; j < K; ++j) for(int i = 1; i <= n; ++i) up[i][j] = up[up[i][j - 1]][j - 1]; vector<pair<int, int>> X = euler; vector<int> pos(mi+1); sort(all(X)); for(int i = 0; i < mi; ++i) pos[X[i].second] = i + 1; roots.pb(build(1, mi)); for(int i = 1; i <= mi; ++i){ roots.pb(update(roots.back(), 1, mi, pos[i], euler[i - 1].first)); } // for(auto x: X){ // cout << x.first << ' ' << x.second << '\n'; // } // for(int i = 1; i <= n; ++i) cout << tin[i] << ' ' << tout[i] << '\n'; for(int i = 0; i < q; ++i){ int s, t; ll x, y; cin >> s >> t >> x >> y; int lca = _lca(s, t); vector<pair<int, int>> T; if(lca != s){ int v = s; while(v != lca){ int next; if(dep[heavy[v]] <= dep[lca]){ next = lca; if(tin[next] + 1 <= tin[v]) T.pb({tin[next] + 1, tin[v]}); break; } else{ next = heavy[v]; int L = tin[heavy[v]] - ESZ[heavy[v]] + 1; if(L <= tin[v]) T.pb({L, tin[v]}); } v = par[next]; } } if(lca != t){ int v = t; while(v != lca){ int next; if(dep[heavy[v]] <= dep[lca]){ next = lca; if(tin[next] + 1 <= tin[v]) T.pb({tin[next] + 1, tin[v]}); break; } else{ next = heavy[v]; int L = tin[heavy[v]] - ESZ[heavy[v]] + 1; if(L <= tin[v]) T.pb({L, tin[v]}); } v = par[next]; } } // cout << "q:"; // cout << s << ' ' << t << ' ' << lca << '\n'; // for(auto x:T) cout << x.first << ' ' << x.second << '\n'; // en;en; int tot_gold = 0; vector<pair<Node*, Node*>> TV; for(auto x: T){ TV.pb({roots[x.first - 1], roots[x.second]}); tot_gold += x.second - x.first + 1; } int rgold = x - (tot_gold - f(1, mi, TV, y)); if(rgold < 0) cout << -1 << '\n'; else cout << rgold << '\n'; } } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); en; } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

currencies.cpp: In function 'Node* build(int, int)':
currencies.cpp:46:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |   int m = l+r>>1;
      |           ~^~
currencies.cpp: In function 'Node* update(Node*, int, int, int, long long int)':
currencies.cpp:54:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   54 |   int m = l+r>>1;
      |           ~^~
currencies.cpp: In function 'int f(int, int, std::vector<std::pair<Node*, Node*> >&, long long int)':
currencies.cpp:72:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   72 |   int m = l+r>>1;
      |           ~^~
currencies.cpp: In function 'int main()':
currencies.cpp:236:15: warning: unused variable 'aa' [-Wunused-variable]
  236 |   int tt = 1, aa;
      |               ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...