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>
#define int long long
using namespace std;
vector<int> prixs[100 * 1000 + 42];
vector<pair<int, int>> voisins[100 * 1000 + 42];
vector<tuple<int, int, int>> index_to_val;
int posSommet[100 * 1000 + 42];
vector<int> valRag;
void dfs(int u, int parent = -1) {
posSommet[u] = valRag.size();
for(pair<int, int> edge : voisins[u]) {
if(edge.first == parent) continue;
for(int prix : prixs[edge.second])
valRag.push_back(prix);
dfs(edge.first, u);
for(int prix : prixs[edge.second])
valRag.push_back(prix);
}
}
struct Req {
int batch;
int id, x, y, z, w;
};
bool operator < (const Req& a, const Req& b) {
if(a.batch != b.batch) return a.batch < b.batch;
if(a.y != b.y) return a.y < b.y;
return a.x < b.x;
}
int sommeSous[(1 << 20)];
int node[(1 << 20)];
void ajoute(int pos, int val) {
pos += (1 << 19);
while(pos != 0) {
sommeSous[pos] += val;
node[pos] += (val > 0)?1:-1;
pos /= 2;
}
}
int z_keep(int pos, int val) {
if(pos >= (1 << 19)) {
return 0;
}
if(sommeSous[2 * pos] <= val)
return z_keep(2 * pos + 1, val - sommeSous[2 * pos]) + node[2 * pos];
return z_keep(2 * pos, val);
}
bool presents[100 * 1000 + 42];
void swp(int prix) {
if(!presents[prix]) {
ajoute(prix, get<0>(index_to_val[prix]));
}
else {
ajoute(prix, -get<0>(index_to_val[prix]));
}
presents[prix] = !presents[prix];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int nbSommets, nbChecks, nbReqs;
cin >> nbSommets >> nbChecks >> nbReqs;
for(int iRoute = 0;iRoute < nbSommets - 1;iRoute++) {
int x, y;
cin >> x >> y;
x--; y--;
voisins[x].push_back({y, iRoute});
voisins[y].push_back({x, iRoute});
}
for(int iCheck = 0;iCheck < nbChecks;iCheck++) {
int route, prix;
cin >> route >> prix;
route--;
index_to_val.push_back({prix, route, prixs[route].size()});
prixs[route].push_back(prix);
}
// Réindexation
index_to_val.push_back({0, -1, -1});
sort(index_to_val.begin(), index_to_val.end());
for(int iTuple = 1;iTuple < (int)index_to_val.size();iTuple++) {
tuple<int, int, int> t = index_to_val[iTuple];
prixs[get<1>(t)][get<2>(t)] = iTuple;
}
// Mo sur arbre
dfs(0);
const int SQR = sqrt(valRag.size());
vector<Req> reqs;
for(int iReq = 0;iReq < nbReqs;iReq++) {
int x, y, z, w;
cin >> x >> y >> z >> w;
x--; y--;
x = posSommet[x];
y = posSommet[y];
if(x > y) swap(x, y);
reqs.push_back({x / SQR, iReq, x, y, z, w});
}
sort(reqs.begin(), reqs.end());
vector<int> reponses(reqs.size(), 0);
int d = 0, f = 0;
for(Req r : reqs) {
while(d < r.x) {
swp(valRag[d]);
d++;
}
while(r.x < d) {
d--;
swp(valRag[d]);
}
while(f < r.y) {
swp(valRag[f]);
f++;
}
while(r.y < f) {
f--;
swp(valRag[f]);
}
cout << d << " " << f << endl;
int gk = z_keep(1, r.w);
if(node[1] > r.z + gk) reponses[r.id] = -1;
else reponses[r.id] = r.z + gk - node[1];
}
for(int iReq = 0;iReq < nbReqs;iReq++) {
cout << reponses[iReq] << "\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... |