제출 #1354471

#제출 시각아이디문제언어결과실행 시간메모리
1354471blackscreen1Regions (IOI09_regions)C++20
55 / 100
5919 ms196608 KiB
#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
#define ll int
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define pll pair<ll, ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
ll n, m, q, t;
ll blk = 200;
ll a[200005];
vector<ll> p[25005];
ll res[205][25005];
vector<ll> adj[200005];
pll cnt[25005], rng[25005];
ll rep[25005];
ll ur[25005];
void dfs1(ll nd, ll ti, ll cc) {
	res[rep[ti]][a[nd]] += cc;
	if (a[nd] == ti) cc++;
	for (auto it : adj[nd]) dfs1(it, ti, cc);
}
ll pre[200005], pos[200005], cid = 0;
void dfs2(ll nd) {
	pre[nd] = cid;
	cid++;
	for (auto it : adj[nd]) dfs2(it);
	pos[nd] = cid - 1;
}
struct node {
	ll s, e, m, v;
	node *l, *r;
	node (ll S, ll E, ll V = 0) {
		s = S, e = E, m = (S+E)>>1;
		v = V;
		l = r = nullptr;
	}
	node (ll S, ll E, node *L, node *R) {
		s = S, e = E, m = (S+E)>>1;
		v = 0;
		l = L, r = R;
		if (l != nullptr) v = l->v + r->v;
	}
};
node* build(ll S, ll E) {
	if (S != E) {
		ll m = (S + E) >> 1;
		return new node(S, E, build(S, m), build(m+1, E));
	}
	return new node(S, E);
}
node* upd(node *rt, ll X, ll V) {
	if (rt->s == rt->e) return new node(rt->s, rt->e, rt->v + V);
	if (X <= rt->m) return new node(rt->s, rt->e, upd(rt->l, X, V), rt->r);
	return new node(rt->s, rt->e, rt->l, upd(rt->r, X, V));
}
ll qu(node *rt, ll S, ll E) {
	if (rt->s == S && rt->e == E) return rt->v;
	if (E <= rt->m) return qu(rt->l, S, E);
	if (S > rt->m) return qu(rt->r, S, E);
	return qu(rt->l, S, rt->m) + qu(rt->r, rt->m + 1, E);
}
vector<node*> rts;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m >> q;
	blk = min(blk, m);
	memset(rep, -1, sizeof(rep));
	cin >> a[0];
	a[0]--;
	p[a[0]].push_back(0);
	iloop(1, n) {
		cin >> t >> a[i];
		t--, a[i]--;
		p[a[i]].push_back(i);
		adj[t].push_back(i);
	}
	iloop(0, m) cnt[i] = {p[i].size(), i};
	sort(cnt, cnt + m, greater<pll>());
	iloop(0, blk) {
		rep[cnt[i].second] = i;
		ur[i] = cnt[i].second;
		dfs1(0, cnt[i].second, 0);
	}
	dfs2(0);
	rts.push_back(build(0, n-1));
	iloop(0, m) {
		rng[i].first = rts.size() - 1;
		for (auto it : p[i]) rts.push_back(upd(rts.back(), pre[it], 1));
		rng[i].second = rts.size() - 1;
	}
	ll t1, t2;
	while (q--) {
		cin >> t1 >> t2;
		t1--, t2--;
		if (rep[t1] != -1) cout << res[rep[t1]][t2];
		else {
			ll cans = 0;
			for (auto it : p[t1]) cans += qu(rts[rng[t2].second], pre[it], pos[it]) - qu(rts[rng[t2].first], pre[it], pos[it]);
			cout << cans;
		} 
		cout << endl;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...