제출 #604248

#제출 시각아이디문제언어결과실행 시간메모리
604248skittles1412식물 비교 (IOI20_plants)C++17
30 / 100
4126 ms1387340 KiB
#include "bits/extc++.h" using namespace std; template <typename T> void dbgh(const T& t) { cerr << t << endl; } template <typename T, typename... U> void dbgh(const T& t, const U&... u) { cerr << t << " | "; dbgh(u...); } #ifdef DEBUG #define dbg(...) \ cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \ dbgh(__VA_ARGS__); #else #define dbg(...) #define cerr \ if (false) \ cerr #endif #define endl "\n" #define long int64_t #define sz(x) int((x).size()) struct PS { vector<int> psum; PS(int n) : psum(n + 1) {} void add(int l, int r, int x) { psum[l] += x; psum[r + 1] -= x; } vector<int> compute() const { vector<int> ans(sz(psum)); partial_sum(begin(psum), end(psum), ans.begin()); return ans; } }; struct PSA { vector<int> psum; PSA() {} PSA(const vector<int>& arr) : psum(sz(arr) + 1) { partial_sum(begin(arr), end(arr), psum.begin() + 1); } int query(int l, int r) const { return psum[r + 1] - psum[l]; } }; const int maxn = 2e5 + 5; int k, n, karr[maxn]; bool vis[maxn]; set<int> gt[maxn]; PSA psa1, psa2; void dfs(int u) { if (vis[u]) { return; } vis[u] = true; for (auto& v : gt[u]) { dfs(v); } set<int> tmp; for (auto& v : gt[u]) { tmp.insert(begin(gt[v]), end(gt[v])); } if (sz(tmp) > sz(gt[u])) { swap(tmp, gt[u]); } gt[u].insert(begin(tmp), end(tmp)); } void init(int _k, vector<int> arr) { k = _k; n = sz(arr); auto tmp = arr; tmp.insert(tmp.end(), begin(arr), end(arr)); psa1 = PSA(tmp); for (auto& a : tmp) { a ^= 1; } psa2 = PSA(tmp); if (k == 2) { return; } bool vis[n] {}; auto irange = [&](int x, int y) -> bool { if (x + k >= n && y < x + k - n) { return true; } return x <= y && y < x + k; }; for (int i = 0; i < n; i++) { PS ps(n * 2); for (int j = 0; j < n; j++) { if (!vis[j] && !arr[j]) { ps.add(j + 1, j + k - 1, 1); } } auto psv = ps.compute(); for (int j = 0; j < n; j++) { if (!psv[j] && !psv[j + n] && !arr[j]) { karr[j] = -i; vis[j] = true; for (int a = 0; a < n; a++) { if (irange(a, j)) { arr[a]--; } if (!vis[a] && (irange(a, j) || irange(j, a))) { gt[j].insert(a); } } break; } } } if (2 * k <= n) { for (int i = 0; i < n; i++) { dfs(i); } } } int compare_plants(int x, int y) { if (k == 2) { if (!psa1.query(x, y - 1) || !psa2.query(y, x + n - 1)) { return 1; } else if (!psa1.query(y, x + n - 1) || !psa2.query(x, y - 1)) { return -1; } return 0; } if (2 * k > n) { if (karr[x] > karr[y]) { return 1; } else { return -1; } } if (gt[x].count(y)) { return 1; } else if (gt[y].count(x)) { return -1; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...