Submission #769383

#TimeUsernameProblemLanguageResultExecution timeMemory
769383ArturgoTwo Currencies (JOI23_currencies)C++14
100 / 100
4859 ms42500 KiB
#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> events;

void dfs(int u, int parent = -1) {
	posSommet[u] = events.size();
	
	for(pair<int, int> edge : voisins[u]) {
		if(edge.first == parent) continue;
		
		for(int prix : prixs[edge.second])
			events.push_back(prix);
		
		dfs(edge.first, u);
		
		for(int prix : prixs[edge.second])
			events.push_back(prix);
	}
}

struct Req {
	int batch;
	int id, deb, fin, gold, silver;
};

bool operator < (const Req& a, const Req& b) {
	if(a.batch != b.batch) return a.batch < b.batch;
	if(a.fin != b.fin) return a.fin < b.fin;
	return a.deb < b.deb;
}

int sommeSous[(1 << 20)];
int nbSous[(1 << 20)];

void ajoute(int pos, int val) {
	pos += (1 << 19);
	
	while(pos != 0) {
		sommeSous[pos] += val;
		nbSous[pos] += (val > 0)?1:-1;
		pos /= 2;
	}
}

int gold_keep(int pos, int val) {
	if(pos >= (1 << 19)) {
		return 0;
	}
	
	if(sommeSous[2 * pos] <= val) 
		return gold_keep(2 * pos + 1, val - sommeSous[2 * pos]) + nbSous[2 * pos];
	return gold_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 deb, fin;
		cin >> deb >> fin;
		deb--; fin--;
		voisins[deb].push_back({fin, iRoute});
		voisins[fin].push_back({deb, 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(events.size());
	
	vector<Req> reqs;
	for(int iReq = 0;iReq < nbReqs;iReq++) {
		int deb, fin, gold, silver;
		cin >> deb >> fin >> gold >> silver;
		deb--; fin--;
		
		deb = posSommet[deb];
		fin = posSommet[fin];
		if(deb > fin) swap(deb, fin);
		
		reqs.push_back({deb / SQR, iReq, deb, fin, gold, silver});
	}
	
	sort(reqs.begin(), reqs.end());
	
	vector<int> reponses(reqs.size(), -1);
	
	int d = 0, f = 0;
	for(Req r : reqs) {
		while(d < r.deb) {
			swp(events[d]);
			d++;
		}
		
		while(r.deb < d) {
			d--;
			swp(events[d]);
		}
		
		while(f < r.fin) {
			swp(events[f]);
			f++;
		}
		
		while(r.fin < f) {
			f--;
			swp(events[f]);
		}
		
		int gk = gold_keep(1, r.silver);
		
		if(nbSous[1] > r.gold + gk) reponses[r.id] = -1;
		else reponses[r.id] = r.gold + gk - nbSous[1];
	}
	
	for(int iReq = 0;iReq < nbReqs;iReq++) {
		cout << reponses[iReq] << "\n";		
	}
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...