#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
#define foreach(it, x) for(type(x) it = x.begin(); it != x.end(); it++)
#define type(x) __typeof(x.begin())
#define mp make_pair
#define pb push_back
#define nd second
#define st first
#define next sadas
const int inf = 1e9 + 5;
const int N = 2e5 + 5;
const int SQ = 300;
typedef pair< int , int > pii;
typedef long long ll;
int n, m, x, y, z, t, S, xx[N], wh[N];
pii next[N];
vector< pii > v[N];
set< pii > SS;
pii take(int ind, int w) {
int t = 0, ss = v[w][ind].st;
while(w <= S && (!v[w].size() || (v[w].rbegin()->st) <= ss + m)) {
w++;
}
if(w > S) return mp(inf + 5, inf + 5);
return mp(lower_bound(v[w].begin(), v[w].end(), mp(ss + m + 1, 0)) - v[w].begin(), w);
}
int take() {
pii cur = mp(0, 1);
int ans = 0;
while(!v[cur.nd].size()) cur.nd++;
while(cur.nd < inf) {
int u = v[cur.nd][cur.st].nd;
if(next[u].st) { ans += next[u].st; cur.st = next[u].nd; }
else { cur = take(cur.st, cur.nd); ans++; }
}
return ans;
}
void init(vector< pii > &v) {
for(int i = (int) v.size() - 1; i >= 0; i--) {
int zz = v[i].nd, ind = upper_bound(v.begin(), v.end(), mp(v[i].st + m, inf)) - v.begin();
if(ind == v.size()) next[zz] = mp(0, i);
else {
ind = v[ind].nd;
next[zz] = mp(next[ind].st + 1, next[ind].nd);
}
}
}
void init() {
for(int i = 1; i <= S; i++) v[i].clear();
int s = 0; S = 0;
set< pii > :: iterator it2 = SS.end(); it2--;
vector< pii > temp;
foreach(it, SS) {
temp.pb(*it);
if(++s % SQ == 0 || (it == it2)) {
v[++S] = temp;
foreach(it, temp) wh[it->nd] = S;
init(v[S]);
temp.clear();
}
}
}
void del(int x, int y) {
int ww = wh[y];
v[ww].erase(find(v[ww].begin(), v[ww].end(), mp(x, y)));
init(v[ww]);
SS.erase(SS.find(mp(x, y)));
}
void add(int x, int y) {
int ww = S;
if(SS.lower_bound(mp(x, 0)) != SS.end())
ww = wh[SS.lower_bound(mp(x, 0))->nd];
SS.insert(mp(x, y));
wh[y] = ww;
v[ww].pb(mp(x, y));
sort(v[ww].begin(), v[ww].end());
init(v[ww]);
}
void init(int N, int L, int X[]) {
n = N;
m = L;
for(int i = 0; i < n; i++) {
xx[i+1] = X[i];
SS.insert(mp(X[i], i+1));
}
init();
}
int update(int i, int y) {
i++;
del(xx[i], i);
add(xx[i] = y, i);
if(++S % 500 == 0) init();
return take();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25168 KB |
Output is correct |
2 |
Correct |
0 ms |
25168 KB |
Output is correct |
3 |
Correct |
0 ms |
25168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
25168 KB |
Output is correct |
2 |
Correct |
0 ms |
25168 KB |
Output is correct |
3 |
Correct |
0 ms |
25168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3305 ms |
26100 KB |
Output is correct |
2 |
Correct |
3298 ms |
26496 KB |
Output is correct |
3 |
Correct |
2002 ms |
28072 KB |
Output is correct |
4 |
Correct |
1031 ms |
27808 KB |
Output is correct |
5 |
Correct |
889 ms |
27808 KB |
Output is correct |
6 |
Correct |
3285 ms |
28332 KB |
Output is correct |
7 |
Correct |
949 ms |
27808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2639 ms |
26352 KB |
Output is correct |
2 |
Correct |
2905 ms |
26612 KB |
Output is correct |
3 |
Correct |
4839 ms |
28340 KB |
Output is correct |
4 |
Correct |
5905 ms |
29520 KB |
Output is correct |
5 |
Correct |
5729 ms |
29524 KB |
Output is correct |
6 |
Correct |
6387 ms |
28996 KB |
Output is correct |
7 |
Correct |
5844 ms |
29524 KB |
Output is correct |
8 |
Correct |
5597 ms |
29528 KB |
Output is correct |
9 |
Correct |
1918 ms |
28996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
9000 ms |
34128 KB |
Program timed out |
2 |
Halted |
0 ms |
0 KB |
- |