Submission #25494

# Submission time Handle Problem Language Result Execution time Memory
25494 2017-06-22T12:44:37 Z Bruteforceman Dancing Elephants (IOI11_elephants) C++11
100 / 100
6546 ms 25108 KB
#include "elephants.h"
#include "bits/stdc++.h"
using namespace std;

int n;
int a[150010];
int len;
const int magic = 390;
vector <int> v[1000];
vector <int> dp[1000];
vector <int> nxt[1000];

int bucket[150010];
int tot;
int counter;

bool cmp(int x, int y) {
	return a[x] < a[y];
}
int search(int b, int e, int block, int value) {
	if(b == e) {
		return a[v[block][b]] <= value ? b : -1;
	}
	int m = (b + e + 1) >> 1;
	if(a[v[block][m]] <= value) return search(m, e, block, value);
	else return search(b, m-1, block, value);
}

void get_done(int b) {
	if(v[b].empty()) return ;
	int sz = v[b].size();
	nxt[b].resize(sz);
	dp[b].resize(sz);

	int pointer = sz-1;
	for(int i = sz-1; i >= 0; i--) {
		while(pointer >= 0 && a[v[b][i]] + len < a[v[b][pointer]]) {
			--pointer;
		}
		int id = pointer;
		if(id == sz - 1) {
			dp[b][i] = 1;
			nxt[b][i] = a[v[b][i]] + len;
		} else {
			dp[b][i] = dp[b][id + 1] + 1;
			nxt[b][i] = nxt[b][id + 1];
		}
	}
}
void init(int N, int L, int X[])
{
	tot = (N-1) / magic;
	len = L;
	n = N;
	for(int i = 0; i < n; i++) {
  		a[i] = X[i];
  	}
  	for(int i = 0; i < n; i++) {
  		v[i / magic].push_back(i);
  		bucket[i] = i / magic;
  	}
  	for(int i = 0; i <= tot; i++) {
  		get_done(i);
  	}
  	counter = 0;
}
int query() {
	int ans = 0;
	int cover = -1;
	for(int i = 0; i <= tot; i++) {
		if(v[i].empty()) continue;
		int sz = v[i].size();
		int u = search(0, sz-1, i, cover);
		if(u < sz-1) {
			ans += dp[i][u + 1];
			cover = nxt[i][u + 1];
		}
	}
	return ans;
}

void rebuild() {
	vector <int> pos;
	for(int i = 0; i <= tot; i++) {
		for(auto j : v[i]) {
			pos.push_back(j);
		}
		v[i].clear();
	}
	for(int i = 0; i < n; i++) {
		bucket[pos[i]] = i / magic;
		v[i / magic].push_back(pos[i]);
	}
	for(int i = 0; i <= tot; i++) {
		get_done(i);
	}
}
int update(int i, int y)
{
	++counter;
	if(counter == magic) {
		counter = 0;
		rebuild();
	}
	int b1 = bucket[i];
	int b2 = tot;
	v[b1].erase(find(v[b1].begin(), v[b1].end(), i));

	a[i] = y;
	for(int x = 0; x <= tot; x++) {
		if(v[x].empty()) continue;
		if(a[i] <= a[v[x].back()]) {
			b2 = x;
			break;
		}
	}
	bucket[i] = b2;
	v[b2].push_back(i);
	int pointer = v[b2].size() - 1;
	while(pointer > 0 && a[v[b2][pointer - 1]] > a[v[b2][pointer]]) {
		swap(v[b2][pointer - 1], v[b2][pointer]);
		--pointer;
	}
	get_done(b1);
	get_done(b2);
	return query();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 18892 KB Output is correct
2 Correct 0 ms 18892 KB Output is correct
3 Correct 0 ms 18892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 18892 KB Output is correct
2 Correct 0 ms 18892 KB Output is correct
3 Correct 0 ms 18892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 339 ms 19440 KB Output is correct
2 Correct 316 ms 19664 KB Output is correct
3 Correct 503 ms 20296 KB Output is correct
4 Correct 379 ms 20140 KB Output is correct
5 Correct 539 ms 20140 KB Output is correct
6 Correct 876 ms 20536 KB Output is correct
7 Correct 383 ms 20140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 426 ms 19664 KB Output is correct
2 Correct 516 ms 19708 KB Output is correct
3 Correct 1596 ms 20528 KB Output is correct
4 Correct 1693 ms 21464 KB Output is correct
5 Correct 1956 ms 21468 KB Output is correct
6 Correct 899 ms 20912 KB Output is correct
7 Correct 1723 ms 21464 KB Output is correct
8 Correct 1683 ms 21468 KB Output is correct
9 Correct 889 ms 20916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4579 ms 23728 KB Output is correct
2 Correct 4663 ms 23836 KB Output is correct
3 Correct 4166 ms 23440 KB Output is correct
4 Correct 4616 ms 22980 KB Output is correct
5 Correct 4593 ms 22976 KB Output is correct
6 Correct 1413 ms 19024 KB Output is correct
7 Correct 1293 ms 19024 KB Output is correct
8 Correct 1359 ms 19024 KB Output is correct
9 Correct 1276 ms 19024 KB Output is correct
10 Correct 4443 ms 22980 KB Output is correct
11 Correct 4139 ms 22980 KB Output is correct
12 Correct 4399 ms 22980 KB Output is correct
13 Correct 3869 ms 25108 KB Output is correct
14 Correct 4956 ms 22976 KB Output is correct
15 Correct 5529 ms 24916 KB Output is correct
16 Correct 4233 ms 22980 KB Output is correct
17 Correct 3883 ms 22976 KB Output is correct
18 Correct 4016 ms 22980 KB Output is correct
19 Correct 6546 ms 24164 KB Output is correct
20 Correct 6246 ms 24148 KB Output is correct