Submission #1106443

#TimeUsernameProblemLanguageResultExecution timeMemory
1106443atomIzbori (COCI22_izbori)C++17
25 / 110
246 ms3020 KiB
#include "bits/stdc++.h" // @JASPER'S BOILERPLATE using namespace std; using ll = long long; #ifdef JASPER #include "debug.h" #else #define debug(...) 166 #endif const int N = 2e5 + 5; const int B = 450; int a[N]; int n; bool sub12() { return (n <= 2000); } bool sub3() { for (int i = 1; i <= n; ++i) if (a[i] > 2) return false; return true; } template <typename T> struct Fenwick { vector <int> f; int n; Fenwick(int _n) { f.resize(_n + 5); n = _n; } void upd(int x, int val) { for (; x <= n; x += x & -x) f[x] += val; } int get(int x) { int ans = 0; for (; x > 0; x -= x & -x) ans += f[x]; return ans; } }; signed main() { cin.tie(0) -> sync_with_stdio(0); #ifdef JASPER freopen("in2", "r", stdin); #endif cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; vector <int> vals; for (int i = 1; i <= n; ++i) vals.push_back(a[i]); sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end()) - vals.begin()); vector <int> cnt(n + 1, 0); for (int i = 1; i <= n; ++i) { a[i] = (int) (lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin() + 1); cnt[a[i]]++; } vector <bool> heavy(n + 1, false); for (int i = 1; i <= n; ++i) { if (cnt[i] >= B) heavy[i] = true; } ll ans = 0; // processing light queries: all elements appear less than sqrt(n) cnt.assign(n + 1, 0); for (int i = 1; i <= n; ++i) { int mx = 0; for (int j = i; j <= n && (j - i + 1) < 2 * B; ++j) { if (!heavy[a[j]]) { cnt[a[j]]++; if (cnt[a[j]] > mx) mx = cnt[a[j]]; if (2 * mx > (j - i + 1)) { ++ans; } } } for (int j = i; j <= n && (j - i + 1) < 2 * B; ++j) if (!heavy[a[j]]) cnt[a[j]]--; } // processing heavy queries: all elements appear at least sqrt(n) for (int x = 1; x <= n; ++x) { if (heavy[x]) { auto get = [&] (int _x) { debug(_x); return _x + n; }; vector <int> f(2 * n + 5, 0); int sum = 0; int prf = 0; f[get(prf)]++; for (int i = 1; i <= n; ++i) { prf += (a[i] == x? 1 : -1); if (get(prf) > 0) sum += f[get(prf - 1)]; else sum -= f[get(prf)]; ans += sum; f[get(prf)]++; } } } cout << ans << "\n"; return 0; }

Compilation message (stderr)

Main.cpp: In lambda function:
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
Main.cpp:101:39: note: in expansion of macro 'debug'
  101 |             auto get = [&] (int _x) { debug(_x); return _x + n; };
      |                                       ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...