This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "elephants.h"
#include <bits/stdc++.h>
#pragma gcc optimize("Ofast")
#define pb push_back
#define All(x) x.begin(), x.end()
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 150000;
const int INF = 1e9;
const int K = 387;
int n, m, seg;
int max_sze;
vector <pii> a;
vector <vector<pii>> B;
vector <pii> rng;
vector <int> pos;
vector <pii> info;
void build_block(int id, bool flag) {
int len = B[id].size();
if (!len) {
rng[id] = pii(id==0 ? -INF : rng[id-1].ss, id==m-1 ? INF : rng[id+1].ff);
return;
}
if (flag) {
for (int i = len-1; i >= 1; i--) {
if (B[id][i].ff < B[id][i-1].ff) {
swap(B[id][i], B[id][i-1]);
} else break;
}
}
int nowR = len-1;
for (int i = len-1; i >= 0; i--) {
pos[B[id][i].ss] = id;
if (B[id][i].ff + seg >= B[id][len-1].ff) {
info[B[id][i].ss].ff = 1;
info[B[id][i].ss].ss = B[id][i].ff + seg;
} else {
while (nowR && B[id][i].ff + seg < B[id][nowR-1].ff) nowR--;
info[B[id][i].ss].ff = info[B[id][nowR].ss].ff + 1;
info[B[id][i].ss].ss = info[B[id][nowR].ss].ss;
}
}
rng[id] = pii(B[id][0].ff, B[id][len-1].ff);
max_sze = max(max_sze, len);
}
void first_build() {
sort(All(a));
for (int i = 0; i < m; i++)
B[i].clear();
for (int i = 0; i < n; i++)
B[i/K].pb(a[i]);
for (int i = 0; i < m; i++)
build_block(i, false);
sort(All(a), [](const pii x, const pii y) {
return x.ss < y.ss;
});
}
void build() {
vector <pii> tmp(n);
int c = 0;
for (int i = 0; i < m; i++) {
for (pii p : B[i]) tmp[c++] = p;
B[i].clear();
}
for (int i = 0; i < n; i++)
B[i/K].pb(tmp[i]);
for (int i = 0; i < m; i++)
build_block(i, false);
}
int solve() {
int ans = 0, now = -1;
for (int i = 0; i < m; i++) {
if (!B[i].size() || B[i][B[i].size()-1].ff <= now) continue;
pii tmp = *upper_bound(All(B[i]), pii(now, n+1));
ans += info[tmp.ss].ff;
now = info[tmp.ss].ss;
}
return ans;
}
void init(int N, int L, int X[]) {
n = N, m = (n-1)/K+1, seg = L;
a.clear(), a.resize(n);
pos.clear(), pos.resize(n);
info.clear(), info.resize(n);
B.clear(), B.resize(m);
rng.clear(), rng.resize(m);
for (int i = 0; i < n; i++)
a[i].ff = X[i], a[i].ss = i;
first_build();
}
int update(int i, int y) {
a[i].ff = y;
if (max_sze > 5*K/2) max_sze = 0, build();
int id = pos[i];
for (int j = 0; j < (int)B[id].size(); j++) {
if (B[id][j].ss == i) {
B[id].erase(B[id].begin()+j);
break;
}
}
for (int j = 0; j < m; j++) {
if ((!j || rng[j-1].ss <= y) && (j==m-1 || rng[j+1].ff >= y)) {
B[j].pb(a[i]);
pos[i] = j;
build_block(j, a[i].ff < rng[j].ss);
if (id != j)
build_block(id, 0);
break;
}
}
return solve();
}
Compilation message (stderr)
elephants.cpp:3: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
3 | #pragma gcc optimize("Ofast")
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |