답안 #941632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941632 2024-03-09T08:11:30 Z Nhoksocqt1 코끼리 (Dancing Elephants) (IOI11_elephants) C++17
100 / 100
5054 ms 26052 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 150005;
const int BLOCK = 390;

ii sorted_pos[MAXN], bel[MAXN];
int id[BLOCK][2 * BLOCK], dp[BLOCK][2 * BLOCK];
int pos_block[BLOCK][2 * BLOCK], nxt[BLOCK][2 * BLOCK];
int pos[MAXN], block_size[MAXN], nArr, numBlock, maxLen;


void build(void) {
    for (int i = 0; i < nArr; ++i)
        sorted_pos[i] = ii(pos[i], i);

    sort(sorted_pos, sorted_pos + nArr);

    numBlock = (nArr + BLOCK - 1) / BLOCK;
    for (int i = 0; i < numBlock; ++i) {
        block_size[i] = min(nArr, (i + 1) * BLOCK) - i * BLOCK;

        int k(block_size[i] - 1);
        for (int j = k; j >= 0; --j) {
            pos_block[i][j] = sorted_pos[i * BLOCK + j].fi;
            while(j < k && pos_block[i][j] + maxLen < pos_block[i][k - 1])
                --k;

            id[i][j] = sorted_pos[i * BLOCK + j].se;
            bel[sorted_pos[i * BLOCK + j].se] = {i, j};
            if(pos_block[i][j] + maxLen >= pos_block[i][block_size[i] - 1]) {
                nxt[i][j] = pos_block[i][j] + maxLen;
                dp[i][j] = 1;
            } else {
                nxt[i][j] = nxt[i][k];
                dp[i][j] = 1 + dp[i][k];
            }
        }
    }
}

void rebuildBlock(int i) {
    int k(block_size[i] - 1);
    for (int j = k; j >= 0; --j) {
        while(j + 1 < k && pos_block[i][j] + maxLen < pos_block[i][k - 1])
            --k;

        if(pos_block[i][j] + maxLen >= pos_block[i][block_size[i] - 1]) {
            nxt[i][j] = pos_block[i][j] + maxLen;
            dp[i][j] = 1;
        } else {
            nxt[i][j] = nxt[i][k];
            dp[i][j] = 1 + dp[i][k];
        }
    }
}

int query(void) {
    int res(0), lst(-1);
    for (int i = 0; i < numBlock; ++i) {
        int p = upper_bound(pos_block[i], pos_block[i] + block_size[i], lst) - pos_block[i];
        if(p < block_size[i]) {
            res += dp[i][p];
            lst = nxt[i][p];
        }
    }

    return res;
}

int update(int u, int newPos) {
    pos[u] = newPos;

    ii x(bel[u]);
    /*for (int j = 0; j < block_size[x.fi]; ++j)
        cout << pos_block[x.fi][j] << ' '; cout << '\n';

    for (int j = 0; j < block_size[x.fi]; ++j)
        cout << id[x.fi][j] << ' '; cout << '\n';

    for (int j = 0; j < block_size[x.fi]; ++j)
        cout << bel[id[x.fi][j]].se << ' '; cout << '\n';*/

    for (int i = x.se + 1; i < block_size[x.fi]; ++i) {
        pos_block[x.fi][i - 1] = pos_block[x.fi][i];
        id[x.fi][i - 1] = id[x.fi][i];
        --bel[id[x.fi][i - 1]].se;
    }

    --block_size[x.fi];
    rebuildBlock(x.fi);

    ii y = ii(numBlock - 1, block_size[numBlock - 1]);
    for (int i = 0; i < numBlock; ++i) {
        if(newPos <= pos_block[i][block_size[i] - 1]) {
            int j = upper_bound(pos_block[i], pos_block[i] + block_size[i], newPos) - pos_block[i];
            y = {i, j};
            break;
        }
    }

    assert(y.fi >= 0);
    for (int i = block_size[y.fi]; i > y.se; --i) {
        pos_block[y.fi][i] = pos_block[y.fi][i - 1];
        id[y.fi][i] = id[y.fi][i - 1];
        ++bel[id[y.fi][i]].se;
    }

    ++block_size[y.fi];
    pos_block[y.fi][y.se] = newPos;
    bel[u] = ii(y.fi, y.se);
    id[y.fi][y.se] = u;
    /*cout << y.fi << ' ' << y.se << ": ";
    for (int j = 0; j < block_size[y.fi]; ++j)
        cout << pos_block[x.fi][j] << ' '; cout << '\n';

    for (int j = 0; j < block_size[y.fi]; ++j)
        cout << id[x.fi][j] << ' '; cout << '\n';

    for (int j = 0; j < block_size[y.fi]; ++j)
        cout << bel[id[x.fi][j]].se << ' '; cout << '\n';*/

    rebuildBlock(y.fi);
    if(block_size[x.fi] == 0 || block_size[y.fi] >= 2 * BLOCK)
        build();

    return query();
}

void init(int _N, int _L, int _X[]) {
    nArr = _N, maxLen = _L;
    for (int i = 0; i < nArr; ++i)
        pos[i] = _X[i];

    build();
}

#ifdef Nhoksocqt1

int main(void) {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    #define TASK "ioi11_elephants"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    int *X, n, L;
    cin >> n >> L;
    X = new int[n + 1];
    for (int i = 0; i < n; ++i)
        cin >> X[i];

    init(n, L, X);

    int q;
    cin >> q;
    for (int t = 0; t < q; ++t) {
        int i, y;
        cin >> i >> y;
        cout << update(i, y) << '\n';
    }

    return 0;
}

#endif // Nhoksocqt1
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14800 KB Output is correct
7 Correct 219 ms 16728 KB Output is correct
8 Correct 238 ms 17756 KB Output is correct
9 Correct 261 ms 18388 KB Output is correct
10 Correct 371 ms 18104 KB Output is correct
11 Correct 368 ms 18104 KB Output is correct
12 Correct 375 ms 18256 KB Output is correct
13 Correct 394 ms 17960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14800 KB Output is correct
7 Correct 219 ms 16728 KB Output is correct
8 Correct 238 ms 17756 KB Output is correct
9 Correct 261 ms 18388 KB Output is correct
10 Correct 371 ms 18104 KB Output is correct
11 Correct 368 ms 18104 KB Output is correct
12 Correct 375 ms 18256 KB Output is correct
13 Correct 394 ms 17960 KB Output is correct
14 Correct 332 ms 18268 KB Output is correct
15 Correct 307 ms 18260 KB Output is correct
16 Correct 609 ms 18636 KB Output is correct
17 Correct 663 ms 18860 KB Output is correct
18 Correct 705 ms 18792 KB Output is correct
19 Correct 1189 ms 18996 KB Output is correct
20 Correct 641 ms 18856 KB Output is correct
21 Correct 624 ms 18828 KB Output is correct
22 Correct 739 ms 18516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 2 ms 14680 KB Output is correct
5 Correct 2 ms 14684 KB Output is correct
6 Correct 2 ms 14800 KB Output is correct
7 Correct 219 ms 16728 KB Output is correct
8 Correct 238 ms 17756 KB Output is correct
9 Correct 261 ms 18388 KB Output is correct
10 Correct 371 ms 18104 KB Output is correct
11 Correct 368 ms 18104 KB Output is correct
12 Correct 375 ms 18256 KB Output is correct
13 Correct 394 ms 17960 KB Output is correct
14 Correct 332 ms 18268 KB Output is correct
15 Correct 307 ms 18260 KB Output is correct
16 Correct 609 ms 18636 KB Output is correct
17 Correct 663 ms 18860 KB Output is correct
18 Correct 705 ms 18792 KB Output is correct
19 Correct 1189 ms 18996 KB Output is correct
20 Correct 641 ms 18856 KB Output is correct
21 Correct 624 ms 18828 KB Output is correct
22 Correct 739 ms 18516 KB Output is correct
23 Correct 1532 ms 25796 KB Output is correct
24 Correct 1641 ms 25936 KB Output is correct
25 Correct 1279 ms 26052 KB Output is correct
26 Correct 3337 ms 25800 KB Output is correct
27 Correct 5054 ms 25796 KB Output is correct
28 Correct 1031 ms 23872 KB Output is correct
29 Correct 930 ms 23876 KB Output is correct
30 Correct 1010 ms 23868 KB Output is correct
31 Correct 951 ms 23868 KB Output is correct
32 Correct 3013 ms 25288 KB Output is correct
33 Correct 3311 ms 24516 KB Output is correct
34 Correct 3131 ms 25544 KB Output is correct
35 Correct 3421 ms 25800 KB Output is correct
36 Correct 2005 ms 25292 KB Output is correct
37 Correct 1463 ms 25784 KB Output is correct
38 Correct 3172 ms 24524 KB Output is correct
39 Correct 4210 ms 25540 KB Output is correct
40 Correct 3102 ms 24520 KB Output is correct
41 Correct 2004 ms 25284 KB Output is correct
42 Correct 2052 ms 25776 KB Output is correct