Submission #727490

#TimeUsernameProblemLanguageResultExecution timeMemory
727490CutSandstoneRegions (IOI09_regions)C++17
12 / 100
626 ms131076 KiB
// #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; vector<int> arr; public: BIT(int size) : size(size), bit(size + 1), arr(size) {} void set(int ind, int val) { add(ind, val - arr[ind]); } void add(int ind, int val) { arr[ind] += val; ind++; for (; ind <= size; ind += ind & -ind) { bit[ind] += val; } } int prev(int ind) { ind++; int total = 0; for (; 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<vector<int>> precomp; vector<int> st, en; vector<vector<int>> g; vector<vector<int>> above; vector<int> ans; vector<int> nums; vector<BIT> sums; void dfs(int s, vector<int> curr, vector<stack<int>> last){ ans[s] = ++curr[nums[s]]; if(big[nums[s]]+1) last[big[nums[s]]].push(s); else for(int i = 0; i<last.size(); i++) above[s][i] = last[i].empty() ? -1:last[i].top(); st[s] = timer++; for(int i: g[s]) dfs(i, curr, last); if(big[nums[s]]+1) last[big[nums[s]]].pop(); curr[nums[s]]--; en[s] = timer-1; } void getComp(int size){ precomp.resize(size, vector<int>(size, 0)); 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].set(st[j], 1); for(int j = 0; j<b.size(); j++) for(int k: cnt[b[j]]) precomp[j][i]+=sums[i].sum(st[k], en[k])-(j == i ? 1:0); } } 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); big.resize(R, -1); int p = 0; vector<int> arr(R); for(int i = 0; i<R; i++) arr[i] = cnt[i].size(); sort(arr.begin(), arr.end()); int minSZ = arr[max(0, R-100)]; for(int i = 0; i<R; i++) if(cnt[i].size() >= minSZ) big[i] = p++; st.resize(N); en.resize(N); ans.resize(N); above.resize(N, vector<int>(p)); dfs(0, vector<int>(R), vector<stack<int>>(p)); sums.resize(p, BIT(N)); getComp(p); BIT b(N); while(Q--> 0){ int up, down; cin >> up >> down; --up, --down; if(big[up]+1 && big[down]+1) cout << precomp[big[up]][big[down]] << endl; else if(big[up]+1){ int ret = 0; for(int i: cnt[down]){ int u = above[i][big[up]]; if(u != -1) ret+=ans[u]; } cout << ret << endl; }else if(big[down]+1){ int ret = 0; for(int i: cnt[up]) ret+=sums[big[down]].sum(st[i], en[i]); cout << ret << endl; }else{ int ret = 0; for(int i: cnt[down]) b.set(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.set(st[i], 0); cout << ret << endl; } } }

Compilation message (stderr)

regions.cpp: In function 'void dfs(int, std::vector<int>, std::vector<std::stack<int> >)':
regions.cpp:56:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::stack<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     else for(int i = 0; i<last.size(); i++) above[s][i] = last[i].empty() ? -1:last[i].top();
      |                         ~^~~~~~~~~~~~
regions.cpp: In function 'void getComp(int)':
regions.cpp:67:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int i = 0; i<b.size(); i++){
      |                    ~^~~~~~~~~
regions.cpp:70:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for(int j = 0; j<b.size(); j++) for(int k: cnt[b[j]])
      |                        ~^~~~~~~~~
regions.cpp: In function 'int main()':
regions.cpp:93:47: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   93 |     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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...