Submission #270387

# Submission time Handle Problem Language Result Execution time Memory
270387 2020-08-17T14:40:50 Z PeppaPig Dancing Elephants (IOI11_elephants) C++14
100 / 100
6773 ms 17540 KB
#include "elephants.h"
#include <bits/stdc++.h>

#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 1.5e5 + 5;
const int B = 390;

int n, k, bcnt;
int R[B + 5], pos[N];
map<int, int> mp;
vector<int> jump[B + 5], cnt[B + 5], bucket[B + 5];

void init(int _n, int _k, int X[]) {
    n = _n, k = _k;
    for(int i = 0; i < n; i++) ++mp[pos[i] = X[i]];
}

void solve_bucket(int idx) {
    vector<int> &vec = bucket[idx];

    jump[idx] = vector<int>(vec.size()), cnt[idx] = vector<int>(vec.size());
    for(int i = vec.size() - 1, j = vec.size(); ~i; i--) {
        while(vec[j - 1] > vec[i] + k) --j;
        if(j < vec.size()) jump[idx][i] = jump[idx][j], cnt[idx][i] = cnt[idx][j] + 1;
        else jump[idx][i] = vec[i] + k, cnt[idx][i] = 1;
    }
}

void revalidate() {
    bcnt = 0;
    for(pii p : mp) {
        if(!bcnt || bucket[bcnt - 1].size() >= B)
            bucket[bcnt++].clear();
        bucket[bcnt - 1].emplace_back(p.x);
    }
    for(int i = 0; i < bcnt; i++) {
        if(i + 1 < bcnt) R[i] = bucket[i + 1][0] - 1;
        else R[i] = 2e9;

        solve_bucket(i);
    } 
}

void add_elephant(int x, bool b) {
    int ptr = -1;
    while(R[++ptr] < x);
    int idx = lower_bound(bucket[ptr].begin(), bucket[ptr].end(), x) - bucket[ptr].begin();

    if(b) bucket[ptr].insert(bucket[ptr].begin() + idx, x);
    else bucket[ptr].erase(bucket[ptr].begin() + idx);

    solve_bucket(ptr);
}

int counter;

int update(int i, int y) {
    if(counter++ % B == 0) revalidate();

    if(!--mp[pos[i]]) add_elephant(pos[i], 0), mp.erase(pos[i]);
    if(!mp[y]) add_elephant(y, 1);
    ++mp[y], pos[i] = y;

    int answer = 0, cur = -1;
    for(int i = 0; i < bcnt; i++) {
        int idx = upper_bound(bucket[i].begin(), bucket[i].end(), cur) - bucket[i].begin();
        if(idx < bucket[i].size())
            answer += cnt[i][idx], cur = jump[i][idx];
    }

    return answer;
}

Compilation message

elephants.cpp: In function 'void solve_bucket(int)':
elephants.cpp:29:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |         if(j < vec.size()) jump[idx][i] = jump[idx][j], cnt[idx][i] = cnt[idx][j] + 1;
      |            ~~^~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:72:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         if(idx < bucket[i].size())
      |            ~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 282 ms 1784 KB Output is correct
8 Correct 356 ms 3304 KB Output is correct
9 Correct 568 ms 5976 KB Output is correct
10 Correct 498 ms 5880 KB Output is correct
11 Correct 475 ms 5752 KB Output is correct
12 Correct 900 ms 6008 KB Output is correct
13 Correct 456 ms 5552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 282 ms 1784 KB Output is correct
8 Correct 356 ms 3304 KB Output is correct
9 Correct 568 ms 5976 KB Output is correct
10 Correct 498 ms 5880 KB Output is correct
11 Correct 475 ms 5752 KB Output is correct
12 Correct 900 ms 6008 KB Output is correct
13 Correct 456 ms 5552 KB Output is correct
14 Correct 264 ms 3948 KB Output is correct
15 Correct 549 ms 4368 KB Output is correct
16 Correct 1178 ms 6520 KB Output is correct
17 Correct 1381 ms 8124 KB Output is correct
18 Correct 1517 ms 8160 KB Output is correct
19 Correct 840 ms 8332 KB Output is correct
20 Correct 1455 ms 8044 KB Output is correct
21 Correct 1593 ms 8076 KB Output is correct
22 Correct 805 ms 7544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 282 ms 1784 KB Output is correct
8 Correct 356 ms 3304 KB Output is correct
9 Correct 568 ms 5976 KB Output is correct
10 Correct 498 ms 5880 KB Output is correct
11 Correct 475 ms 5752 KB Output is correct
12 Correct 900 ms 6008 KB Output is correct
13 Correct 456 ms 5552 KB Output is correct
14 Correct 264 ms 3948 KB Output is correct
15 Correct 549 ms 4368 KB Output is correct
16 Correct 1178 ms 6520 KB Output is correct
17 Correct 1381 ms 8124 KB Output is correct
18 Correct 1517 ms 8160 KB Output is correct
19 Correct 840 ms 8332 KB Output is correct
20 Correct 1455 ms 8044 KB Output is correct
21 Correct 1593 ms 8076 KB Output is correct
22 Correct 805 ms 7544 KB Output is correct
23 Correct 4696 ms 17500 KB Output is correct
24 Correct 4003 ms 17540 KB Output is correct
25 Correct 3570 ms 17408 KB Output is correct
26 Correct 3994 ms 17528 KB Output is correct
27 Correct 4220 ms 17336 KB Output is correct
28 Correct 1210 ms 5756 KB Output is correct
29 Correct 1181 ms 5624 KB Output is correct
30 Correct 1272 ms 5632 KB Output is correct
31 Correct 1176 ms 5624 KB Output is correct
32 Correct 3676 ms 16892 KB Output is correct
33 Correct 1733 ms 16384 KB Output is correct
34 Correct 3658 ms 17144 KB Output is correct
35 Correct 1604 ms 17400 KB Output is correct
36 Correct 86 ms 7800 KB Output is correct
37 Correct 2410 ms 17272 KB Output is correct
38 Correct 3579 ms 16120 KB Output is correct
39 Correct 3404 ms 17092 KB Output is correct
40 Correct 3656 ms 16120 KB Output is correct
41 Correct 6773 ms 16968 KB Output is correct
42 Correct 6346 ms 17188 KB Output is correct