Submission #583845

#TimeUsernameProblemLanguageResultExecution timeMemory
583845thezomb1eRegions (IOI09_regions)C++17
100 / 100
7614 ms113248 KiB
//thatsramen #include <bits/stdc++.h> #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define eb emplace_back #define pb push_back #define ft first #define sd second #define pi pair<int, int> #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define dbg(...) dbg_out(__VA_ARGS__) using ll = long long; using ld = long double; using namespace std; using namespace __gnu_pbds; //Constants const ll INF = 5 * 1e18; const int IINF = 2 * 1e9; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const ld PI = 3.14159265359; //Templates template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';} template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';} void dbg_out() {cerr << endl;} template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); } template<typename T> void mins(T& x, T y) {x = min(x, y);} template<typename T> void maxs(T& x, T y) {x = max(x, y);} template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; //order_of_key(k): number of elements strictly less than k //find_by_order(k): k-th element in the set void setPrec() {cout << fixed << setprecision(15);} void unsyncIO() {cin.tie(0)->sync_with_stdio(0);} void setIn(string s) {freopen(s.c_str(), "r", stdin);} void setOut(string s) {freopen(s.c_str(), "w", stdout);} void setIO(string s = "") { unsyncIO(); setPrec(); if(s.size()) setIn(s + ".in"), setOut(s + ".out"); } // #define TEST_CASES struct SegmentTree { vector<vector<int>> tre; int sz; SegmentTree(vector<int> &a) { int n = (int) a.size(); sz = 1; while (sz < n) sz *= 2; tre.resize(2 * sz); build(a, 1, 0, sz); } vector<int> op(vector<int> &a, vector<int> &b) { vector<int> res; int n = a.size(), m = b.size(); int i = 0, j = 0; while (i < n || j < m) { if (j == m || (i < n && a[i] < b[j])) { res.pb(a[i++]); } else { res.pb(b[j++]); } } return res; } void rcl(int x) { tre[x] = op(tre[2 * x], tre[2 * x + 1]); } void build(vector<int> &a, int x, int lx, int rx) { if (rx - lx == 1) { if (lx < a.size()) { tre[x] = {a[lx]}; } return; } int m = (lx + rx) / 2; build(a, 2 * x, lx, m); build(a, 2 * x + 1, m, rx); rcl(x); } int get(int l, int r, int bb, int x, int lx, int rx) { if (rx <= l || lx >= r) { return 0; } if (l <= lx && r >= rx) { int lo = 0, hi = (int) tre[x].size() - 1; int res = -1; while (lo <= hi) { int mi = (lo + hi) / 2; if (tre[x][mi] <= bb) { res = mi; lo = mi + 1; } else { hi = mi - 1; } } return res + 1; } int m = (lx + rx) / 2; int s1 = get(l, r, bb, 2 * x, lx, m); int s2 = get(l, r, bb, 2 * x + 1, m, rx); return s1 + s2; } int get(int l, int r, int bb) { return get(l, r, bb, 1, 0, sz); } }; const int MAX = 1000 * 1000 * 1000; const int sq = 420; void solve() { int n, r, q; cin >> n >> r >> q; vector<vector<int>> e(n + 5); vector<int> region(n + 5), cnt(r + 5, 0); cin >> region[1]; for (int i = 2; i <= n; i++) { int p; cin >> p; e[p].pb(i); cin >> region[i]; cnt[region[i]]++; } vector<int> fun(r + 5, 0); int pp = 1; for (int i = 1; i <= r; i++) { if (cnt[i] > sq) fun[i] = pp++; } vector<int> up_cnt(pp + 2, 0); vector<vector<int>> pre(pp + 2, vector<int> (n + 5, 0)); vector<vector<pi>> init(r + 5); int time = 0; function<void(int)> dfs = [&](int f) { int reg = region[f]; for (int i = 1; i < pp; i++) { if (pre[i][reg] <= MAX) pre[i][reg] += up_cnt[i]; } up_cnt[fun[reg]]++; pi cur; cur.ft = time++; for (int to : e[f]) { dfs(to); } cur.sd = time++; init[reg].pb(cur); up_cnt[fun[reg]]--; }; dfs(1); vector<SegmentTree> sts; for (int i = 1; i <= r; i++) { sort(all(init[i])); vector<int> tru; for (pi x : init[i]) tru.pb(x.sd); sts.pb(SegmentTree(tru)); } for (int i = 0; i < q; i++) { int r1, r2; cin >> r1 >> r2; if (cnt[r1] > sq) { cout << pre[fun[r1]][r2] << endl; } else { int ans = 0; if (!init[r2].empty()) { for (pi x : init[r1]) { pi val = {x.ft, -IINF}; int j = lower_bound(all(init[r2]), val) - init[r2].begin(); ans += sts[r2 - 1].get(j, (int) init[r2].size(), x.sd); } } cout << ans << endl; } } } int main() { setIO(); int tt = 1; #ifdef TEST_CASES cin >> tt; #endif while (tt--) solve(); return 0; }

Compilation message (stderr)

regions.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("O3")
      | 
regions.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("unroll-loops")
      | 
regions.cpp: In member function 'void SegmentTree::build(std::vector<int>&, int, int, int)':
regions.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |             if (lx < a.size()) {
      |                 ~~~^~~~~~~~~~
regions.cpp: In function 'void setIn(std::string)':
regions.cpp:47:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
regions.cpp: In function 'void setOut(std::string)':
regions.cpp:48:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...