Submission #274451

# Submission time Handle Problem Language Result Execution time Memory
274451 2020-08-19T12:02:12 Z evpipis Regions (IOI09_regions) C++11
60 / 100
8000 ms 131076 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

const int k = 100, len = 2e5+5;
int cnt_st[len], cnt_en[len], order[len], pos[len], cnt;
vector<int> vec[len], adj[len], store_big, buck[len];
int par[len], reg[len], big[len], n, r, q;
int prec[len/k][len/k];

struct node{
    int val;
    node *lef, *rig;

    node(int v = 0, node *l = NULL, node *r = NULL){
        val = v;
        lef = l;
        rig = r;
    }
};

typedef node *pnode;
pnode null, root[len];

pnode update(pnode t, int l, int r, int i){
    if (l == r)
        return new node(t->val+1, null, null);

    int mid = (l+r)/2;
    if (i <= mid)
        return new node(t->val+1, update(t->lef, l, mid, i), t->rig);
    else
        return new node(t->val+1, t->lef, update(t->rig, mid+1, r, i));
}

int query(pnode t, int l, int r, int i){
    if (l == r)
        return t->val;

    int mid = (l+r)/2;
    if (i <= mid)
        return query(t->lef, l, mid, i);
    return query(t->rig, mid+1, r, i);
}

void fix(int u){
    cnt_st[u] = ++cnt;
    order[cnt] = u;
    root[u] = update(root[par[u]], 1, r, reg[u]);

    for (auto v: adj[u])
        fix(v);

    cnt_en[u] = cnt;
}

int bs(int myr, int l, int r){
    return upper_bound(vec[myr].begin(), vec[myr].end(), r) - lower_bound(vec[myr].begin(), vec[myr].end(), l);
}

int main(){
    scanf("%d %d %d", &n, &r, &q);
    scanf("%d", &reg[1]);
    buck[reg[1]].pb(1);
    for (int i = 2; i <= n; i++){
        scanf("%d %d", &par[i], &reg[i]);
        buck[reg[i]].pb(i);
        adj[par[i]].pb(i);
    }

    for (int i = 1; i <= r; i++)
        big[i] = (buck[i].size() > k);

    /// make persistent segment tree
    null = new node();
    null->lef = null->rig = null;
    root[0] = null;
    fix(1);

    /// fix vec[]
    for (int i = 1; i <= n; i++)
        vec[reg[order[i]]].pb(i);

    /// compute prec[]
    for (int i = 1; i <= r; i++)
        if (big[i])
            pos[i] = store_big.size(), store_big.pb(i);

    for (int u = 1; u <= n; u++){
        if (!big[reg[u]]) continue;

        int r1 = reg[u];
        for (auto r2: store_big)
            prec[pos[r1]][pos[r2]] += bs(r2, cnt_st[u], cnt_en[u]);
    }

    // answer queries
    while (q--){
        int r1, r2;
        scanf("%d %d", &r1, &r2);

        if (!big[r1]){
            int ans = 0;
            for (auto u: buck[r1])
                ans += bs(r2, cnt_st[u], cnt_en[u]);
            printf("%d\n", ans), fflush(stdout);
        }
        else if (!big[r2]){
            int ans = 0;
            for (auto u: buck[r2])
                ans += query(root[u], 1, r, r1);
            printf("%d\n", ans), fflush(stdout);
        }
        else{
            printf("%d\n", prec[pos[r1]][pos[r2]]), fflush(stdout);
        }
    }
    return 0;
}

Compilation message

regions.cpp: In function 'int main()':
regions.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   63 |     scanf("%d %d %d", &n, &r, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |     scanf("%d", &reg[1]);
      |     ~~~~~^~~~~~~~~~~~~~~
regions.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |         scanf("%d %d", &par[i], &reg[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:101:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  101 |         scanf("%d %d", &r1, &r2);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 11 ms 14464 KB Output is correct
4 Correct 14 ms 14464 KB Output is correct
5 Correct 17 ms 14592 KB Output is correct
6 Correct 25 ms 14720 KB Output is correct
7 Correct 36 ms 14976 KB Output is correct
8 Correct 44 ms 15104 KB Output is correct
9 Correct 60 ms 16384 KB Output is correct
10 Correct 91 ms 18176 KB Output is correct
11 Correct 156 ms 19840 KB Output is correct
12 Correct 168 ms 22144 KB Output is correct
13 Correct 204 ms 23040 KB Output is correct
14 Correct 496 ms 25592 KB Output is correct
15 Correct 608 ms 30992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1678 ms 41720 KB Output is correct
2 Correct 1661 ms 42232 KB Output is correct
3 Correct 2464 ms 48504 KB Output is correct
4 Correct 378 ms 29176 KB Output is correct
5 Correct 484 ms 33656 KB Output is correct
6 Correct 652 ms 40956 KB Output is correct
7 Correct 1635 ms 51576 KB Output is correct
8 Correct 2299 ms 70212 KB Output is correct
9 Correct 7373 ms 99960 KB Output is correct
10 Execution timed out 8022 ms 117900 KB Time limit exceeded
11 Execution timed out 8052 ms 129156 KB Time limit exceeded
12 Execution timed out 8029 ms 121116 KB Time limit exceeded
13 Execution timed out 8041 ms 121076 KB Time limit exceeded
14 Execution timed out 8048 ms 127324 KB Time limit exceeded
15 Runtime error 304 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
16 Runtime error 275 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
17 Runtime error 316 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)