답안 #1106339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106339 2024-10-30T02:53:24 Z Zero_OP 코끼리 (Dancing Elephants) (IOI11_elephants) C++14
100 / 100
4206 ms 24908 KB
#include <bits/stdc++.h>
 
#ifndef LOCAL
    #include "elephants.h"
#endif // LOCAL
 
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
 
using namespace std;
 
const int MAX = 150000;
int magic = sqrt(MAX) + 10;
const int maxMagic = 500;
 
int N, L, x[MAX], tmp[MAX], nBlocks, countUpdates = 0;
vector<int> blocks[maxMagic];
pair<int, int> id[MAX];
int original[MAX];
vector<pair<int, int>> infoBlocks[MAX]; //[n_jumps, last_jump]
 
void buildBlock(int ib){
    infoBlocks[ib].resize(sz(blocks[ib]));
    for(int i = sz(blocks[ib]) - 1; i >= 0; --i){
        if(i == sz(blocks[ib]) - 1){
            infoBlocks[ib][i] = {1, blocks[ib][i] + L};
        } else{
            int l = i + 1, r = sz(blocks[ib]) - 1;
            int n_jumps = 1, last_jump = blocks[ib][i] + L;
            while(l <= r){
                int mid = l + r >> 1;
                if(blocks[ib][i] + L < blocks[ib][mid]){
                    n_jumps = infoBlocks[ib][mid].first + 1;
                    last_jump = infoBlocks[ib][mid].second;
                    r = mid - 1;
                } else l = mid + 1;
            }
 
            infoBlocks[ib][i] = {n_jumps, last_jump};
        }
    }
}
 
int simulate(){
    int total = 0, cur = -1;
 
    for(int i = 0; i < nBlocks; ++i){
        if(blocks[i].empty() || blocks[i].back() <= cur) {
            continue; //over
        }
        int pos = upper_bound(all(blocks[i]), cur) - blocks[i].begin();
        total += infoBlocks[i][pos].first;
        cur = infoBlocks[i][pos].second;
    }
 
    return total;
}
 
void init(int _N, int _L, int _X[]){
    N = _N;
    L = _L;
    for(int i = 0; i < N; ++i) x[i] = _X[i];
 
    //tested for magic exactly sqrt(n)
    magic = 500;
    nBlocks = 0;
 
    iota(original, original + N, 0);
    for(int i = 0; i < N; i += magic, ++nBlocks){
        int j = min(i + magic, N) - 1;
        blocks[nBlocks] = vector<int>(x + i, x + j + 1);
        buildBlock(nBlocks);
    }
}
 
void debugBlock(int i){
    cout << "block : " << i << '\n';
    for(int x : blocks[i]) cout << x << ' ';
    cout << '\n';
}
 
void debugAllBlock(){
    for(int i = 0; i < nBlocks; ++i){
        debugBlock(i);
    }
}
 
void rebuild(){
    copy(x, x + N, tmp);
    sort(tmp, tmp + N);
 
    nBlocks = 0;
    for(int i = 0; i < N; i += magic, ++nBlocks){
        int j = min(i + magic, N) - 1;
        blocks[nBlocks] = vector<int>(tmp + i, tmp + j + 1);
        buildBlock(nBlocks);
    }
}
 
void remove(int x){
    for(int i = 0; i < nBlocks; ++i){
        if(blocks[i].empty()) continue;
        if(!binary_search(all(blocks[i]), x)) continue;
        blocks[i].erase(find(all(blocks[i]), x));
        buildBlock(i);
        return;
    }
 
    assert(false);
}
 
void insert(int x){
    for(int i = 0; i < nBlocks; ++i){
        if(blocks[i].empty()) continue;
        if(blocks[i].back() > x){
            int pos = upper_bound(all(blocks[i]), x) - blocks[i].begin();
            blocks[i].insert(blocks[i].begin() + pos, x);
            buildBlock(i);
            return;
        }
    }
 
    blocks[nBlocks - 1].push_back(x);
    buildBlock(nBlocks - 1);
}
 
int update(int i, int y){
    ++countUpdates;
 
    if(countUpdates == 2 * magic){
        x[i] = y;
        rebuild();
        countUpdates = 0;
    }
 
    remove(x[i]);
    x[i] = y;
    insert(x[i]);
 
    return simulate();
}
 
#ifdef LOCAL
    int main(){
        ios_base::sync_with_stdio(0); cin.tie(0);
 
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
 
        int N, L;
        cin >> N >> L;
        int X[N];
        for(int i = 0; i < N; ++i) cin >> X[i];
 
        init(N, L, X);
        int Q; cin >> Q;
        while(Q--){
            int i, y;
            cin >> i >> y;
            cout << update(i, y) << '\n';
        }
 
        return 0;
    }
#endif // LOCAL

Compilation message

elephants.cpp: In function 'void buildBlock(int)':
elephants.cpp:32:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |                 int mid = l + r >> 1;
      |                           ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14928 KB Output is correct
2 Correct 2 ms 14928 KB Output is correct
3 Correct 2 ms 14928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14928 KB Output is correct
2 Correct 2 ms 14928 KB Output is correct
3 Correct 2 ms 14928 KB Output is correct
4 Correct 2 ms 14928 KB Output is correct
5 Correct 2 ms 15116 KB Output is correct
6 Correct 2 ms 14928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14928 KB Output is correct
2 Correct 2 ms 14928 KB Output is correct
3 Correct 2 ms 14928 KB Output is correct
4 Correct 2 ms 14928 KB Output is correct
5 Correct 2 ms 15116 KB Output is correct
6 Correct 2 ms 14928 KB Output is correct
7 Correct 706 ms 17452 KB Output is correct
8 Correct 715 ms 17480 KB Output is correct
9 Correct 487 ms 17736 KB Output is correct
10 Correct 647 ms 17488 KB Output is correct
11 Correct 622 ms 17648 KB Output is correct
12 Correct 787 ms 17992 KB Output is correct
13 Correct 671 ms 17644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14928 KB Output is correct
2 Correct 2 ms 14928 KB Output is correct
3 Correct 2 ms 14928 KB Output is correct
4 Correct 2 ms 14928 KB Output is correct
5 Correct 2 ms 15116 KB Output is correct
6 Correct 2 ms 14928 KB Output is correct
7 Correct 706 ms 17452 KB Output is correct
8 Correct 715 ms 17480 KB Output is correct
9 Correct 487 ms 17736 KB Output is correct
10 Correct 647 ms 17488 KB Output is correct
11 Correct 622 ms 17648 KB Output is correct
12 Correct 787 ms 17992 KB Output is correct
13 Correct 671 ms 17644 KB Output is correct
14 Correct 643 ms 19508 KB Output is correct
15 Correct 723 ms 19532 KB Output is correct
16 Correct 1286 ms 20296 KB Output is correct
17 Correct 1346 ms 20768 KB Output is correct
18 Correct 1367 ms 20804 KB Output is correct
19 Correct 1014 ms 19792 KB Output is correct
20 Correct 1347 ms 20668 KB Output is correct
21 Correct 1240 ms 20756 KB Output is correct
22 Correct 1028 ms 19792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14928 KB Output is correct
2 Correct 2 ms 14928 KB Output is correct
3 Correct 2 ms 14928 KB Output is correct
4 Correct 2 ms 14928 KB Output is correct
5 Correct 2 ms 15116 KB Output is correct
6 Correct 2 ms 14928 KB Output is correct
7 Correct 706 ms 17452 KB Output is correct
8 Correct 715 ms 17480 KB Output is correct
9 Correct 487 ms 17736 KB Output is correct
10 Correct 647 ms 17488 KB Output is correct
11 Correct 622 ms 17648 KB Output is correct
12 Correct 787 ms 17992 KB Output is correct
13 Correct 671 ms 17644 KB Output is correct
14 Correct 643 ms 19508 KB Output is correct
15 Correct 723 ms 19532 KB Output is correct
16 Correct 1286 ms 20296 KB Output is correct
17 Correct 1346 ms 20768 KB Output is correct
18 Correct 1367 ms 20804 KB Output is correct
19 Correct 1014 ms 19792 KB Output is correct
20 Correct 1347 ms 20668 KB Output is correct
21 Correct 1240 ms 20756 KB Output is correct
22 Correct 1028 ms 19792 KB Output is correct
23 Correct 2801 ms 23688 KB Output is correct
24 Correct 2929 ms 23744 KB Output is correct
25 Correct 2152 ms 23428 KB Output is correct
26 Correct 3115 ms 22936 KB Output is correct
27 Correct 3457 ms 22932 KB Output is correct
28 Correct 1732 ms 19188 KB Output is correct
29 Correct 1866 ms 19024 KB Output is correct
30 Correct 1775 ms 19188 KB Output is correct
31 Correct 1857 ms 19192 KB Output is correct
32 Correct 2629 ms 22928 KB Output is correct
33 Correct 2488 ms 22928 KB Output is correct
34 Correct 2777 ms 22932 KB Output is correct
35 Correct 2085 ms 22940 KB Output is correct
36 Correct 1553 ms 22932 KB Output is correct
37 Correct 3560 ms 24672 KB Output is correct
38 Correct 2904 ms 22928 KB Output is correct
39 Correct 3094 ms 22932 KB Output is correct
40 Correct 2836 ms 22932 KB Output is correct
41 Correct 4144 ms 24908 KB Output is correct
42 Correct 4206 ms 24732 KB Output is correct