Submission #61427

#TimeUsernameProblemLanguageResultExecution timeMemory
61427qkxwsmBubble Sort 2 (JOI18_bubblesort2)C++17
100 / 100
4378 ms312060 KiB
#include <bits/stdc++.h> #include "bubblesort2.h" using namespace std; template<class T> void readi(T &x) { T input = 0; bool negative = false; char c = ' '; while (c < '-') { c = getchar(); } if (c == '-') { negative = true; c = getchar(); } while (c >= '0') { input = input * 10 + (c - '0'); c = getchar(); } if (negative) { input = -input; } x = input; } template<class T> void printi(T output) { if (output == 0) { putchar('0'); return; } if (output < 0) { putchar('-'); output = -output; } int aout[20]; int ilen = 0; while(output) { aout[ilen] = ((output % 10)); output /= 10; ilen++; } for (int i = ilen - 1; i >= 0; i--) { putchar(aout[i] + '0'); } return; } template<class T> void ckmin(T &a, T b) { a = min(a, b); } template<class T> void ckmax(T &a, T b) { a = max(a, b); } long long randomize(long long mod) { return ((1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod; } #define MP make_pair #define PB push_back #define PF push_front #define LB lower_bound #define UB upper_bound #define fi first #define se second const long double PI = 4.0 * atan(1.0); const long double EPS = 1e-10; #define MAGIC 347 #define SINF 10007 #define CO 1000007 #define INF 1000000007 #define BIG 1000000931 #define LARGE 1696969696967ll #define GIANT 2564008813937411ll #define LLINF 2696969696969696969ll #define MAXN 500013 long long normalize(long long x, long long mod = INF) { return (((x % mod) + mod) % mod); } typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; int N, Q, M; int arr[MAXN], temp[MAXN]; pii quer[MAXN]; int ans[MAXN]; int pos0[2 * MAXN], pos1[2 * MAXN]; vector<ll> compress; int seg[8 * MAXN], lazy[8 * MAXN]; int tree[2 * MAXN]; void ud(int idx, int val) { for (int e = idx + 1; e <= M; e += e & (-e)) { tree[e] += val; } } int get(int idx) { int res = 0; for (int e = idx + 1; e > 0; e -= e & (-e)) { res += tree[e]; } return res; } void down(int w, int L, int R) { seg[w] += lazy[w]; if (L != R) { lazy[2 * w] += lazy[w]; lazy[2 * w + 1] += lazy[w]; } lazy[w] = 0; return; } void update(int w, int L, int R, int a, int b, int val) { // cerr << "update " << a << '-' << b << " = " << val << endl; down(w, L, R); if (b < L || R < a) { return; } if (a <= L && R <= b) { lazy[w] += val; down(w, L, R); return; } int mid = (L + R)/2; update(2 * w, L, mid, a, b, val); update(2 * w + 1, mid + 1, R, a, b, val); seg[w] = max(seg[2 * w], seg[2 * w + 1]); } int query(int w, int L, int R, int a, int b) { down(w, L, R); if (b < L || R < a) { return -INF; } if (a <= L && R <= b) { return seg[w]; } int mid = (L + R)/2; return max(query(2 * w, L, mid, a, b), query(2 * w + 1, mid + 1, R, a, b)); } void setval(int idx, int val) { int was = query(1, 0, M - 1, idx, idx); update(1, 0, M - 1, idx, idx, val - was); return; } vi countScans(vi a, vi b, vi c) { N = a.size(); Q = b.size(); for (int i = 0; i < N; i++) { arr[i] = a[i]; } for (int i = 0; i < Q; i++) { quer[i] = MP(b[i], c[i]); } for (int i = 0; i < N; i++) { compress.PB(1ll * arr[i] * INF + i); } for (int i = 0; i < Q; i++) { compress.PB(1ll * quer[i].se * INF + quer[i].fi); } sort(compress.begin(), compress.end()); compress.erase(unique(compress.begin(), compress.end()), compress.end()); for (int i = 0; i < N; i++) { arr[i] = LB(compress.begin(), compress.end(), 1ll * arr[i] * INF + i) - compress.begin(); } for (int i = 0; i < Q; i++) { quer[i].se = LB(compress.begin(), compress.end(), 1ll * quer[i].se * INF + quer[i].fi) - compress.begin(); } // for (int i = 0; i < N; i++) // { // cerr << arr[i] << ' '; // } // cerr << endl; // for (int i = 0; i < Q; i++) // { // cerr << quer[i].fi << ' ' << quer[i].se << endl; // } M = compress.size(); update(1, 0, M - 1, 0, M - 1, -10 * CO); for (int i = 0; i < N; i++) { pos1[arr[i]] = i; } for (int i = 0; i < N; i++) { temp[i] = arr[i]; } sort(temp, temp + N); for (int i = 0; i < N; i++) { pos0[temp[i]] = i; } for (int i = 0; i < N; i++) { setval(arr[i], pos1[arr[i]] - pos0[arr[i]]); } for (int i = 0; i < N; i++) { ud(arr[i], 1); } for (int q = 0; q < Q; q++) { //each guy with value > arr[idx] has to int idx = quer[q].fi, val = quer[q].se, was = arr[idx]; arr[idx] = val; ud(was, -1); ud(val, 1); // pos1[was] = 0; setval(was, -10 * CO); // pos1[val] = idx; setval(val, idx); //the thing is, each guy only moves by at most one... //when you delete x, each guy moves down 1 // for (int i = was + 1; i < M; i++) // { // pos0[i]--; // } update(1, 0, M - 1, was + 1, M, 1); // for (int i = val + 1; i < M; i++) // { // pos0[i]++; // } update(1, 0, M - 1, val + 1, M, -1); // pos0[val] = 0; // for (int i = 0; i < N; i++) // { // if (arr[i] < val) // { //// pos0[val]++; // update(1, 0, M - 1, val, val, -1); // } // } update(1, 0, M - 1, val, val, -get(val - 1)); ans[q] = query(1, 0, M - 1, 0, M - 1); // for (int i = 0; i < N; i++) // { // ckmax(ans[q], pos1[arr[i]] - pos0[arr[i]]); // } } vi res(Q); for (int i = 0; i < Q; i++) { res[i] = ans[i]; } return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...