Submission #152663

# Submission time Handle Problem Language Result Execution time Memory
152663 2019-09-09T05:55:02 Z qkxwsm Dancing Elephants (IOI11_elephants) C++14
100 / 100
7587 ms 19140 KB
#include "elephants.h"
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 150013
#define INF 1000000007
#define MAGIC 1700

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, L, Q, B;
int arr[MAXN];
multiset<int> vals;
vector<array<int, 3> > stor[MAGIC];
int ans;

//ok keep blocks of size k.
//each block stores: this guy takes x cost to jump to the end. and he arrives at y.
//ok so each query takes O(k) + O(N/k logN)
//so you choose k = sqrt(NlogN)
//so rebuild every sqrt(NlogN). each rebuild takes O(N). solution in Nsqrt(NlogN)

void calc(int i)
{
	int iter = SZ(stor[i]) - 1;
	FORD(j, SZ(stor[i]), 0)
	{
		int x = stor[i][j][0];
		while(iter >= 0 && stor[i][iter][0] >= x + L) iter--;
		if (iter == SZ(stor[i]) - 1)
		{
			stor[i][j][1] = 1;
			stor[i][j][2] = x + L;
		}
		else
		{
			stor[i][j][1] = 1 + stor[i][iter + 1][1];
			stor[i][j][2] = stor[i][iter + 1][2];
		}
	}
}
void del(int v)
{
	//find which block has it.
	FORD(i, B, 0)
	{
		if (stor[i][0][0] > v) continue;
		//there's something that has x.
		FOR(j, 0, SZ(stor[i]))
		{
			if (stor[i][j][0] == v)
			{
				stor[i].erase(stor[i].begin() + j);
				break;
			}
		}
		calc(i);
		break;
	}
}
void ins(int v)
{
	FORD(i, B, 0)
	{
		if (stor[i][0][0] > v && i != 0) continue;
		FOR(j, 0, SZ(stor[i]) + 1)
		{
			if (j == SZ(stor[i]) || stor[i][j][0] > v)
			{
				stor[i].insert(stor[i].begin() + j, {v, 0, 0});
				break;
			}
		}
		calc(i);
		break;
	}
}
void build()
{
	auto it = vals.begin();
	FOR(i, 0, B)
	{
		stor[i].clear();
		FOR(j, 0, MAGIC)
		{
			if (it == vals.end()) break;
			stor[i].PB({*it, 0, 0});
			it++;
		}
		calc(i);
	}
	return;
}
void debug()
{
	FOR(i, 0, B)
	{
		cerr << "BLOCK " << i << endl;
		FOR(j, 0, SZ(stor[i]))
		{
			FOR(k, 0, 3)
			{
				cerr << stor[i][j][k] << ' ';
			}
			cerr << endl;
		}
	}
}
int trav()
{
	int res = 0, pos = -INF;
	FOR(i, 0, B)
	{
		//find the first guy that's > pos.
		array<int, 3> tmp = {pos, -1, -1};
		int idx = LB(ALL(stor[i]), tmp) - stor[i].begin();
		if (idx == SZ(stor[i])) continue;
		res += stor[i][idx][1];
		pos = stor[i][idx][2];
	}
	return res;
}
void init(int n, int l, int x[])
{
	N = n; L = l; B = (N + MAGIC - 1) / MAGIC;
	L++;
	FOR(i, 0, N)
	{
		arr[i] = x[i];
		vals.insert(arr[i]);
	}
	build();
}
int update(int idx, int v)
{
	vals.erase(vals.find(arr[idx]));
	del(arr[idx]);
	arr[idx] = v;
	vals.insert(arr[idx]);
	ins(arr[idx]);
	Q++;
	if (Q % MAGIC == 0)
	{
		build();
	}
	// debug();
	// if (Q >= 17000)
	// {
	// 	cerr << "Q = " << Q << endl;
	// 	debug();
	// }
	// cerr << "Q = " << Q << endl;
	// debug();
	ans = trav();
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 1115 ms 1900 KB Output is correct
8 Correct 1182 ms 2488 KB Output is correct
9 Correct 847 ms 4728 KB Output is correct
10 Correct 659 ms 4856 KB Output is correct
11 Correct 652 ms 4728 KB Output is correct
12 Correct 1658 ms 4656 KB Output is correct
13 Correct 643 ms 4728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 1115 ms 1900 KB Output is correct
8 Correct 1182 ms 2488 KB Output is correct
9 Correct 847 ms 4728 KB Output is correct
10 Correct 659 ms 4856 KB Output is correct
11 Correct 652 ms 4728 KB Output is correct
12 Correct 1658 ms 4656 KB Output is correct
13 Correct 643 ms 4728 KB Output is correct
14 Correct 1012 ms 2756 KB Output is correct
15 Correct 1632 ms 4344 KB Output is correct
16 Correct 3048 ms 6520 KB Output is correct
17 Correct 2758 ms 8184 KB Output is correct
18 Correct 3004 ms 8312 KB Output is correct
19 Correct 1224 ms 8312 KB Output is correct
20 Correct 2762 ms 8400 KB Output is correct
21 Correct 2533 ms 8200 KB Output is correct
22 Correct 831 ms 7800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 1115 ms 1900 KB Output is correct
8 Correct 1182 ms 2488 KB Output is correct
9 Correct 847 ms 4728 KB Output is correct
10 Correct 659 ms 4856 KB Output is correct
11 Correct 652 ms 4728 KB Output is correct
12 Correct 1658 ms 4656 KB Output is correct
13 Correct 643 ms 4728 KB Output is correct
14 Correct 1012 ms 2756 KB Output is correct
15 Correct 1632 ms 4344 KB Output is correct
16 Correct 3048 ms 6520 KB Output is correct
17 Correct 2758 ms 8184 KB Output is correct
18 Correct 3004 ms 8312 KB Output is correct
19 Correct 1224 ms 8312 KB Output is correct
20 Correct 2762 ms 8400 KB Output is correct
21 Correct 2533 ms 8200 KB Output is correct
22 Correct 831 ms 7800 KB Output is correct
23 Correct 4494 ms 17580 KB Output is correct
24 Correct 5157 ms 17636 KB Output is correct
25 Correct 3245 ms 17596 KB Output is correct
26 Correct 2910 ms 17528 KB Output is correct
27 Correct 5479 ms 17472 KB Output is correct
28 Correct 5881 ms 5496 KB Output is correct
29 Correct 5849 ms 5492 KB Output is correct
30 Correct 5894 ms 5500 KB Output is correct
31 Correct 5814 ms 5500 KB Output is correct
32 Correct 2757 ms 17056 KB Output is correct
33 Correct 2361 ms 16504 KB Output is correct
34 Correct 2326 ms 17272 KB Output is correct
35 Correct 2063 ms 17564 KB Output is correct
36 Correct 1299 ms 16860 KB Output is correct
37 Correct 5095 ms 19140 KB Output is correct
38 Correct 2351 ms 16264 KB Output is correct
39 Correct 3045 ms 17272 KB Output is correct
40 Correct 2419 ms 16400 KB Output is correct
41 Correct 7209 ms 17124 KB Output is correct
42 Correct 7587 ms 17296 KB Output is correct