이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
bool remender(ll a , ll b){return a%b;}
//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);
const int N = 100002 , L = 20;
struct query {
int start,end;
ll gold , silver;
int l , r , ans , left, right , left1, right1;
};
vector<pair<int,int>> adj[N];
vector<pair<int,int>> check_points[N];
vector<int> tp;
int start[N],tin[N],tout[N];
vector<pair<ll,int>> arr;
vector<query> queries(N);
int up[N][L],in[N],out[N],timer;
void dfs(int node , int par){
start[node] = tp.size();
in[node] = timer++;
up[node][0] = par;
for(int i = 1; i < L; i++){
up[node][i] = up[up[node][i-1]][i-1];
}
for(auto i : adj[node]){
if(i.vf == par)continue;
for(auto j : check_points[i.vs]){
tin[j.vs] = tp.size();
tp.pb(j.vf);
}
dfs(i.vf , node);
for(auto j : check_points[i.vs]){
tout[j.vs] = tp.size();
tp.pb(j.vf);
}
}
out[node] = timer++;
}
bool is_lca(int x , int y){
return in[x] <= in[y] && out[x] >= out[y];
}
int find(int x , int y){
if(is_lca(x,y))return x;
if(is_lca(y,x))return y;
for(int i = L - 1; i >= 0; i--){
if(!is_lca(up[x][i],y))x = up[x][i];
}
return up[x][0];
}
struct Fen {
int n;
vector<ll> tree;
void init(int n1){
n = n1;
tree.assign(n + 1 , 0);
}
void set(int i , ll val){
i++;
while(i <= n){
tree[i] += val;
i += (i & -i);
}
}
ll sum(int i){
i++;
ll res = 0;
while(i > 0){
res += tree[i];
i -= (i & -i);
}
return res;
}
};
int n , m , q;
Fen ft,ft1;
void pbs(){
vector<int> v[m + 1];
ft.init(2*m + 1);
ft1.init(2*m + 1);
for(int i = 0; i < q; i++){
if(queries[i].l <= queries[i].r){
v[(queries[i].l + queries[i].r)/2].pb(i);
}
}
for(int i = 0; i < m; i++){
ft.set(tin[arr[i].vs],arr[i].vf);
ft.set(tout[arr[i].vs],-arr[i].vf);
}
for(int i : v[m]){
ll sum = ft.sum(queries[i].right) - ft.sum(queries[i].left-1);
sum += ft.sum(queries[i].right1) - ft.sum(queries[i].left1-1);
if(sum <= queries[i].silver){
queries[i].ans = 0;
queries[i].l = m+1;
}
else {
queries[i].r = m - 1;
}
}
for(int j = m - 1; j >= 0; j--){
ft.set(tin[arr[j].vs],-arr[j].vf);
ft.set(tout[arr[j].vs],arr[j].vf);
ft1.set(tin[arr[j].vs],1);
ft1.set(tout[arr[j].vs],-1);
for(int i : v[j]){
ll sum = ft.sum(queries[i].right) - ft.sum(queries[i].left-1);
sum += ft.sum(queries[i].right1) - ft.sum(queries[i].left1-1);
int cnt = ft1.sum(queries[i].right) - ft1.sum(queries[i].left-1);
cnt += ft1.sum(queries[i].right1) - ft1.sum(queries[i].left1-1);
if(sum <= queries[i].silver){
queries[i].ans = cnt;
queries[i].l = j+1;
}
else {
queries[i].r = j - 1;
}
}
}
}
void solve(){
cin >> n >> m >> q;
for(int i = 0; i < n - 1; i++){
int a , b;
cin >> a >> b;
adj[a].pb(mp(b,i+1));
adj[b].pb(mp(a,i+1));
}
for(int i = 0; i < m; i++){
ll p , c;
cin >> p >> c;
check_points[p].pb(mp(c,i));
arr.pb(mp(c,i));
}
timer = 0;
dfs(1,1);
sort(all(arr));
for(int i = 0; i < q; i++){
ll s , t , x , y;
cin >> s >> t >> x >> y;
int left , right , left1 , right1;
int lca = find(s,t);
if(lca == s){
left = start[s];
right = start[t]-1;
left1 = 0;
right1 = -1;
}
else if(lca == t){
left = start[t];
right = start[s]-1;
left1 = 0;
right1 = -1;
}
else {
left = start[lca];
right = start[s]-1;
left1 = start[lca];
right1 = start[t]-1;
}
//cout << left << ' ' << right << ' ' << left1 << ' ' << right1 << '\n';
queries[i] = {s,t,x,y,0,m,-1,left,right,left1,right1};
}
for(int i = 0; i < L; i++){
pbs();
}
for(int i = 0; i < q; i++){
if(queries[i].ans == -1 || queries[i].ans > queries[i].gold)cout << -1 << '\n';
else cout << queries[i].gold - queries[i].ans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp: In function 'void solve()':
currencies.cpp:195:17: warning: narrowing conversion of 's' from 'long long int' to 'int' [-Wnarrowing]
195 | queries[i] = {s,t,x,y,0,m,-1,left,right,left1,right1};
| ^
currencies.cpp:195:19: warning: narrowing conversion of 't' from 'long long int' to 'int' [-Wnarrowing]
195 | queries[i] = {s,t,x,y,0,m,-1,left,right,left1,right1};
| ^
# | 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... |