Submission #52688

# Submission time Handle Problem Language Result Execution time Memory
52688 2018-06-26T11:16:18 Z aquablitz11 Regions (IOI09_regions) C++14
50 / 100
7581 ms 131072 KB
// headers
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#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;
// types
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using dbl = double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using piipi = pair<pii, int>;
using pipii = pair<int, pii>;
using plpii = pair<ll, pii>;
using ppii = pair<pii, pii>;
// loops
#define forx(i, x, y) for (int i = (x); i <= (y); ++i)
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define for1(i, n) for (int i = 1; i <= (n); ++i)
#define rofx(i, x, y) for (int i = x; i >= y; --i)
#define rofn(i, n) for (int i = n-1; i >= 0; --i)
#define rof1(i, n) for (int i = n; i >= 1; --i)
#define fora(x, v) for (auto x : v)
// define shortcuts
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define X first
#define Y second
#define MP make_pair
#define MT make_tuple
// functions
template<class T> inline bool hasbit(T x, int i) { return x&(1<<i); }
template<class T> inline T togbit(T x, int i) { return x|(1<<i); }
template<class T> inline T setbit(T x, int i) { return x|(1<<i); }
template<class T> inline T rembit(T x, int i) { return x&~(1<<i); }
template<class T> inline bool setmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool setmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> int compress(vector<T> &v) { sort(all(v)); v.resize(unique(all(v))-v.begin()); return v.size(); }
// read functions
int read_int() { int x; scanf("%d", &x); return x; }
ll read_ll() { ll x; scanf("%" SCNd64, &x); return x; }
ull read_ull() { ull x; scanf("%" SCNu64, &x); return x; }
dbl read_dbl() { dbl x; scanf("%lf", &x); return x; }
void _R(int &x) { x = read_int(); }
void _R(ll &x) { x = read_ll(); }
void _R(dbl &x) { x = read_dbl(); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
// print functions
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%" PRId64, x); }
void _W(const ull &x) { printf("%" PRIu64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
// pseudo-random number generator
template<class T, T x1, T x2, T x3, int y1, int y2, int y3>
struct PRNG {
    using S = typename make_signed<T>::type;
    T s;
    PRNG(T _s = 0) : s(_s) {}
    T next() {
        T z = (s += x1);
        z = (z ^ (z >> y1)) * x2;
        z = (z ^ (z >> y2)) * x3;
        return z ^ (z >> y3);
    }
    T next(T n) { return next() % n; }
    S next(S l, S r) { return l + next(r - l + 1); }
    T operator()() { return next(); }
    T operator()(T n) { return next(n); }
    S operator()(S l, S r) { return next(l, r); }
    static T gen(T s) { return PRNG(s)(); }
    template<class U>
    void shuffle(U first, U last) {
        size_t n = last - first;
        for (size_t i = 0; i < n; i++) swap(first[i], first[next(i + 1)]);
    }
};
PRNG<uint32_t, 0x9E3779B1, 0x85EBCA6B, 0xC2B2AE35, 16, 13, 16> r32;
PRNG<uint64_t, 0x9E3779B97F4A7C15, 0xBF58476D1CE4E5B9, 0x94D049BB133111EB, 30, 27, 31> r64;
// program
int _main();
int main() {
    //ios::sync_with_stdio(false), cin.tie(0);
    //time_t begin = clock();
    int ret = _main();
    //time_t end = clock();
    //fprintf(stderr, "Program took %.3f seconds to compute\n", ((double)end-begin)/CLOCKS_PER_SEC);
    return ret;
}
 
/************************************************************
                CODE STARTS BELOW THIS POINT
************************************************************/
 
const int N = 200010;
const int K = 25010;
const int SN = 501;
 
#define map unordered_map
 
int n, k, q, sn;
int par[N], reg[N], pre[N], post[N], tick, seq[N];
vector<int> G[N], mem[K], memp[K];
map<int, map<int, int>> prec, qs;
 
map<int, int> *dfs(int u)
{
    pre[u] = ++tick;
    memp[reg[u]].pb(tick);
    seq[tick] = u;
    
    map<int, int> *S = new map<int, int>;
    fora(v, G[u]) {
        map<int, int> *T = dfs(v);
        if (T->size() > S->size()) swap(S, T);
        fora(p, *T) (*S)[p.F] += p.S;
    }
    ++((*S)[reg[u]]);
    post[u] = tick;
    if (mem[reg[u]].size() > sn) {
        ++qs[reg[u]][pre[u]];
        --qs[reg[u]][post[u]+1];
    }
 
    return S;
}
 
int _main()
{
    R(n, k, q);
    R(reg[1]);
    mem[reg[1]].pb(1);
    forx(i, 2, n) {
        R(par[i], reg[i]);
        G[par[i]].pb(i);
        mem[reg[i]].pb(i);
    }
    sn = sqrt(n);
    dfs(1);
    for1(i, k) if (mem[i].size() > sn) {
        for1(j, n) {
            qs[i][j] += qs[i][j-1];
            prec[i][reg[seq[j]]] += qs[i][j];
        }
    }
    while (q--) {
        int r1, r2;
        R(r1, r2);
        if (mem[r1].size() > sn) {
            W(prec[r1][r2]);
        } else {
            int ans = 0;
            fora(u, mem[r1]) {
                auto lb = lower_bound(all(memp[r2]), pre[u]);
                auto ub = upper_bound(all(memp[r2]), post[u]);
                ans += ub-lb;
            }
            W(ans);
        }
        fflush(stdout);
    }
    
    return 0;
}

Compilation message

regions.cpp: In function 'll read_ll()':
regions.cpp:49:42: warning: format '%ld' expects argument of type 'long int*', but argument 2 has type 'll* {aka long long int*}' [-Wformat=]
 ll read_ll() { ll x; scanf("%" SCNd64, &x); return x; }
                                        ~~^
regions.cpp: In function 'ull read_ull()':
regions.cpp:50:45: warning: format '%lu' expects argument of type 'long unsigned int*', but argument 2 has type 'ull* {aka long long unsigned int*}' [-Wformat=]
 ull read_ull() { ull x; scanf("%" SCNu64, &x); return x; }
                                           ~~^
regions.cpp: In function 'void _W(const ll&)':
regions.cpp:60:44: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
 void _W(const ll &x) { printf("%" PRId64, x); }
                                           ~^
regions.cpp: In function 'void _W(const ull&)':
regions.cpp:61:45: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'ull {aka long long unsigned int}' [-Wformat=]
 void _W(const ull &x) { printf("%" PRIu64, x); }
                                            ~^
regions.cpp: In function 'std::unordered_map<int, int>* dfs(int)':
regions.cpp:134:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (mem[reg[u]].size() > sn) {
         ~~~~~~~~~~~~~~~~~~~^~~~
regions.cpp: In function 'int _main()':
regions.cpp:154:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for1(i, k) if (mem[i].size() > sn) {
                    ~~~~~~~~~~~~~~^~~~
regions.cpp:163:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (mem[r1].size() > sn) {
             ~~~~~~~~~~~~~~~^~~~
regions.cpp: In function 'int read_int()':
regions.cpp:48:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 int read_int() { int x; scanf("%d", &x); return x; }
                         ~~~~~^~~~~~~~~~
regions.cpp: In function 'll read_ll()':
regions.cpp:49:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 ll read_ll() { ll x; scanf("%" SCNd64, &x); return x; }
                      ~~~~~^~~~~~~~~~~~~~~~
regions.cpp: In function 'ull read_ull()':
regions.cpp:50:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 ull read_ull() { ull x; scanf("%" SCNu64, &x); return x; }
                         ~~~~~^~~~~~~~~~~~~~~~
regions.cpp: In function 'dbl read_dbl()':
regions.cpp:51:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 dbl read_dbl() { dbl x; scanf("%lf", &x); return x; }
                         ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6264 KB Output is correct
2 Correct 7 ms 6264 KB Output is correct
3 Correct 7 ms 6276 KB Output is correct
4 Correct 10 ms 6296 KB Output is correct
5 Correct 16 ms 6344 KB Output is correct
6 Correct 27 ms 6504 KB Output is correct
7 Correct 36 ms 6632 KB Output is correct
8 Correct 35 ms 6632 KB Output is correct
9 Correct 48 ms 7280 KB Output is correct
10 Correct 86 ms 8432 KB Output is correct
11 Correct 137 ms 9072 KB Output is correct
12 Correct 151 ms 9456 KB Output is correct
13 Correct 215 ms 11760 KB Output is correct
14 Correct 384 ms 16540 KB Output is correct
15 Correct 420 ms 16540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2919 ms 91672 KB Output is correct
2 Correct 2835 ms 91672 KB Output is correct
3 Correct 4141 ms 91672 KB Output is correct
4 Correct 362 ms 91672 KB Output is correct
5 Correct 424 ms 91672 KB Output is correct
6 Correct 1902 ms 131072 KB Output is correct
7 Correct 3246 ms 131072 KB Output is correct
8 Runtime error 2997 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Incorrect 3156 ms 131072 KB Unexpected end of file - int32 expected
10 Runtime error 2778 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
11 Execution timed out 7581 ms 131072 KB Time limit exceeded (wall clock)
12 Incorrect 2127 ms 131072 KB Unexpected end of file - int32 expected
13 Incorrect 2812 ms 131072 KB Unexpected end of file - int32 expected
14 Execution timed out 5320 ms 131072 KB Time limit exceeded (wall clock)
15 Incorrect 4659 ms 131072 KB Unexpected end of file - int32 expected
16 Incorrect 3873 ms 131072 KB Unexpected end of file - int32 expected
17 Execution timed out 5511 ms 131072 KB Time limit exceeded (wall clock)