Submission #1276744

#TimeUsernameProblemLanguageResultExecution timeMemory
1276744huydayyIndex (COCI21_index)C++20
110 / 110
684 ms12420 KiB
#include <bits/stdc++.h> #define TASK "coci2021_r6_index" #define int long long #define double long double #define fi first #define se second #define pb push_back #define ii pair<int, int> #define vi vector<int> #define vvi vector<vi> #define vii vector<ii> #define reset(f, x) memset(f, x, sizeof(f)) #define all(x) x.begin(), x.end() #define sz(x) (long long)x.size() #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define FORV(v, H) for (auto &v: H) #define __builtin_popcount __builtin_popcountll #define BIT(mask, i) ((mask >> i) & 1ll) #define MASK(i) (1ll << (i)) #define ONBIT(mask, i) (mask (1ll << (i))) #define OFFBIT(mask, i) (mask &~ (1ll << (i))) #define mid(l, r) ((l + r) >> 1) #define left(id) (id << 1) #define right(id) ((id << 1) 1) #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define UNIQUE(x) sort(all(x)); x.resize(distance(x.begin(), unique(all(x)))) #define BUG1(a) cout << a << '\n' #define BUG2(a, b) cout << a << " " << b << '\n' #define BUG3(a, b, c) cout << a << " " << b << " " << c << '\n' #define BUG4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << '\n' #define BUG5(a, b, c, d, e) cout << a << " " << b << " " << c << " " << d << " " << e << '\n' #define BUG6(a, b, c, d, e, f) cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << '\n' using namespace std; template <class X, class Y> bool maximize(X &A, const Y &B){ if (A < B) return A = B, true; return false; } template <class X, class Y> bool minimize(X &A, const Y &B){ if (A > B) return A = B, true; return false; } const int oo = 1e18; const int MOD = 1e9 + 7; const int MAXN = 2e5 + 5; const int LOG = 16; const int base = 447; const int BLOCK = 317; const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; int N, Q; int a[MAXN], block[MAXN], bit[MAXN], res[MAXN], ans = 0; struct Query{ int l, r, id; } q[MAXN]; void add(int x, int y){ for(; x; x -= x & -x) bit[x] += y; } int get(int x){ int ans = 0; for(; x < MAXN; x += x & -x) ans += bit[x]; return ans; } void Add(int x){ add(a[x], 1); if (get(ans + 1) >= ans + 1) ans++; } void Remove(int x){ add(a[x], -1); if (get(ans) < ans) ans--; } main(){ if (fopen(TASK".inp", "r")){ freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> Q; FOR(i, 1, N) cin >> a[i]; FOR(i, 1, Q){ cin >> q[i].l >> q[i].r; q[i].id = i; } FOR(i, 1, N) block[i] = (i - 1) / BLOCK; sort(q + 1, q + Q + 1, [&](const Query &x, const Query &y){ if (block[x.l] != block[y.l]) return block[x.l] < block[y.l]; if (block[x.l] & 1) return x.r > y.r; return x.r < y.r; }); int l = 1, r = 0; FOR(i, 1, Q){ while (r < q[i].r) Add(++r); while (r > q[i].r) Remove(r--); while (l < q[i].l) Remove(l++); while (l > q[i].l) Add(--l); res[q[i].id] = ans; } FOR(i, 1, Q) cout << res[i] << '\n'; return 0; } /* ___ __ ___ _______ ___ ___ ________ ___ ________ ___ ___ ___ ___ ___ ___ \ \\ \ |\ \|\ ___ \ |\ \|\ \ |\ ____\|\ \|\ __ \ |\ \|\ \|\ \|\ \ |\ \ / / | \ \ \/ /|\ \ \ \ __/|\ \ \\\ \ \ \ \___|\ \ \ \ \|\ \ \ \ \\\ \ \ \\\ \ \ \ \/ / / \ \ ___ \ \ \ \ \_|/_\ \ \\\ \ \ \ \ __\ \ \ \ __ \ \ \ __ \ \ \\\ \ \ \ / / \ \ \\ \ \ \ \ \ \_|\ \ \ \\\ \ \ \ \|\ \ \ \ \ \ \ \ \ \ \ \ \ \ \\\ \ \/ / / \ \__\\ \__\ \__\ \_______\ \_______\ \ \_______\ \__\ \__\ \__\ \ \__\ \__\ \_______\__/ / / \|__| \|__|\|__|\|_______|\|_______| \|_______|\|__|\|__|\|__| \|__|\|__|\|_______|\___/ / \|___|/ Author: Kieu Gia Huy a.k.a kiwi From: C.H.V with luv <3 ... */

Compilation message (stderr)

index.cpp:83:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   83 | main(){
      | ^~~~
index.cpp: In function 'int main()':
index.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...