Submission #311383

# Submission time Handle Problem Language Result Execution time Memory
311383 2020-10-10T03:44:44 Z caoash Regions (IOI09_regions) C++14
100 / 100
7768 ms 101460 KB
#include <bits/stdc++.h> 
using namespace std;

using ll = long long;

using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

const int MX = 200005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;

namespace output {
  void pr(int x) {
    cout << x;
  }
  void pr(long x) {
    cout << x;
  }
  void pr(ll x) {
    cout << x;
  }
  void pr(unsigned x) {
    cout << x;
  }
  void pr(unsigned long x) {
    cout << x;
  }
  void pr(unsigned long long x) {
    cout << x;
  }
  void pr(float x) {
    cout << x;
  }
  void pr(double x) {
    cout << x;
  }
  void pr(long double x) {
    cout << x;
  }
  void pr(char x) {
    cout << x;
  }
  void pr(const char * x) {
    cout << x;
  }
  void pr(const string & x) {
    cout << x;
  }
  void pr(bool x) {
    pr(x ? "true" : "false");
  }

  template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
  template < class T > void pr(const T & x);

  template < class T, class...Ts > void pr(const T & t,
    const Ts & ...ts) {
    pr(t);
    pr(ts...);
  }
  template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
    pr("{", x.f, ", ", x.s, "}");
  }
  template < class T > void pr(const T & x) {
    pr("{"); // const iterator needed for vector<bool>
    bool fst = 1;
    for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
    pr("}");
  }

  void ps() {
    pr("\n");
  } // print w/ spaces
  template < class T, class...Ts > void ps(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(" ");
    ps(ts...);
  }

  void pc() {
    cout << "]" << endl;
  } // debug w/ commas
  template < class T, class...Ts > void pc(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(", ");
    pc(ts...);
  }
  #define dbg(x...) pr("[", #x, "] = ["), pc(x);
}

#ifdef LOCAL
using namespace output;
#endif

template <class T, int ...Ns> struct BIT {
	T val = 0; void upd(T v) { val += v; }
	T query() { return val; }
};
template <class T, int N, int... Ns> struct BIT<T, N, Ns...> {
	BIT<T,Ns...> bit[N+1];
	template<typename... Args> void upd(int pos, Args... args) { assert(pos > 0);
		for (; pos<=N; pos+=pos&-pos) bit[pos].upd(args...); }
	template<typename... Args> T sum(int r, Args... args) {
		T res=0; for (;r;r-=r&-r) res += bit[r].query(args...); 
		return res; }
	template<typename... Args> T query(int l, int r, Args... 
		args) { return sum(r,args...)-sum(l-1,args...); }
}; 

const int B = 500;
const int MXR = 25005;

vi adj[MX];
int nr[MX], p[MX], good[MX], st[MX], sz[MX];
int cnt = 0;
BIT<int, MX> orz;
vi locs[MX];
map<int, int> vals[MX];
map<pi, int> pre;

void dfs_init(int v, int p) {
    st[v] = cnt++;
    sz[v] = 1;
    for (int to : adj[v]) {
        if (to != p) {
            dfs_init(to, v);
            sz[v] += sz[to];
        }
    }
}

void comb(map<int, int> &a, map<int, int> &b) {
    if (sz(a) < sz(b)) swap(a, b);
    for (auto x : b) a[x.f] += x.s;
    b.clear();
}

void dfs_merge(int v, int p) {
    if (good[nr[v]]) vals[v][nr[v]]++;
    for (int to : adj[v]) {
        if (to != p) {
            dfs_merge(to, v);
            comb(vals[v], vals[to]);
        }
    }
    // assert(sz(vals[v]) <= B);
    if (!good[nr[v]]) {
        for (auto x : vals[v]) {
            pre[mp(nr[v], x.f)] += x.s;
        }
    }
}

void dfs_path(int v, int p, map<int, int> &curr) {
    for (auto x : curr) {
        pre[mp(x.f, nr[v])] += x.s;
    }
    if (good[nr[v]]) curr[nr[v]]++;
    // assert(sz(curr) <= B);
    for (int to : adj[v]) {
        if (to != p) {
            dfs_path(to, v, curr);
        }
    }
    if (good[nr[v]]) {
        curr[nr[v]]--;
        if (curr[nr[v]] == 0) curr.erase(nr[v]);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, r, q; cin >> n >> r >> q;
    for (int i = 0; i < n; i++) {
        if (i) {
            cin >> p[i];
            p[i]--;
            adj[p[i]].pb(i);
        }
        cin >> nr[i];
        nr[i]--;
        good[nr[i]]++;
        locs[nr[i]].pb(i);
    }
    for (int i = 0; i < r; i++) {
        if (good[i] > B) good[i] = 1; 
        else good[i] = 0;
    }
    dfs_init(0, -1);
    dfs_merge(0, -1);
    map<int, int> init;
    dfs_path(0, -1, init);
    for (int i = 0; i < q; i++) {
        int qu, qv; cin >> qu >> qv;
        qu--, qv--;
        if (!good[qu] && !good[qv]) {
            int ret = 0;
            for (auto p : locs[qv]) {
                orz.upd(st[p] + 1, 1);
                // dbg(st[p], 1);
            }
            for (auto p : locs[qu]) {
                ret += orz.query(st[p] + 1, st[p] + sz[p]);
                // dbg(st[p], st[p] + sz[p] - 1);
            }
            for (auto p : locs[qv]) {
                orz.upd(st[p] + 1, -1);
                // dbg(st[p], -1);
            }
            cout << ret << endl;
        } else {
            cout << pre[mp(qu, qv)] << endl;
        }
    }
}

# Verdict Execution time Memory Grader output
1 Correct 12 ms 19200 KB Output is correct
2 Correct 12 ms 19200 KB Output is correct
3 Correct 14 ms 19200 KB Output is correct
4 Correct 17 ms 19200 KB Output is correct
5 Correct 20 ms 19200 KB Output is correct
6 Correct 29 ms 19328 KB Output is correct
7 Correct 45 ms 19328 KB Output is correct
8 Correct 49 ms 19328 KB Output is correct
9 Correct 70 ms 20128 KB Output is correct
10 Correct 127 ms 19712 KB Output is correct
11 Correct 161 ms 20096 KB Output is correct
12 Correct 187 ms 20864 KB Output is correct
13 Correct 256 ms 20352 KB Output is correct
14 Correct 375 ms 20864 KB Output is correct
15 Correct 492 ms 26496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1949 ms 25708 KB Output is correct
2 Correct 1971 ms 23416 KB Output is correct
3 Correct 3810 ms 29528 KB Output is correct
4 Correct 436 ms 20992 KB Output is correct
5 Correct 431 ms 24448 KB Output is correct
6 Correct 2023 ms 22520 KB Output is correct
7 Correct 2722 ms 23544 KB Output is correct
8 Correct 2619 ms 33528 KB Output is correct
9 Correct 4890 ms 30072 KB Output is correct
10 Correct 7329 ms 40440 KB Output is correct
11 Correct 7768 ms 28792 KB Output is correct
12 Correct 2360 ms 34112 KB Output is correct
13 Correct 3116 ms 35632 KB Output is correct
14 Correct 4743 ms 69752 KB Output is correct
15 Correct 5481 ms 45948 KB Output is correct
16 Correct 5432 ms 60908 KB Output is correct
17 Correct 6926 ms 101460 KB Output is correct