Submission #413024

# Submission time Handle Problem Language Result Execution time Memory
413024 2021-05-28T05:09:12 Z ja_kingy Dancing Elephants (IOI11_elephants) C++14
97 / 100
2460 ms 16244 KB
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;

const int SQRT = 500, mxN = 150000;
int n, x[mxN+1], l, req[mxN], nx[mxN], blk[mxN], ucnt;
vector<int> blocks[SQRT];

void build_block(int b) {
    int ne = blocks[b].size();
    for (int i = blocks[b].size(); i--;) {
        while (x[blocks[b][ne-1]] > x[blocks[b][i]] + l) ne--;
        req[blocks[b][i]] = ne == blocks[b].size() ? 1 : req[blocks[b][ne]] + 1;
        nx[blocks[b][i]] = ne == blocks[b].size() ? x[blocks[b][i]] + l + 1 : nx[blocks[b][ne]];
    }
}

void rebuild() {
    for (int i = 0; i < (n+SQRT-1)/SQRT; ++i) {
        blocks[i].clear();
    }
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int a, int b){return x[a] < x[b];});
    int b = 0;
    for (int i = 0; i < n; ++i) {
        blocks[b].push_back(order[i]);
        blk[order[i]] = b;
        if (blocks[b].size() == SQRT || i == n-1) {
            build_block(b);
            b++;
        }
    }
}

void init(int N, int L, int X[]) {
    n = N;
    l = L;
    copy(X, X+n, x);
    rebuild();
}

int update(int i, int y) {
    ucnt++;
    if (ucnt == SQRT) {
        ucnt = 0;
        rebuild();
    }
    blocks[blk[i]].erase(find(blocks[blk[i]].begin(), blocks[blk[i]].end(), i));
    build_block(blk[i]);
    x[i] = y;
    for (int b = 0; b < (n+SQRT-1)/SQRT; ++b) {
        if (b == (n+SQRT-1)/SQRT-1 || blocks[b].size() && x[blocks[b].back()] >= y) {
            blk[i] = b;
            blocks[b].insert(lower_bound(blocks[b].begin(), blocks[b].end(), i, [&](int a, int b){return x[a] < x[b];}), i);
            build_block(b);
            break;                        
        }
    }
    int ans = 0;
    x[n+1] = 0;
    for (int b = 0; b < (n+SQRT-1)/SQRT; ++b) {
        auto it = lower_bound(blocks[b].begin(), blocks[b].end(), n+1, [&](int a, int b){return x[a] < x[b];});
        if (it != blocks[b].end()) {
            ans += req[*it];
            x[n+1] = nx[*it];
        }
    }
    return ans;
}

Compilation message

elephants.cpp: In function 'void build_block(int)':
elephants.cpp:13:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |         req[blocks[b][i]] = ne == blocks[b].size() ? 1 : req[blocks[b][ne]] + 1;
      |                             ~~~^~~~~~~~~~~~~~~~~~~
elephants.cpp:14:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |         nx[blocks[b][i]] = ne == blocks[b].size() ? x[blocks[b][i]] + l + 1 : nx[blocks[b][ne]];
      |                            ~~~^~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:53:56: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   53 |         if (b == (n+SQRT-1)/SQRT-1 || blocks[b].size() && x[blocks[b].back()] >= y) {
      |                                       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 491 ms 1328 KB Output is correct
8 Correct 530 ms 2496 KB Output is correct
9 Correct 700 ms 3876 KB Output is correct
10 Correct 885 ms 3648 KB Output is correct
11 Correct 865 ms 3592 KB Output is correct
12 Correct 1348 ms 3900 KB Output is correct
13 Correct 862 ms 3464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 491 ms 1328 KB Output is correct
8 Correct 530 ms 2496 KB Output is correct
9 Correct 700 ms 3876 KB Output is correct
10 Correct 885 ms 3648 KB Output is correct
11 Correct 865 ms 3592 KB Output is correct
12 Correct 1348 ms 3900 KB Output is correct
13 Correct 862 ms 3464 KB Output is correct
14 Correct 775 ms 3252 KB Output is correct
15 Correct 798 ms 3320 KB Output is correct
16 Correct 2082 ms 4364 KB Output is correct
17 Correct 2235 ms 5272 KB Output is correct
18 Correct 2460 ms 5300 KB Output is correct
19 Correct 1519 ms 5264 KB Output is correct
20 Correct 2302 ms 5280 KB Output is correct
21 Correct 2244 ms 5404 KB Output is correct
22 Correct 1519 ms 4696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 491 ms 1328 KB Output is correct
8 Correct 530 ms 2496 KB Output is correct
9 Correct 700 ms 3876 KB Output is correct
10 Correct 885 ms 3648 KB Output is correct
11 Correct 865 ms 3592 KB Output is correct
12 Correct 1348 ms 3900 KB Output is correct
13 Correct 862 ms 3464 KB Output is correct
14 Correct 775 ms 3252 KB Output is correct
15 Correct 798 ms 3320 KB Output is correct
16 Correct 2082 ms 4364 KB Output is correct
17 Correct 2235 ms 5272 KB Output is correct
18 Correct 2460 ms 5300 KB Output is correct
19 Correct 1519 ms 5264 KB Output is correct
20 Correct 2302 ms 5280 KB Output is correct
21 Correct 2244 ms 5404 KB Output is correct
22 Correct 1519 ms 4696 KB Output is correct
23 Runtime error 88 ms 16244 KB Execution killed with signal 11
24 Halted 0 ms 0 KB -