Submission #826307

# Submission time Handle Problem Language Result Execution time Memory
826307 2023-08-15T12:34:45 Z 79brue Dancing Elephants (IOI11_elephants) C++17
100 / 100
2104 ms 18876 KB
#include "elephants.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int SZ_MIN = 300, SZ_MAX = 600;

int len;

struct Group{
    int arr[SZ_MAX+2], sz; /// 0-base
    int nxt[SZ_MAX+2], cnt[SZ_MAX+2];

    void calculateNxt(){
        int last = sz;
        for(int i=sz-1; i>=0; i--){
            if(arr[sz-1] - arr[i] <= len){
                nxt[i] = i, cnt[i] = 0;
                continue;
            }
            while(arr[last-1] - arr[i] > len) last--;
            nxt[i] = nxt[last], cnt[i] = cnt[last] + 1;
        }
    }

    Group(int *A, int L, int R){
        sz = R-L+1;
        for(int i=L; i<=R; i++) arr[i-L] = A[i];
        sort(arr, arr+sz);

        calculateNxt();
    }

    void insert(int v){
        if(arr[sz-1] <= v) arr[sz++] = v;
        else for(int i=0; i<sz; i++){
            if(v <= arr[i]){
                for(int j=sz-1; j>=i; j--) arr[j+1] = arr[j];
                arr[i] = v;
                sz++;
                break;
            }
        }

        calculateNxt();
    }

    void erase(int v){
        for(int i=0; i<sz; i++){
            if(arr[i] == v){
                for(int j=i+1; j<sz; j++) arr[j-1] = arr[j];
                arr[--sz] = 0;
                break;
            }
            if(i==sz-1) exit(1);
        }

        if(sz) calculateNxt();
    }
};

int n, k;
int arr[150002];
Group *groups[1002]; /// 0-base

void init(int N, int L, int X[]){
    n = N;
    len = L;
    for(int i=0; i<n; i++) arr[i] = X[i];
    for(int s=0; s<n; s++){
        int e = min(s+SZ_MIN-1, n-1);
        groups[k++] = new Group(arr, s, e);
        s = e;
    }
}

int update(int I, int y){
    /// 제거
    for(int i=0; i<k; i++){
        if(groups[i]->arr[0] <= arr[I] && arr[I] <= groups[i]->arr[groups[i]->sz-1]){
            groups[i]->erase(arr[I]);
            if(groups[i]->sz == 0){
                for(int j=i+1; j<k; j++) groups[j-1] = groups[j];
                --k;
                groups[k] = nullptr;
            }
            break;
        }
    }
    arr[I] = y;
    /// 삽입
    if(k==0){
        ++k;
        int X[1] = {y};
        groups[0] = new Group(X, 0, 0);
    }
    else{
        for(int i=0; i<k; i++){
            if(groups[i]->arr[groups[i]->sz-1] >= y || i==k-1){
                groups[i]->insert(y);
                if(groups[i]->sz >= SZ_MAX){
                    int X[SZ_MAX+2], len = groups[i]->sz;
                    for(int j=0; j<groups[i]->sz; j++) X[j] = groups[i]->arr[j];
                    for(int j=k-1; j>=i+1; j--) groups[j+1] = groups[j];
                    k++;
                    groups[i] = new Group(X, 0, SZ_MIN-1);
                    groups[i+1] = new Group(X, SZ_MIN, len-1);
                }
                break;
            }
        }
    }

    /// 계산
    int lastX = -1e9-1, c=0;
    for(int i=0; i<k; i++){
        if(groups[i]->arr[groups[i]->sz-1] - lastX <= len) continue;
        int x = upper_bound(groups[i]->arr, groups[i]->arr + groups[i]->sz, lastX + len) - groups[i]->arr;
        lastX = groups[i]->arr[groups[i]->nxt[x]];
        c += 1 + groups[i]->cnt[x];
    }

    return c;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 121 ms 3556 KB Output is correct
8 Correct 144 ms 3680 KB Output is correct
9 Correct 219 ms 2484 KB Output is correct
10 Correct 181 ms 4760 KB Output is correct
11 Correct 157 ms 4836 KB Output is correct
12 Correct 304 ms 3296 KB Output is correct
13 Correct 201 ms 4864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 121 ms 3556 KB Output is correct
8 Correct 144 ms 3680 KB Output is correct
9 Correct 219 ms 2484 KB Output is correct
10 Correct 181 ms 4760 KB Output is correct
11 Correct 157 ms 4836 KB Output is correct
12 Correct 304 ms 3296 KB Output is correct
13 Correct 201 ms 4864 KB Output is correct
14 Correct 136 ms 4300 KB Output is correct
15 Correct 220 ms 3300 KB Output is correct
16 Correct 498 ms 4532 KB Output is correct
17 Correct 533 ms 5920 KB Output is correct
18 Correct 541 ms 6032 KB Output is correct
19 Correct 327 ms 8772 KB Output is correct
20 Correct 519 ms 5860 KB Output is correct
21 Correct 510 ms 6588 KB Output is correct
22 Correct 330 ms 8140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 121 ms 3556 KB Output is correct
8 Correct 144 ms 3680 KB Output is correct
9 Correct 219 ms 2484 KB Output is correct
10 Correct 181 ms 4760 KB Output is correct
11 Correct 157 ms 4836 KB Output is correct
12 Correct 304 ms 3296 KB Output is correct
13 Correct 201 ms 4864 KB Output is correct
14 Correct 136 ms 4300 KB Output is correct
15 Correct 220 ms 3300 KB Output is correct
16 Correct 498 ms 4532 KB Output is correct
17 Correct 533 ms 5920 KB Output is correct
18 Correct 541 ms 6032 KB Output is correct
19 Correct 327 ms 8772 KB Output is correct
20 Correct 519 ms 5860 KB Output is correct
21 Correct 510 ms 6588 KB Output is correct
22 Correct 330 ms 8140 KB Output is correct
23 Correct 1637 ms 11960 KB Output is correct
24 Correct 1734 ms 11836 KB Output is correct
25 Correct 1282 ms 11812 KB Output is correct
26 Correct 1323 ms 18876 KB Output is correct
27 Correct 1364 ms 18728 KB Output is correct
28 Correct 651 ms 5312 KB Output is correct
29 Correct 661 ms 5396 KB Output is correct
30 Correct 658 ms 5400 KB Output is correct
31 Correct 664 ms 5508 KB Output is correct
32 Correct 990 ms 18304 KB Output is correct
33 Correct 892 ms 17660 KB Output is correct
34 Correct 1320 ms 18556 KB Output is correct
35 Correct 674 ms 18852 KB Output is correct
36 Correct 246 ms 11248 KB Output is correct
37 Correct 1458 ms 11684 KB Output is correct
38 Correct 1407 ms 17524 KB Output is correct
39 Correct 1527 ms 18536 KB Output is correct
40 Correct 1416 ms 17576 KB Output is correct
41 Correct 2016 ms 12132 KB Output is correct
42 Correct 2104 ms 12064 KB Output is correct