이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
template<class T> using indexed_set = tree<T, null_type, less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order //(k-indexed val with 0-indexing)
#define ook order_of_key //(num of vals in set that are strictly less)
#define ms(x, a) memset(x, a, sizeof(x))
#define siz(x) (int)x.size()
#define len(x) (int)x.length()
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vals, Args&&... values){
cout << vals << " = ";
string delim = "";
(...,(cout << delim << values, delim = ", "));
cout << endl;
}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
//===========================================
const int MAX = 2e5+5;
const int BLOCK = 500;
vector<int> adj[MAX], comp[MAX], occ[MAX];
int cmp[MAX], in[MAX], sub[MAX], res[400][25005], pp[MAX], curr;
int dfs(int v){
in[v] = ++curr;
occ[cmp[v]].pb(in[v]);
for (int u: adj[v]) sub[v] += dfs(u);
return (sub[v] += 1);
}
void dfs2(int v, int tar, int curr){
if (cmp[v] == tar) curr++;
res[pp[tar]][cmp[v]] += curr;
for (int u: adj[v]) dfs2(u, tar, curr);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
//freopen("test.txt", "r", stdin);
int n, r, q; cin >> n >> r >> q;
for (int i = 1; i <= n; i++){
if (i > 1){
int a; cin >> a;
adj[a].pb(i);
}
cin >> cmp[i];
comp[cmp[i]].pb(i);
}
dfs(1);
curr = 0;
for (int i = 1; i <= r; i++){
if (siz(comp[i]) < BLOCK) continue;
pp[i] = ++curr;
dfs2(1, i, 0);
}
while (q--){
int a, b; cin >> a >> b;
if (pp[a]) cout << res[pp[a]][b] << endl;
else {
int tot = 0;
for (int v: comp[a]){
tot += lower_bound(all(occ[b]), in[v]+sub[v])-lower_bound(all(occ[b]), in[v]);
}
cout << tot << endl;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |