#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 = 390;
int n, m, seg;
int cnt;
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) sort(All(B[id]));
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);
}
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[]) {
cnt = 0;
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) {
cnt++;
a[i].ff = y;
if (cnt % (K-190) == 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;
}
}
build_block(id, false);
vector <int> sav;
for (int j = 0; j < m; j++) {
if ((!j || rng[j-1].ss <= y) && (j==m-1 || rng[j+1].ff >= y)) {
sav.pb(j);
}
}
sort(All(sav), [&](const int x, const int y) {
return B[x].size() < B[y].size();
});
B[sav[0]].pb(a[i]);
pos[i] = sav[0];
build_block(sav[0], a[i].ff < rng[sav[0]].ss);
return solve();
}
Compilation message
elephants.cpp:3: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
3 | #pragma gcc optimize("Ofast")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1088 ms |
1512 KB |
Output is correct |
8 |
Correct |
1143 ms |
1728 KB |
Output is correct |
9 |
Correct |
1110 ms |
3076 KB |
Output is correct |
10 |
Correct |
546 ms |
3168 KB |
Output is correct |
11 |
Correct |
538 ms |
3052 KB |
Output is correct |
12 |
Correct |
1460 ms |
3216 KB |
Output is correct |
13 |
Correct |
635 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1088 ms |
1512 KB |
Output is correct |
8 |
Correct |
1143 ms |
1728 KB |
Output is correct |
9 |
Correct |
1110 ms |
3076 KB |
Output is correct |
10 |
Correct |
546 ms |
3168 KB |
Output is correct |
11 |
Correct |
538 ms |
3052 KB |
Output is correct |
12 |
Correct |
1460 ms |
3216 KB |
Output is correct |
13 |
Correct |
635 ms |
3052 KB |
Output is correct |
14 |
Correct |
1131 ms |
2016 KB |
Output is correct |
15 |
Correct |
1675 ms |
2200 KB |
Output is correct |
16 |
Correct |
2312 ms |
3424 KB |
Output is correct |
17 |
Correct |
2405 ms |
4280 KB |
Output is correct |
18 |
Correct |
2543 ms |
4164 KB |
Output is correct |
19 |
Correct |
2497 ms |
4160 KB |
Output is correct |
20 |
Correct |
2355 ms |
4164 KB |
Output is correct |
21 |
Correct |
2332 ms |
4272 KB |
Output is correct |
22 |
Correct |
1184 ms |
4296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1088 ms |
1512 KB |
Output is correct |
8 |
Correct |
1143 ms |
1728 KB |
Output is correct |
9 |
Correct |
1110 ms |
3076 KB |
Output is correct |
10 |
Correct |
546 ms |
3168 KB |
Output is correct |
11 |
Correct |
538 ms |
3052 KB |
Output is correct |
12 |
Correct |
1460 ms |
3216 KB |
Output is correct |
13 |
Correct |
635 ms |
3052 KB |
Output is correct |
14 |
Correct |
1131 ms |
2016 KB |
Output is correct |
15 |
Correct |
1675 ms |
2200 KB |
Output is correct |
16 |
Correct |
2312 ms |
3424 KB |
Output is correct |
17 |
Correct |
2405 ms |
4280 KB |
Output is correct |
18 |
Correct |
2543 ms |
4164 KB |
Output is correct |
19 |
Correct |
2497 ms |
4160 KB |
Output is correct |
20 |
Correct |
2355 ms |
4164 KB |
Output is correct |
21 |
Correct |
2332 ms |
4272 KB |
Output is correct |
22 |
Correct |
1184 ms |
4296 KB |
Output is correct |
23 |
Correct |
6536 ms |
8532 KB |
Output is correct |
24 |
Correct |
7229 ms |
8548 KB |
Output is correct |
25 |
Correct |
5561 ms |
8508 KB |
Output is correct |
26 |
Correct |
5482 ms |
8532 KB |
Output is correct |
27 |
Correct |
8608 ms |
8440 KB |
Output is correct |
28 |
Correct |
3555 ms |
5284 KB |
Output is correct |
29 |
Correct |
3543 ms |
5292 KB |
Output is correct |
30 |
Correct |
3541 ms |
5356 KB |
Output is correct |
31 |
Correct |
3553 ms |
5228 KB |
Output is correct |
32 |
Correct |
3746 ms |
12876 KB |
Output is correct |
33 |
Correct |
3338 ms |
12148 KB |
Output is correct |
34 |
Correct |
4027 ms |
13084 KB |
Output is correct |
35 |
Correct |
3915 ms |
13420 KB |
Output is correct |
36 |
Correct |
4528 ms |
12780 KB |
Output is correct |
37 |
Correct |
7442 ms |
14560 KB |
Output is correct |
38 |
Correct |
5113 ms |
12088 KB |
Output is correct |
39 |
Correct |
7240 ms |
13124 KB |
Output is correct |
40 |
Correct |
5413 ms |
12116 KB |
Output is correct |
41 |
Correct |
8013 ms |
12884 KB |
Output is correct |
42 |
Correct |
8061 ms |
13164 KB |
Output is correct |