Submission #727508

# Submission time Handle Problem Language Result Execution time Memory
727508 2023-04-20T22:06:18 Z CutSandstone Regions (IOI09_regions) C++17
70 / 100
8000 ms 131072 KB
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx,avx2")
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define f first
#define s second
#define pb push_back
#define inf ~((long long)1<<63)
using namespace std;
const int mod = 1000000007;
void setio(string name = "") { 
    ios::sync_with_stdio(false), cin.tie(nullptr); 
    if(name.length()) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
class BIT {
  private:
    int size;
    vector<int> bit;
  public:
    BIT(int size) : size(size), bit(size + 1) {}
    void add(int ind, int val){for(ind++; ind <= size; ind += ind & -ind) { bit[ind] += val; };}
    int prev(int ind) {
        int total = 0;
        for (ind++; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
        return total;
    }
    int sum(int a, int b){
        return prev(b)-(a == 0 ? 0:prev(a-1));
    }
};
int N, R, timer = 0;
vector<vector<int>> cnt;
vector<int> big;
vector<int> st, en;
vector<vector<int>> g;
vector<vector<int>> ans;
vector<int> nums;
vector<BIT> sums;
void dfs(int s, vector<int>& curr){
    if(big[nums[s]]+1) curr[big[nums[s]]]++;
    else for(int i = 0; i<curr.size(); i++) ans[s][i] = curr[i];
    st[s] = timer++;
    for(int i: g[s]) dfs(i, curr);
    if(big[nums[s]]+1) curr[big[nums[s]]]--;
    en[s] = timer-1;
}
void getComp(int size){
    vector<int> b(size);
    for(int i = R-1; i>=0; i--) if(big[i]+1) b[--size] = i;
    for(int i = 0; i<b.size(); i++) for(int j: cnt[b[i]])
        sums[i].add(st[j], 1);
}
int main() {
    setio();
    int Q; cin >> N >> R >> Q;
    g.resize(N, vector<int>(0));
    nums.resize(N);
    cin >> nums[0];
    for(int i = 1; i<N; i++){
        int par; cin >> par;
        g[par-1].pb(i);
        cin >> nums[i];
    }
    cnt.resize(R, vector<int>(0));
    for(int i = 0; i<N; i++) cnt[--nums[i]].pb(i);
    vector<int> arr(R);
    for(int i = 0; i<R; i++) arr[i] = cnt[i].size();
    sort(arr.begin(), arr.end());
    int minSZ = 0;
    for(int i = R-1; i>=0; i--) if(arr[i] > R-i) minSZ = i; else break;
    big.resize(R, -1);
    int p = 0;
    for(int i = 0; i<R; i++) if(cnt[i].size() >= minSZ) big[i] = p++;
    st.resize(N);
    en.resize(N);
    ans.resize(N, vector<int>(p));
    vector<int> curr(p);
    dfs(0, curr);
    sums.resize(p, BIT(N));
    getComp(p);
    BIT b(N);
    map<int, int> mem;
    while(Q--> 0){
        int up, down; cin >> up >> down; --up, --down;
        if(mem.find(up*R+down) != mem.end()) cout << mem[up*R+down] << endl;
        else if(big[down]+1){
            int ret = 0;
            for(int i: cnt[up]) ret+=sums[big[down]].sum(st[i], en[i]);
            mem[up*R+down] = ret;
            cout << ret << endl;
        }else if(big[up]+1){
            int ret = 0;
            for(int i: cnt[down])
                ret+=ans[i][big[up]];
            mem[up*R+down] = ret;
            cout << ret << endl;
        }else{
            int ret = 0;
            for(int i: cnt[down]) b.add(st[i], 1);
            for(int i: cnt[up]) ret+=b.sum(st[i], en[i])-(up == down ? 1:0);
            for(int i: cnt[down]) b.add(st[i], -1);
            mem[up*R+down] = ret;
            cout << ret << endl;
        }
    }
}

Compilation message

regions.cpp: In function 'void dfs(int, std::vector<int>&)':
regions.cpp:45:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     else for(int i = 0; i<curr.size(); i++) ans[s][i] = curr[i];
      |                         ~^~~~~~~~~~~~
regions.cpp: In function 'void getComp(int)':
regions.cpp:54:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |     for(int i = 0; i<b.size(); i++) for(int j: cnt[b[i]])
      |                    ~^~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:77:47: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   77 |     for(int i = 0; i<R; i++) if(cnt[i].size() >= minSZ) big[i] = p++;
      |                                 ~~~~~~~~~~~~~~^~~~~~~~
regions.cpp: In function 'void setio(std::string)':
regions.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         freopen((name + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regions.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 3 ms 208 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 8 ms 376 KB Output is correct
6 Correct 10 ms 496 KB Output is correct
7 Correct 27 ms 500 KB Output is correct
8 Correct 28 ms 592 KB Output is correct
9 Correct 34 ms 1248 KB Output is correct
10 Correct 100 ms 1640 KB Output is correct
11 Correct 149 ms 2252 KB Output is correct
12 Correct 187 ms 2972 KB Output is correct
13 Correct 226 ms 3004 KB Output is correct
14 Correct 272 ms 51140 KB Output is correct
15 Correct 400 ms 83568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1214 ms 129004 KB Output is correct
2 Correct 1344 ms 113368 KB Output is correct
3 Runtime error 86 ms 131072 KB Execution killed with signal 9
4 Correct 403 ms 4456 KB Output is correct
5 Correct 500 ms 6964 KB Output is correct
6 Correct 761 ms 7412 KB Output is correct
7 Correct 954 ms 8716 KB Output is correct
8 Correct 2110 ms 16884 KB Output is correct
9 Correct 3809 ms 22892 KB Output is correct
10 Correct 5985 ms 30172 KB Output is correct
11 Correct 6199 ms 27796 KB Output is correct
12 Execution timed out 8042 ms 29004 KB Time limit exceeded
13 Execution timed out 8061 ms 29348 KB Time limit exceeded
14 Correct 5071 ms 34512 KB Output is correct
15 Execution timed out 8058 ms 26244 KB Time limit exceeded
16 Execution timed out 8085 ms 31860 KB Time limit exceeded
17 Execution timed out 8038 ms 31104 KB Time limit exceeded