Submission #559653

# Submission time Handle Problem Language Result Execution time Memory
559653 2022-05-10T11:04:02 Z ngpin04 Dancing Elephants (IOI11_elephants) C++14
100 / 100
7275 ms 11548 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define ALL(x) (x).begin(), (x).end() 
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
    if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
    if (a < b) {a = b; return true;} return false;
}
const int Nmax = 15e4 + 5;
const int Q = 400;
const int K = 700;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);

#include "elephants.h"
//#include "grader.cpp"

int ptr[Nmax / K + 1][2 * K + 5];
int dp[Nmax / K + 1][2 * K + 5];
int b[Nmax / K + 1][2 * K + 5];
int v[Nmax / K + 1][2 * K + 5];

pair <int, int> pos[Nmax];

int sz[Nmax];
int x[Nmax];
int n,l,cnt,cntb;

void build(int p, int b[], int ptr[], int dp[], int v[]) {
    dp[sz[p]] = 0;
    for (int i = sz[p] - 1, j = sz[p]; i >= 0; i--) {
        while (v[j - 1] - v[i] > l)
            j--;
        if (j == sz[p]) {
            dp[i] = 0;
            ptr[i] = i;
        } else {
            ptr[i] = ptr[j];
            dp[i] = dp[j] + 1;
        }
    }
}

void del(int i) {
    int p = pos[i].fi;
    int ind = pos[i].se;
    for (int j = ind; j + 1 < sz[p]; j++)
        b[p][j] = b[p][j + 1];
    sz[p]--;

    for (int j = 0; j < sz[p]; j++) {
        pos[b[p][j]] = mp(p, j);
        v[p][j] = x[b[p][j]];
    }

    build(p, b[p], ptr[p], dp[p], v[p]);
}

int getmax(int p) {
    return v[p][sz[p] - 1];
}

void add(int i) {
    int p = 0;
    while (p < cntb && (sz[p] > 0 && getmax(p) < x[i]))
        p++;

    int ind = -1;
    while (ind + 1 < sz[p] && v[p][ind + 1] < x[i])
        ind++;

    for (int j = sz[p]; j - 1 > ind; j--)
        b[p][j] = b[p][j - 1];

    b[p][ind + 1] = i;
    sz[p]++;
    for (int j = 0; j < sz[p]; j++) {
        pos[b[p][j]] = mp(p, j);
        v[p][j] = x[b[p][j]];
    }

    build(p, b[p], ptr[p], dp[p], v[p]);
}

void rebuild() {
    vector <int> ind;

    for (int i = 0; i <= cntb; i++) {
        for (int j = 0; j < sz[i]; j++)
            ind.push_back(b[i][j]);
        sz[i] = 0;
    }

    for (int i = 0; i < n; i++) {
        int p = i / K;
        int it = ind[i];
        pos[it] = mp(p, sz[p]);
        b[p][sz[p]++] = it;
    }

    for (int i = 0; i <= cntb; i++) {
        for (int j = 0; j < sz[i]; j++) 
            v[i][j] = x[b[i][j]];

        build(i, b[i], ptr[i], dp[i], v[i]);
    }
}

void init(int N, int L, int X[]) {
    n = N;
    l = L;
    for (int i = 0; i < n; i++)
        x[i] = X[i];

    cntb = (n - 1) / K;

    vector <int> ind(n, 0);
    iota(ALL(ind), 0);
    sort(ALL(ind), [](int i, int j) {
        return x[i] < x[j];
    });

    for (int i = 0; i < n; i++) {
        int p = i / K;
        int it = ind[i];
        pos[it] = mp(p, sz[p]);
        b[p][sz[p]++] = it;
    }

    for (int i = 0; i <= cntb; i++) {
        for (int j = 0; j < sz[i]; j++) 
            v[i][j] = x[b[i][j]];

        build(i, b[i], ptr[i], dp[i], v[i]);
    }
}

int update(int i, int y) {
    cnt++;
    del(i);
    x[i] = y;
    add(i);

    int res = 0;
    int p = 0;
    int cur = 0;

    while (true) {
        res += dp[p][cur];
        cur = ptr[p][cur];
        int nxt = p + 1;
        while (nxt <= cntb && 
        (sz[nxt] == 0 || getmax(nxt) - v[p][cur] <= l)) 
            nxt++;

        res++;
        if (nxt > cntb) 
            break;
            
        int lo = -1;
        int hi = sz[nxt] - 1;
        while (hi - lo > 1) {
            int mid = (lo + hi) >> 1;
            if (v[nxt][mid] - v[p][cur] <= l)
                lo = mid;
            else
                hi = mid;
        }

        cur = hi;
        p = nxt;
    }

    if (cnt == Q) {
        cnt = 0;
        rebuild();
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 599 ms 1636 KB Output is correct
8 Correct 643 ms 2136 KB Output is correct
9 Correct 785 ms 3784 KB Output is correct
10 Correct 762 ms 3760 KB Output is correct
11 Correct 662 ms 3792 KB Output is correct
12 Correct 1216 ms 3920 KB Output is correct
13 Correct 695 ms 3848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 599 ms 1636 KB Output is correct
8 Correct 643 ms 2136 KB Output is correct
9 Correct 785 ms 3784 KB Output is correct
10 Correct 762 ms 3760 KB Output is correct
11 Correct 662 ms 3792 KB Output is correct
12 Correct 1216 ms 3920 KB Output is correct
13 Correct 695 ms 3848 KB Output is correct
14 Correct 942 ms 2276 KB Output is correct
15 Correct 1066 ms 2492 KB Output is correct
16 Correct 1802 ms 4076 KB Output is correct
17 Correct 1947 ms 5428 KB Output is correct
18 Correct 2215 ms 5412 KB Output is correct
19 Correct 1263 ms 5320 KB Output is correct
20 Correct 2129 ms 5252 KB Output is correct
21 Correct 2119 ms 5272 KB Output is correct
22 Correct 1362 ms 5276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 599 ms 1636 KB Output is correct
8 Correct 643 ms 2136 KB Output is correct
9 Correct 785 ms 3784 KB Output is correct
10 Correct 762 ms 3760 KB Output is correct
11 Correct 662 ms 3792 KB Output is correct
12 Correct 1216 ms 3920 KB Output is correct
13 Correct 695 ms 3848 KB Output is correct
14 Correct 942 ms 2276 KB Output is correct
15 Correct 1066 ms 2492 KB Output is correct
16 Correct 1802 ms 4076 KB Output is correct
17 Correct 1947 ms 5428 KB Output is correct
18 Correct 2215 ms 5412 KB Output is correct
19 Correct 1263 ms 5320 KB Output is correct
20 Correct 2129 ms 5252 KB Output is correct
21 Correct 2119 ms 5272 KB Output is correct
22 Correct 1362 ms 5276 KB Output is correct
23 Correct 5183 ms 10868 KB Output is correct
24 Correct 5321 ms 10952 KB Output is correct
25 Correct 4415 ms 10888 KB Output is correct
26 Correct 4199 ms 10808 KB Output is correct
27 Correct 4536 ms 10992 KB Output is correct
28 Correct 2085 ms 2348 KB Output is correct
29 Correct 2044 ms 2352 KB Output is correct
30 Correct 2081 ms 2352 KB Output is correct
31 Correct 2066 ms 2356 KB Output is correct
32 Correct 3691 ms 10796 KB Output is correct
33 Correct 3181 ms 10820 KB Output is correct
34 Correct 4274 ms 10872 KB Output is correct
35 Correct 3786 ms 10936 KB Output is correct
36 Correct 2568 ms 10924 KB Output is correct
37 Correct 6372 ms 10928 KB Output is correct
38 Correct 4498 ms 10952 KB Output is correct
39 Correct 4567 ms 11268 KB Output is correct
40 Correct 4554 ms 11396 KB Output is correct
41 Correct 7275 ms 11548 KB Output is correct
42 Correct 6828 ms 11272 KB Output is correct