이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"
const double PI=3.141592653589;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
int n,m,q;
vector<vector<int>>adj, up,tax;
vector<int>par,dist;
void comp(int u, int p){
for(int v : adj[u]){
if(v==p)continue;
par[v] = u;
dist[v] = dist[u]+1;
up[v][0] = u;
for(int j = 1;j<=20;++j)up[v][j] = up[up[v][j-1]][j-1];
comp(v,u);
}
}
int lca(int u, int v){
if(dist[v] > dist[u])swap(v,u);
int k = dist[u]-dist[v];
for(int j = 0;j<=20;++j){
if(k&(1ll<<j))u = up[u][j];
}
if(u==v)return u;
for(int j = 20;j>=0;--j){
if(up[u][j] != up[v][j])u = up[u][j], v = up[v][j];
}
return up[v][0];
}
struct mergesorttree{
int sz = 1;
vector<vector<int>>seg,pref;
void init(int n){
while(sz < n)sz*=2;
seg.resize(sz*2);
pref.resize(sz*2);
}
void build(int x, int lx, int rx, vector<int>a){
if(rx-lx == 1){
if(lx < (int)(a.size()))seg[x].pb(a[lx]);
return;
}
int m = (lx+rx)/2;
build(2*x+1,lx,m,a);
build(2*x+2,m,rx,a);
merge(all(seg[2*x+1]),all(seg[2*x+2]), back_inserter(seg[x]));
}
void build(vector<int>a){
build(0,0,sz,a);
for(int i = 0;i<seg.size();++i){
if(seg[i].empty())continue;
int p = 0;
for(int j = 0;j<seg[i].size();++j)p+=seg[i][j],pref[i].pb(p);
}
}
pair<int,int>merge2(pair<int,int>a, pair<int,int>b){
return {a.first+b.first,a.second+b.second};
}
pair<int,int> sol(int x, int lx, int rx, int l, int r, int v){
if(lx >= l && rx <= r){
int val = upper_bound(all(seg[x]),v)-seg[x].begin()-1;
if(val < 0)return {0,0};
return {pref[x][val],val+1};
}
if(rx <= l || lx >= r)return {0,0};
int m = (lx+rx)/2;
return merge2(sol(2*x+1,lx,m,l,r,v),sol(2*x+2,m,rx,l,r,v));
}
pair<int,int> sol(int l, int r, int v){
// sum of elements <= v
return sol(0,0,sz,l,r,v);
}
};
void solve()
{
cin >> n >> m >> q;
adj.resize(n+1);
up = vector<vector<int>>(n+1, vector<int>(21));
vector<pair<int,int>>edges = {{0,0}};
dist.resize(n+1);
tax.resize(n+1);
par.resize(n+1);
for(int i = 1;i<n;++i){
int u,v;
cin >> u >> v;
edges.pb({u,v});
adj[u].pb(v);
adj[v].pb(u);
}
comp(1,-1);
for(int i = 1;i<=m;++i){
int p,c;
cin >> p >> c;
int u = edges[p].first, v = edges[p].second;
if(par[u] == v)swap(v,u);
// par[v] = u
tax[v].pb(c);
}
vector<int>arr={0};
vector<int>start(n+1);
int p = 1;
for(int i = 1;i<=n;++i){
start[i] = p;
for(auto x : tax[i])arr.pb(x);
p+=tax[i].size();
}
mergesorttree st;
st.init(arr.size()+1);
st.build(arr);
while(q--){
int u,v,g,s;
cin >> u >> v >> g >> s;
if(u > v)swap(v,u);
int l = 0, r = 1e18;
int res = -1;
while(l <= r){
int m = (l+r)/2;
int beg = start[u+1],ee = start[v]+tax[v].size();
pair<int,int> sum = st.sol(beg,ee,m);
if(sum.first <= s){
int tot = ee-beg;
tot-=sum.second;
int ans = g-tot;
if(ans < 0)ans = -1;
res = max(res,ans);
l = m+1;
}else r = m-1;
}
cout << res << endl;
}
}
/*
7 9 1
1 2
2 3
3 4
4 5
5 6
6 7
1 3
1 4
2 1
2 2
2 6
4 4
5 1
6 8
6 3
2 5 2 7
*/
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T=1;
for(int i = 1;i<=T;++i)
{
// cout << "Case #" << i << ": ";
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp: In member function 'void mergesorttree::build(std::vector<long long int>)':
currencies.cpp:89:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i = 0;i<seg.size();++i){
| ~^~~~~~~~~~~
currencies.cpp:92:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int j = 0;j<seg[i].size();++j)p+=seg[i][j],pref[i].pb(p);
| ~^~~~~~~~~~~~~~
# | 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... |