답안 #559648

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559648 2022-05-10T11:00:55 Z ngpin04 코끼리 (Dancing Elephants) (IOI11_elephants) C++14
100 / 100
7666 ms 15560 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 = 1500;
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][2 * K + 5];
int dp[Nmax / K][2 * K + 5];
int b[Nmax / K][2 * K + 5];
int v[Nmax / K][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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 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 1045 ms 2400 KB Output is correct
8 Correct 1016 ms 2920 KB Output is correct
9 Correct 1071 ms 5364 KB Output is correct
10 Correct 814 ms 4920 KB Output is correct
11 Correct 739 ms 4792 KB Output is correct
12 Correct 1592 ms 5144 KB Output is correct
13 Correct 753 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 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 1045 ms 2400 KB Output is correct
8 Correct 1016 ms 2920 KB Output is correct
9 Correct 1071 ms 5364 KB Output is correct
10 Correct 814 ms 4920 KB Output is correct
11 Correct 739 ms 4792 KB Output is correct
12 Correct 1592 ms 5144 KB Output is correct
13 Correct 753 ms 4700 KB Output is correct
14 Correct 1255 ms 3732 KB Output is correct
15 Correct 1370 ms 3912 KB Output is correct
16 Correct 3071 ms 5644 KB Output is correct
17 Correct 3007 ms 6912 KB Output is correct
18 Correct 2941 ms 5620 KB Output is correct
19 Correct 1388 ms 5480 KB Output is correct
20 Correct 2651 ms 5708 KB Output is correct
21 Correct 2516 ms 5560 KB Output is correct
22 Correct 1368 ms 5716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 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 1045 ms 2400 KB Output is correct
8 Correct 1016 ms 2920 KB Output is correct
9 Correct 1071 ms 5364 KB Output is correct
10 Correct 814 ms 4920 KB Output is correct
11 Correct 739 ms 4792 KB Output is correct
12 Correct 1592 ms 5144 KB Output is correct
13 Correct 753 ms 4700 KB Output is correct
14 Correct 1255 ms 3732 KB Output is correct
15 Correct 1370 ms 3912 KB Output is correct
16 Correct 3071 ms 5644 KB Output is correct
17 Correct 3007 ms 6912 KB Output is correct
18 Correct 2941 ms 5620 KB Output is correct
19 Correct 1388 ms 5480 KB Output is correct
20 Correct 2651 ms 5708 KB Output is correct
21 Correct 2516 ms 5560 KB Output is correct
22 Correct 1368 ms 5716 KB Output is correct
23 Correct 5685 ms 10484 KB Output is correct
24 Correct 7065 ms 15120 KB Output is correct
25 Correct 5275 ms 15216 KB Output is correct
26 Correct 5866 ms 15068 KB Output is correct
27 Correct 5867 ms 14936 KB Output is correct
28 Correct 4274 ms 5260 KB Output is correct
29 Correct 4102 ms 5252 KB Output is correct
30 Correct 3859 ms 5256 KB Output is correct
31 Correct 3760 ms 5324 KB Output is correct
32 Correct 4345 ms 14848 KB Output is correct
33 Correct 4018 ms 14148 KB Output is correct
34 Correct 4616 ms 15076 KB Output is correct
35 Correct 4214 ms 15560 KB Output is correct
36 Correct 3672 ms 14800 KB Output is correct
37 Correct 5940 ms 15424 KB Output is correct
38 Correct 4510 ms 13896 KB Output is correct
39 Correct 4504 ms 15156 KB Output is correct
40 Correct 4514 ms 13900 KB Output is correct
41 Correct 7619 ms 14792 KB Output is correct
42 Correct 7666 ms 15048 KB Output is correct