이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//####################
//TwoCurrencies
//####################
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
const int maxiN = 1000042;
const int LOG = 18;
vector < int > links[maxiN];
int depth[maxiN];
int up[maxiN][LOG];
void root(int node, int d = 0, int parent = -1) {
up[node][0] = parent;
depth[node] = d;
for (int v: links[node])
if (v != parent) {
root(v, d + 1, node);
}
}
int getK(int node, int K) {
for (int i = LOG - 1; i >= 0; i--) {
if ((K >> i) & 1) {
node = up[node][i];
}
}
return node;
}
int LCA(int a, int b) {
if (depth[b] > depth[a]) swap(a, b);
a = getK(a, depth[a] - depth[b]);
if (a == b) {
return a;
}
for (int l = LOG - 1; l >= 0; l--) {
if (up[a][l] != up[b][l]) {
a = up[a][l];
b = up[b][l];
}
}
return up[a][0];
}
pair<int,int> eulerseg[maxiN];
int actEuleur=-1;
void computeEuleurTour(int node,int parent = -1){
actEuleur++;
eulerseg[node].first = actEuleur;
for(int v:links[node])if(parent!=v){
computeEuleurTour(v,node);
}
actEuleur++;
eulerseg[node].second = actEuleur;
}
struct segtree{
vector<int> vals;
int leaf;
segtree(int n){
leaf = (1<<((int)ceil(log2(n))));
vals.resize(2*leaf);
fill(vals.begin(),vals.end(),0);
}
void _addRange(int a,int b,int val){
if(a==b){
vals[a]+=val;
}else if(a&1){
vals[a]+=val;
_addRange(a+1,b,val);
}else if(b%2==0){
vals[b]+=val;
_addRange(a,b-1,val);
}else{
_addRange(a/2,b/2,val);
}
}
void addRange(int a,int b,int val){
_addRange(a+leaf,b+leaf,val);
}
int getVal(int pos){
pos+=leaf;
int re = 0;
for(int p=pos;p>=1;p>>=1){
re+=vals[p];
}
return re;
}
};
signed main() {
fill(depth, depth + maxiN, -1);
int n, m, q;
cin >> n >> m >> q;
segtree counter(3*n);
vector < pair < int, int >> edges;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
edges.push_back({a,b});
links[a].push_back(b);
links[b].push_back(a);
}
root(0, 0, -1);
int C=-1;
computeEuleurTour(0);
for (int i = 0; i < m; i++) {
int p, c;
cin >> p >> c;
C = c;
p--;
auto a = edges[p].first;
auto b = edges[p].second;
if (depth[a] < depth[b]) swap(a, b);
counter.addRange(eulerseg[a].first,eulerseg[a].second,1);
}
//On calcule la tab de LCA
for (int l = 1; l < LOG; l++) {
for (int i = 0; i < n; i++) {
if (up[i][l - 1] == -1) {
up[i][l] = -1;
}
up[i][l] = up[up[i][l - 1]][l - 1];
}
}
for (int i = 0; i < q; i++) {
ll a, b, x, y;
cin >> a >> b >> x >> y;
a--;
b--;
int lca = LCA(a, b);
ll nbocc=0;
if(a==lca && b==lca){
nbocc=0;
}else if(a==lca){
nbocc=counter.getVal(eulerseg[b].first)-counter.getVal(eulerseg[a].first);
}else if(b==lca){
nbocc=counter.getVal(eulerseg[a].first)-counter.getVal(eulerseg[b].first);
}else{
nbocc=counter.getVal(eulerseg[b].first)+counter.getVal(eulerseg[a].first)-2*counter.getVal(eulerseg[lca].first);;
}
ll nb_silver = y/C;
ll g=0;
if(nb_silver>=nbocc){g=0;}
else{g=nbocc-nb_silver;}
if(g<=x){
cout<<x-g<<endl;
}else{
cout<<-1<<endl;
}
}
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... |