Submission #542508

# Submission time Handle Problem Language Result Execution time Memory
542508 2022-03-26T19:40:43 Z tabr Dancing Elephants (IOI11_elephants) C++17
97 / 100
9000 ms 8296 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

const int MAX_N = 150015;
const int MAX_B = 810;
int B;
int n;
int l;
int upd_cnt;
int x[MAX_N];
pair<int, int> pos[MAX_N];
int jump_cnt[MAX_N];
int jump_pos[MAX_N];
vector<int> e[MAX_B];

void init(int n_, int l_, int x_[]) {
    n = n_;
    l = l_;
    B = (n <= 1000 ? (int) sqrt(n) : 400);
    for (int i = 0; i < n; i++) {
        x[i] = x_[i];
    }
}

void build_one(int id) {
    assert(!e[id].empty());
    sort(e[id].begin(), e[id].end(), [&](int i, int j) {
        return make_pair(x[i], i) < make_pair(x[j], j);
    });
    for (int i = 0; i < (int) e[id].size(); i++) {
        pos[e[id][i]] = make_pair(id, i);
    }
    for (int i = (int) e[id].size() - 1, j = (int) e[id].size(); i >= 0; i--) {
        while (j > 0 && x[e[id][j - 1]] > x[e[id][i]] + l) {
            j--;
        }
        if (j == (int) e[id].size()) {
            jump_cnt[e[id][i]] = 1;
            jump_pos[e[id][i]] = x[e[id][i]] + l + 1;
        } else {
            jump_cnt[e[id][i]] = jump_cnt[e[id][j]] + 1;
            jump_pos[e[id][i]] = jump_pos[e[id][j]];
        }
    }
}

void build() {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) {
        return make_pair(x[i], i) < make_pair(x[j], j);
    });
    for (int i = 0; i < B; i++) {
        e[i].clear();
        for (int j = n * i / B; j < n * (i + 1) / B; j++) {
            e[i].emplace_back(order[j]);
        }
        build_one(i);
    }
}

int update(int i, int y) {
    if (n == 1) {
        return 1;
    }
    if (upd_cnt == 0) {
        build();
    }
    upd_cnt++;
    if (upd_cnt == n / B - 1) {
        upd_cnt = 0;
    }
    // remove x[i]
    e[pos[i].first].erase(e[pos[i].first].begin() + pos[i].second);
    build_one(pos[i].first);
    // add x[i]
    x[i] = y;
    for (int id = 0; id < B; id++) {
        if (id == B - 1 || x[e[id + 1][0]] > y) {
            e[id].emplace_back(i);
            build_one(id);
            break;
        }
    }
    // answer
    int ans = 0;
    int now = 0;
    for (int id = 0; id < B; id++) {
        x[150001] = now;
        auto iter = lower_bound(e[id].begin(), e[id].end(), 150001, [&](int j, int k) {
            return x[j] < x[k];
        });
        if (iter == e[id].end()) {
            continue;
        }
        ans += jump_cnt[*iter];
        now = jump_pos[*iter];
    }
    return ans;
}

#ifdef tabr
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());

int rand_int(int a, int b) {  // [a, b]
    return uniform_int_distribution<int>(a, b)(rng);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n_ = 70000;
    int l_ = 100000;
    int x_[150000];
    for (int i = 0; i < n_; i++) {
        x_[i] = rand_int(0, (int) 1e9);
    }
    auto start_time = clock();
    init(n_, l_, x_);
    for (int i = 0; i < n_; i++) {
        update(rand_int(0, n - 1), rand_int(0, (int) 1e9));
    }
    auto end_time = clock();
    cerr << end_time - start_time << endl;
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1211 ms 1388 KB Output is correct
8 Correct 1329 ms 1528 KB Output is correct
9 Correct 1472 ms 2512 KB Output is correct
10 Correct 1868 ms 2564 KB Output is correct
11 Correct 1897 ms 2516 KB Output is correct
12 Correct 2996 ms 2520 KB Output is correct
13 Correct 1900 ms 2524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1211 ms 1388 KB Output is correct
8 Correct 1329 ms 1528 KB Output is correct
9 Correct 1472 ms 2512 KB Output is correct
10 Correct 1868 ms 2564 KB Output is correct
11 Correct 1897 ms 2516 KB Output is correct
12 Correct 2996 ms 2520 KB Output is correct
13 Correct 1900 ms 2524 KB Output is correct
14 Correct 2906 ms 1832 KB Output is correct
15 Correct 1940 ms 2016 KB Output is correct
16 Correct 4331 ms 2896 KB Output is correct
17 Correct 4606 ms 3496 KB Output is correct
18 Correct 4858 ms 3488 KB Output is correct
19 Correct 3367 ms 3492 KB Output is correct
20 Correct 4576 ms 3496 KB Output is correct
21 Correct 4588 ms 3488 KB Output is correct
22 Correct 2892 ms 3520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1211 ms 1388 KB Output is correct
8 Correct 1329 ms 1528 KB Output is correct
9 Correct 1472 ms 2512 KB Output is correct
10 Correct 1868 ms 2564 KB Output is correct
11 Correct 1897 ms 2516 KB Output is correct
12 Correct 2996 ms 2520 KB Output is correct
13 Correct 1900 ms 2524 KB Output is correct
14 Correct 2906 ms 1832 KB Output is correct
15 Correct 1940 ms 2016 KB Output is correct
16 Correct 4331 ms 2896 KB Output is correct
17 Correct 4606 ms 3496 KB Output is correct
18 Correct 4858 ms 3488 KB Output is correct
19 Correct 3367 ms 3492 KB Output is correct
20 Correct 4576 ms 3496 KB Output is correct
21 Correct 4588 ms 3488 KB Output is correct
22 Correct 2892 ms 3520 KB Output is correct
23 Correct 7920 ms 7008 KB Output is correct
24 Correct 8485 ms 7008 KB Output is correct
25 Correct 6978 ms 7008 KB Output is correct
26 Correct 8222 ms 6976 KB Output is correct
27 Execution timed out 9057 ms 8296 KB Time limit exceeded
28 Halted 0 ms 0 KB -