Submission #1044228

# Submission time Handle Problem Language Result Execution time Memory
1044228 2024-08-05T08:11:06 Z 정민찬(#11007) Team Coding (EGOI24_teamcoding) C++17
23 / 100
4000 ms 1048576 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

const ll B = 3000;

struct mySet{
    map<int,int> sub;
    map<pair<int,int>, int> save;
    set<pair<int,int>> ssave;
    deque<int> dq;
    int sz = 0;
};

mySet st[100010];
int id[100010];
vector<int> g[100010];
int dep[100010];
int l[100010];
int par[100010];
vector<pair<int,int>> vec[100010];
map<int,int> cnt[100010];
vector<int> large;
int down[100010];

void Merge(mySet &u, mySet &v, int cdep) {
    while (u.dq.size() < v.dq.size()) u.dq.push_back(0);
    /*for (auto &[col, d] : v.ssave) {
        if (u.save.find(make_pair(col, d)) != u.save.end()) {
            u.save[make_pair(col, d)] ++;
            continue;
        }
        if (vec[d].size() < B) {
            if (cnt[col][d] > u.dq[d-cdep]) {
                u.sub[col] -= cnt[col][d] - u.dq[d-cdep];
            }
        }
        u.save[{col, d}] = 1;
        u.ssave.insert({col, d});
    }*/
    
    for (int i=0; i<v.dq.size(); i++) {
        /*if (vec[cdep + i].size() < B) {
            for (auto &[k, col] : vec[cdep + i]) {
                if (u.ssave.find({col, cdep + i}) != u.ssave.end()) continue;
                if (k > u.dq[i]) {
                    u.sub[col] -= k - u.dq[i];
                }
                else break;
            }
        }*/
        u.dq[i] += v.dq[i];
        /*if (vec[cdep + i].size() < B) {
            for (auto &[k, col] : vec[cdep + i]) {
                if (u.ssave.find({col, cdep + i}) != u.ssave.end()) continue;
                if (k > u.dq[i]) {
                    // sub.find()
                    u.sub[col] += k - u.dq[i];
                }
                else break;
            }
        }*/
    }
    u.sz += v.sz;
}

int pv;
int chk[100010];
int ans[100010];
int tme[100010];
int poss[100010];

int dfs(int x) {
    if (g[x].empty()) {
        id[x] = pv ++;
        st[id[x]].dq.push_back(1);
        //st[id[x]].ssave.insert({l[x], dep[x]});
        //st[id[x]].save[{l[x], dep[x]}] = 1;
        st[id[x]].sz = 1;
        /*if (vec[dep[x]].size() < B) {
            for (auto &[i, col] : vec[dep[x]]) {
                if (col == l[x]) continue;
                if (i > 1) {
                    st[id[x]].sub[col] += i - 1;
                }
            }
        }*/
        if (!chk[l[x]]) {
            poss[x] = 1;
            ans[x] = 1;
        }
        return id[x];
    }
    int cur = -1;
    chk[l[x]] ++;
    for (auto &y : g[x]) {
        int child = dfs(y);
        if (cur == -1) {
            cur = child;
            continue;
        }
        if (st[cur].sz < st[child].sz) swap(cur, child);
        Merge(st[cur], st[child], dep[x] + 1);
    }
    id[x] = cur;
    st[id[x]].dq.push_front(1);
    //st[id[x]].ssave.insert({l[x], dep[x]});
    //st[id[x]].save[{l[x], dep[x]}] = 1;
    st[id[x]].sz ++;
    /*if (vec[dep[x]].size() < B) {
        for (auto &[i, col] : vec[dep[x]]) {
            if (col == l[x]) continue;
            if (i > 1) {
                st[id[x]].sub[col] += i - 1;
            }
        }
    }*/
    chk[l[x]] --;
    if (true) {
        poss[x] = 1;
        /*if (st[id[x]].sub.find(l[x]) != st[id[x]].sub.end())
            ans[x] -= st[id[x]].sub[l[x]];
        auto lit = st[id[x]].ssave.lower_bound(make_pair(l[x], -1));
        auto rit = st[id[x]].ssave.upper_bound(make_pair(l[x], (int)1e9));
        for (auto it=lit; it!=rit; it++) {
            int tot = st[id[x]].save[{l[x], it->second}];
            int temp = min(cnt[l[x]][it->second], st[id[x]].dq[it->second - dep[x]]); 
            ans[x] += temp - cnt[l[x]][it->second];
        }*/
        for (int d=dep[x]; d<=down[x]; d++) {
            /*int tot = 0;
            if (st[id[x]].save.find({l[x], d}) != st[id[x]].save.end())
                tot = st[id[x]].save[{l[x], d}];*/
            int temp = min(cnt[l[x]][d], st[id[x]].dq[d - dep[x]]); 
            ans[x] += temp;
        }
        for (auto &d : large) {
            assert(0);
            if (d < dep[x] || d > down[x]) continue;
            int tot = st[id[x]].save[{l[x], d}];
            int temp = min(cnt[l[x]][d], st[id[x]].dq[d - dep[x]]);
            ans[x] += temp;
        }
    }
    
    return id[x];
}

int in[100010], out[100010];
int pv2;

void dfs2(int x) {
    down[x] = dep[x];
    in[x] = ++pv2;
    for (auto &y : g[x]) {
        dep[y] = dep[x] + 1;
        dfs2(y);
        down[x] = max(down[x], down[y]);
    }
    out[x] = pv2;
}

struct Fenwick{
    vector<ll> tree;
    void init(ll n) {
        tree.assign(n+1, 0);
    }
    void upd(ll i, ll v, ll n) {
        while (i <= n) {
            tree[i] += v;
            i += i & -i;
        }
    }
    ll qry(ll i) {
        ll ret = 0;
        while (i) {
            ret += tree[i];
            i -= i & -i;
        }
        return ret;
    }
};

Fenwick seg;

vector<int> t[100010];

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    int N, K;
    cin >> N >> K;
    for (int i=0; i<N; i++) {
        cin >> l[i];
    }
    for (int i=1; i<N; i++) {
        cin >> par[i];
        g[par[i]].push_back(i);
    }
    dep[1] = 0;
    dfs2(0);
    int mxdep = 0;
    for (int i=0; i<N; i++) {
        //t[dep[i]].push_back(l[i]);
        mxdep = max(mxdep, dep[i]);
    }
    /*for (int i=0; i<=mxdep; i++) {
        assert(!t[i].empty());
        sort(t[i].begin(), t[i].end());
        vec[i].push_back({1, t[i][0]});
        for (int j=1; j<t[i].size(); j++) {
            if (t[i][j] != vec[i].back().second) {
                vec[i].push_back({1, t[i][j]});
            }
            else vec[i].back().first ++;
        }
        sort(vec[i].begin(), vec[i].end());
    }*/
    
    /*for (int i=0; i<=mxdep; i++) {
        if (vec[i].size() < B) {
            for (auto &[j, col] : vec[i]) {
                cnt[col][i] = j;
            }
        }
        else {
            large.push_back(i);
        }
    }*/
    for (int i=0; i<N; i++) {
        if (cnt[l[i]].find(dep[i]) == cnt[l[i]].end())
            cnt[l[i]][dep[i]] = 1;
        else
            cnt[l[i]][dep[i]] ++;
    }
    
    dfs(0);
    vector<vector<int>> qrs(K);
    for (int i=0; i<N; i++) {
        //if (vec[dep[i]].size() < B)
        qrs[l[i]].push_back(i); 
    }
    /*seg.init(mxdep + 1);
    for (int i=0; i<K; i++) {
        for (auto &j : qrs[i]) {
            seg.upd(dep[j]+1, 1, mxdep+1);
        }
        for (auto &j : qrs[i]) {
            if (poss[j]) {
                ans[j] += seg.qry(down[j]+1) - seg.qry(dep[j]);
            }
        }
        for (auto &j : qrs[i]) {
            seg.upd(dep[j]+1, -1, mxdep+1);
        }
    }*/
    seg.init(N);
    for (int i=0; i<K; i++) {
        for (auto &j : qrs[i]) {
            seg.upd(in[j], 1, N);
        }
        for (auto &j : qrs[i]) {
            if (poss[j]) {
                tme[j] = ans[j] - (seg.qry(out[j]) - seg.qry(in[j]-1));
            }
        }
        for (auto &j : qrs[i]) {
            seg.upd(in[j], -1, N);
        }
    }
    int mx = -1, mn = 0;
    for (int i=0; i<N; i++) {
        if (poss[i]) {
            if (mx < ans[i]) {
                mx = ans[i];
                mn = tme[i];
            }
            else if (mx == ans[i] && mn > tme[i]) {
                mn = tme[i];
            }
        }
    }
    cout << mx << ' ' << mn << '\n';

    return 0;
}

Compilation message

Main.cpp: In function 'void Merge(mySet&, mySet&, int)':
Main.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (int i=0; i<v.dq.size(); i++) {
      |                   ~^~~~~~~~~~~~
Main.cpp: In function 'int dfs(int)':
Main.cpp:143:17: warning: unused variable 'tot' [-Wunused-variable]
  143 |             int tot = st[id[x]].save[{l[x], d}];
      |                 ^~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 98652 KB Output is correct
2 Correct 30 ms 98476 KB Output is correct
3 Correct 40 ms 98616 KB Output is correct
4 Correct 29 ms 98460 KB Output is correct
5 Correct 31 ms 98472 KB Output is correct
6 Correct 29 ms 98644 KB Output is correct
7 Correct 33 ms 98640 KB Output is correct
8 Correct 108 ms 101400 KB Output is correct
9 Runtime error 1326 ms 1048576 KB Execution killed with signal 9
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 98644 KB Output is correct
2 Correct 32 ms 98640 KB Output is correct
3 Correct 31 ms 98640 KB Output is correct
4 Correct 76 ms 99152 KB Output is correct
5 Execution timed out 4065 ms 118748 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 98516 KB Output is correct
2 Correct 33 ms 98640 KB Output is correct
3 Correct 43 ms 98688 KB Output is correct
4 Correct 36 ms 98652 KB Output is correct
5 Correct 46 ms 98648 KB Output is correct
6 Correct 43 ms 98568 KB Output is correct
7 Correct 33 ms 98652 KB Output is correct
8 Runtime error 1457 ms 1048576 KB Execution killed with signal 9
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 47 ms 98580 KB Output is correct
2 Correct 38 ms 98616 KB Output is correct
3 Correct 39 ms 98488 KB Output is correct
4 Correct 53 ms 98552 KB Output is correct
5 Correct 52 ms 98644 KB Output is correct
6 Correct 51 ms 98620 KB Output is correct
7 Correct 32 ms 98656 KB Output is correct
8 Correct 124 ms 101456 KB Output is correct
9 Correct 33 ms 98652 KB Output is correct
10 Correct 79 ms 98964 KB Output is correct
11 Correct 40 ms 98640 KB Output is correct
12 Correct 222 ms 168532 KB Output is correct
13 Correct 31 ms 98556 KB Output is correct
14 Correct 53 ms 99176 KB Output is correct
15 Correct 52 ms 98652 KB Output is correct
16 Correct 48 ms 98904 KB Output is correct
17 Correct 46 ms 98652 KB Output is correct
18 Correct 41 ms 98644 KB Output is correct
19 Correct 41 ms 98652 KB Output is correct
20 Correct 39 ms 98904 KB Output is correct
21 Correct 37 ms 98908 KB Output is correct
22 Correct 51 ms 98640 KB Output is correct
23 Correct 33 ms 99072 KB Output is correct
24 Correct 49 ms 99212 KB Output is correct
25 Correct 33 ms 99072 KB Output is correct
26 Correct 69 ms 109140 KB Output is correct
27 Correct 107 ms 128084 KB Output is correct
28 Correct 36 ms 98988 KB Output is correct
29 Correct 37 ms 99208 KB Output is correct
30 Correct 97 ms 122452 KB Output is correct
31 Correct 36 ms 98736 KB Output is correct
32 Correct 48 ms 98976 KB Output is correct
33 Correct 54 ms 100688 KB Output is correct
34 Correct 49 ms 98988 KB Output is correct
35 Correct 32 ms 98636 KB Output is correct
36 Correct 46 ms 98900 KB Output is correct
37 Correct 38 ms 98828 KB Output is correct
38 Correct 35 ms 98708 KB Output is correct
39 Correct 33 ms 98512 KB Output is correct
40 Correct 40 ms 98648 KB Output is correct
41 Correct 37 ms 98568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 98652 KB Output is correct
2 Correct 30 ms 98476 KB Output is correct
3 Correct 40 ms 98616 KB Output is correct
4 Correct 29 ms 98460 KB Output is correct
5 Correct 31 ms 98472 KB Output is correct
6 Correct 29 ms 98644 KB Output is correct
7 Correct 33 ms 98640 KB Output is correct
8 Correct 108 ms 101400 KB Output is correct
9 Runtime error 1326 ms 1048576 KB Execution killed with signal 9
10 Halted 0 ms 0 KB -