Submission #1274801

#TimeUsernameProblemLanguageResultExecution timeMemory
1274801JeanBombeurDancing Elephants (IOI11_elephants)C++17
100 / 100
1221 ms7272 KiB
#include <bits/stdc++.h>
#include "elephants.h"
#define all(a) (a).begin(), (a).end()
using namespace std;

const int N = 15e4;
const int SZ = 400;

vector <int> pos[3 * N / SZ];
vector <pair <int, int>> dp[3 * N / SZ];

int x[N];

int length, nb_blocks;

void compute(int id) {
    vector <int> &cur_pos = pos[id];
    vector <pair <int, int>> &cur_dp = dp[id];
    int sz = (int)size(cur_pos);
    cur_dp.resize(sz);
    for (int nxt = sz, i = sz - 1; ~i; i --) {
        while (cur_pos[nxt - 1] > cur_pos[i] + length)
            nxt --;
        cur_dp[i] = (nxt == sz) ? make_pair(1, cur_pos[i] + length) : make_pair(1 + cur_dp[nxt].first, cur_dp[nxt].second);
    }
    return;
}

void upd(int v, bool add) {
    int id = 0;
    while (id < nb_blocks - 1 && pos[id].back() < v)
        id ++;
    if (add)
        pos[id].insert(lower_bound(all(pos[id]), v), v);
    else
        pos[id].erase(lower_bound(all(pos[id]), v));
    if (pos[id].size() == 2 * SZ) {
        for (int i = nb_blocks ++; i > id + 1; i --) {
            swap(pos[i], pos[i - 1]);
            swap(dp[i], dp[i - 1]);
        }
        pos[id + 1].insert(pos[id + 1].begin(), pos[id].begin() + SZ, pos[id].end());
        pos[id].erase(pos[id].begin() + SZ, pos[id].end());
        compute(id + 1);
    }
    if (pos[id].empty()) {
        -- nb_blocks;
        for (int i = id; i < nb_blocks; i ++) {
            swap(pos[i], pos[i + 1]);
            swap(dp[i], dp[i + 1]);
        }
    }
    else
        compute(id);
    return;
}

int answer() {
    int ans = dp[0][0].first, cur = dp[0][0].second;
    for (int id = 1; id < nb_blocks; id ++) {
        if (pos[id].back() <= cur)
            continue;
        int i = upper_bound(all(pos[id]), cur) - pos[id].begin();
        ans += dp[id][i].first;
        cur = dp[id][i].second;
    }
    return ans;
}

void init(int N, int L, int X[]) {
    length = L;
    nb_blocks = (SZ - 1 + N) / SZ;
    for (int i = 0; i < N; i ++)
        pos[i / SZ].push_back(x[i] = X[i]);
    for (int i = 0; i < nb_blocks; i ++)
        compute(i);
    return;
}

int update(int id, int nv) {
    upd(x[id], 0);
    upd(x[id] = nv, 1);
    return answer();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...