Submission #585297

# Submission time Handle Problem Language Result Execution time Memory
585297 2022-06-28T14:50:50 Z algorithm16 Fountain (eJOI20_fountain) C++14
100 / 100
643 ms 25848 KB
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const int maxn = 1e5 + 7;

typedef pair <int, int> pii;

int n, q;
int c[maxn], d[maxn], v[maxn], r[maxn], par[maxn][21], ps[maxn];
set <pii> s;
vector <int> sus[maxn];

void pres (int x, int prf) {
	ps[x] = prf + c[x];
	if (sus[x].size() == 0) {
		return;
	}
	for (auto it : sus[x]) {
		pres(it, prf + c[x]);
	}
}

int main () {
	cin >> n >> q;
	
	for (int i = 1; i <= n; i++) {
		cin >> d[i] >> c[i];
		auto it = s.lower_bound({d[i], 0});
		if (it == s.begin()) {
			s.insert({d[i], i});
			continue;
		}
		do {
			--it;
			pii tr = *it;
			par[tr.S][0] = i;
			sus[i].push_back(tr.S);
			it = s.erase(it);
		} while (it != s.begin());
		s.insert({d[i], i});
	}
	
	auto it = s.begin();
	while (!s.empty()) {
		pii tr = *it;
		sus[0].push_back(tr.S);
		it = s.erase(it);
	}
	for (int k = 1; k <= 20; k++) {
		for (int i = 1; i <= n; i++) {
			par[i][k] = par[par[i][k-1]][k-1];
		}
	}
	
	pres(0, 0);
	
	/*for (int i = 0; i <= n; i++) {
		for (int k = 0; k < 4; k++)
			cout << par[i][k] << " \n"[k==3];
	}*/
	
	for (int i = 0; i < q; i++) {
		cin >> r[i] >> v[i];
		int tr = r[i];
		if (v[i] > ps[tr]) {
			cout << "0\n";
			continue;
		}
		for (int k = 20; k >= 0; k--) {
			int cur = ps[tr] - ps[par[tr][k]];
			//cout << "K: " << k << ", " << tr << " " << cur << " " << ps[tr] << " " << par[tr][k] << "\n";
			if (v[i] > cur) {
				tr = par[tr][k];
				v[i] -= cur;
			}
		}
		cout << tr << "\n";
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 4 ms 2672 KB Output is correct
3 Correct 4 ms 2772 KB Output is correct
4 Correct 5 ms 2772 KB Output is correct
5 Correct 8 ms 2900 KB Output is correct
6 Correct 6 ms 2772 KB Output is correct
7 Correct 7 ms 2756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 394 ms 20664 KB Output is correct
2 Correct 445 ms 22360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 4 ms 2672 KB Output is correct
3 Correct 4 ms 2772 KB Output is correct
4 Correct 5 ms 2772 KB Output is correct
5 Correct 8 ms 2900 KB Output is correct
6 Correct 6 ms 2772 KB Output is correct
7 Correct 7 ms 2756 KB Output is correct
8 Correct 394 ms 20664 KB Output is correct
9 Correct 445 ms 22360 KB Output is correct
10 Correct 6 ms 2772 KB Output is correct
11 Correct 237 ms 12532 KB Output is correct
12 Correct 643 ms 25848 KB Output is correct
13 Correct 534 ms 20544 KB Output is correct
14 Correct 451 ms 18964 KB Output is correct
15 Correct 474 ms 21836 KB Output is correct