# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1132441 | steveonalex | Dancing Elephants (IOI11_elephants) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
// #include "elephants.h"
const int N = 150069;
const int BLOCK = 400;
int n, l;
int pos[N], group[N];
vector<pair<int, int>> nodes[N / BLOCK + 1];
vector<pair<int, int>> dp[N / BLOCK + 1];
int query_count = 0;
void construct_dp(int i){
sort(ALL(nodes[i]));
int sz = nodes[i].size();
dp[i].clear();
for(pair<int, int> j: nodes[i]){
dp[i].push_back({j.first + l + 1, 1});
}
for(int j = sz - 1; j >= 0; --j){
if (nodes[i].back().first >= dp[i][j].first){
pair<int, int> tmp = make_pair(dp[i][j].first, -1);
int idx = lower_bound(ALL(nodes[i]), tmp) - nodes[i].begin();
dp[i][j] = make_pair(dp[i][idx].first, dp[i][idx].second + 1);
}
}
}
void construct_from_pos(){
vector<pair<int, int>> val;
for(int i = 0; i < n; ++i) val.push_back(make_pair(pos[i], i));
sort(ALL(val));
for(int i = 0; i <= n / BLOCK; ++i) {
nodes[i].clear(); dp[i].clear();
}
for(int i = 0; i < n; ++i) {
group[val[i].second] = i / BLOCK;
nodes[i / BLOCK].push_back(val[i]);
}
for(int i = 0; i <= n / BLOCK; ++i) {
construct_dp(i);
}
}
void init(int N, int L, int X[]){
n = N, l = L;
for(int i = 0; i < n; ++i) pos[i] = X[i];
construct_from_pos();
}
int let_go(){
int j = 0;
int ans = 0;
for(int i = 0; i <= n / BLOCK; ++i) {
int idx = lower_bound(ALL(nodes[i]), make_pair(j, -1)) - nodes[i].begin();
if (idx == (int)nodes[i].size()) continue;
ans += dp[i][idx].second;
j = dp[i][idx].first;
}
return ans;
}
int update(int i, int y){
query_count++;
if (query_count == BLOCK){
query_count = 0;
construct_from_pos();
}
int g1 = group[i];
for(int j = 0; j < (int)nodes[g1].size(); ++j)
if (nodes[g1][j].second == i) {
nodes[g1].erase(nodes[g1].begin() + j);
break;
}
pos[i] = y;
int g2 = 0;
for(int j = 0; j <= n / BLOCK; ++j) if (nodes[j].size() && nodes[j][0].first <= pos[i])
g2 = j;
group[i] = g2;
nodes[g2].push_back(make_pair(pos[i], i));
construct_dp(g1);
construct_dp(g2);
return let_go();
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
int n, l; cin >> n >> l;
int x[n];
for(int i= 0; i < n; ++i) cin >> x[i];
init(n, l, x);
int q; cin >> q;
while(q--){
int i, y; cin >> i >> y;
cout << update(i, y) << "\n";
}
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}