답안 #1118526

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1118526 2024-11-25T15:46:36 Z _callmelucian 코끼리 (Dancing Elephants) (IOI11_elephants) C++14
100 / 100
4073 ms 14132 KB
#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int blockSize = 800, resetCycle = 200, mn = 150'005;
int n, L, queryCount, save[mn];

struct block {
    vector<int> x, jumpTo, step;

    void refine (int pos) {
        int iter = upper_bound(all(x), x[pos] + L) - x.begin();
        for (int i = pos; i >= 0; i--) {
            while (iter - 1 >= 0 && x[iter - 1] - x[i] > L) iter--;
            if (iter == x.size()) jumpTo[i] = x[i] + L, step[i] = 1;
            else jumpTo[i] = jumpTo[iter], step[i] = step[iter] + 1;
        }
    }

    block (vector<int> x) : x(x), jumpTo(x.size()), step(x.size()) { refine((int)x.size() - 1); }

    void insert (int xP) {
        int pos = upper_bound(all(x), xP) - x.begin();
        x.insert(x.begin() + pos, xP);
        jumpTo.insert(jumpTo.begin() + pos, 0);
        step.insert(step.begin() + pos, 0);
        refine(pos);
    }

    void remove (int xP) {
        int pos = lower_bound(all(x), xP) - x.begin();
        x.erase(x.begin() + pos);
        jumpTo.erase(jumpTo.begin() + pos);
        step.erase(step.begin() + pos);
        refine(pos);
    }

    void batchJump (int &start, int &ans) {
        assert(x.size() < (1 << 10));
        if (x[0] > start) return start = jumpTo[0], ans += step[0], void();
        int pos = 0;
        for (int mask = 1 << 9; mask; mask >>= 1) {
            if ((pos | mask) >= x.size()) continue;
            if (x[pos | mask] <= start) pos |= mask;
        }
        pos++;
        if (pos < x.size()) start = jumpTo[pos], ans += step[pos];
    }

    int last() { return x.back(); }
};

vector<block> blocks;

void build (const vector<int> &x) {
    for (int i = 0; i < x.size(); i += blockSize) {
        vector<int> cur(x.begin() + i, x.begin() + min((int)x.size(), i + blockSize));
        blocks.emplace_back(cur);
    }
}

void reBuild() {
    vector<int> x;
    for (block &cur : blocks)
        for (int u : cur.x) x.push_back(u);
    blocks.clear(), build(x);
}

void insert (int xP) {
    for (block &cur : blocks)
        if (cur.last() >= xP) return cur.insert(xP), void();
    blocks[(int)blocks.size() - 1].insert(xP);
}

void remove (int xP) {
    for (block &cur : blocks)
        if (cur.last() >= xP) return cur.remove(xP), void();
}

int query() {
    int curPos = -1, ans = 0;
    for (block &cur : blocks) cur.batchJump(curPos, ans);
    return ans;
}

void init (int _n, int _L, int X[]) {
    n = _n, L = _L;
    vector<int> vec(n);
    for (int i = 0; i < n; i++) vec[i] = save[i] = X[i];
    build(vec);
}

int update (int i, int y) {
    if (queryCount && queryCount % resetCycle == 0) reBuild();
    queryCount++;
    remove(save[i]), insert(y), save[i] = y;
    return query();
}

Compilation message

elephants.cpp: In member function 'void block::refine(int)':
elephants.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |             if (iter == x.size()) jumpTo[i] = x[i] + L, step[i] = 1;
      |                 ~~~~~^~~~~~~~~~~
elephants.cpp: In member function 'void block::batchJump(int&, int&)':
elephants.cpp:52:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |             if ((pos | mask) >= x.size()) continue;
      |                 ~~~~~~~~~~~~~^~~~~~~~~~~
elephants.cpp:56:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         if (pos < x.size()) start = jumpTo[pos], ans += step[pos];
      |             ~~~~^~~~~~~~~~
elephants.cpp: In function 'void build(const std::vector<int>&)':
elephants.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for (int i = 0; i < x.size(); i += blockSize) {
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 352 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 352 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 155 ms 2352 KB Output is correct
8 Correct 181 ms 2620 KB Output is correct
9 Correct 302 ms 4092 KB Output is correct
10 Correct 312 ms 3864 KB Output is correct
11 Correct 281 ms 3804 KB Output is correct
12 Correct 618 ms 4360 KB Output is correct
13 Correct 374 ms 3636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 352 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 155 ms 2352 KB Output is correct
8 Correct 181 ms 2620 KB Output is correct
9 Correct 302 ms 4092 KB Output is correct
10 Correct 312 ms 3864 KB Output is correct
11 Correct 281 ms 3804 KB Output is correct
12 Correct 618 ms 4360 KB Output is correct
13 Correct 374 ms 3636 KB Output is correct
14 Correct 326 ms 3564 KB Output is correct
15 Correct 260 ms 3408 KB Output is correct
16 Correct 937 ms 5060 KB Output is correct
17 Correct 1082 ms 6204 KB Output is correct
18 Correct 1244 ms 6172 KB Output is correct
19 Correct 751 ms 5752 KB Output is correct
20 Correct 1066 ms 6240 KB Output is correct
21 Correct 1076 ms 6088 KB Output is correct
22 Correct 729 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 444 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 352 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 155 ms 2352 KB Output is correct
8 Correct 181 ms 2620 KB Output is correct
9 Correct 302 ms 4092 KB Output is correct
10 Correct 312 ms 3864 KB Output is correct
11 Correct 281 ms 3804 KB Output is correct
12 Correct 618 ms 4360 KB Output is correct
13 Correct 374 ms 3636 KB Output is correct
14 Correct 326 ms 3564 KB Output is correct
15 Correct 260 ms 3408 KB Output is correct
16 Correct 937 ms 5060 KB Output is correct
17 Correct 1082 ms 6204 KB Output is correct
18 Correct 1244 ms 6172 KB Output is correct
19 Correct 751 ms 5752 KB Output is correct
20 Correct 1066 ms 6240 KB Output is correct
21 Correct 1076 ms 6088 KB Output is correct
22 Correct 729 ms 5204 KB Output is correct
23 Correct 2682 ms 11912 KB Output is correct
24 Correct 3064 ms 11908 KB Output is correct
25 Correct 2259 ms 11940 KB Output is correct
26 Correct 3095 ms 11928 KB Output is correct
27 Correct 2414 ms 11808 KB Output is correct
28 Correct 880 ms 5364 KB Output is correct
29 Correct 811 ms 5208 KB Output is correct
30 Correct 849 ms 5144 KB Output is correct
31 Correct 820 ms 5372 KB Output is correct
32 Correct 2536 ms 11360 KB Output is correct
33 Correct 2082 ms 10732 KB Output is correct
34 Correct 2303 ms 11596 KB Output is correct
35 Correct 2180 ms 12076 KB Output is correct
36 Correct 2218 ms 11328 KB Output is correct
37 Correct 2863 ms 12532 KB Output is correct
38 Correct 3127 ms 10772 KB Output is correct
39 Correct 2784 ms 13500 KB Output is correct
40 Correct 3209 ms 12456 KB Output is correct
41 Correct 3869 ms 12480 KB Output is correct
42 Correct 4073 ms 14132 KB Output is correct