Submission #233945

# Submission time Handle Problem Language Result Execution time Memory
233945 2020-05-22T12:56:30 Z crossing0ver Dancing Elephants (IOI11_elephants) C++17
100 / 100
4991 ms 13152 KB
#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
const int K = 400, MXN = 1.5E5 + 5;
int P[150000], inbox[150000] , L,B = MXN/K, n,Q,sorted[150000]; 
struct BLOCK{
	int sz;
	int arr[K*2 + 10] , last[K*2 + 10], cost[K*2 + 10];
	
	void del(int x) {
		for (int i = 0; i < sz; i++)
			if (arr[i] == x) {
				x = i; break;
			}
		for (; x < sz;x++)
			swap(arr[x],arr[x+1]);
		sz--;
	}
	void insert (int x) {
		arr[sz] = x;
		int pos = sz;
		for ( ; pos > 0 && P[x] <  P[ arr[pos - 1] ]; pos--)
				swap(arr[pos],arr[pos - 1]);
		sz++;
	}
	void slv() {
		int ptr = sz - 1;
		for (int i = sz - 1; i >= 0; i--) {
			while (ptr > 0 && P[arr[i]] + L < P[arr[ptr - 1]]) ptr--;
			if (P[arr[i]] + L>= P[arr[sz - 1]]) 
					cost[i] = 1, last[i] = arr[i];
			else cost[i] = 1 + cost[ptr], last[i] = last[ptr];
		}
	}
	
	int get (int val) {
		if (sz == 0 ) return -1;
		int l = 0, r = sz - 1;
		while ( l != r) {
			int m = (l + r)/2;
			if (P[arr[m]] > val) r = m;
			else l = m + 1;
		}
		if (P[arr[r]] <= val) return -1;
		return r;
	} 
} box[MXN/K + 10];             
					  
void build() {
	if (Q % K != 0) return;
	if (Q == 0) {
		for (int i = 0; i < n; i++)
		sorted[i] = i;
	}
	else {
		int cnt = 0;
		for (int i = 0; i <= B; i++) {
			 for (int j = 0; j < box[i].sz; j++)
			 	sorted[cnt++] = box[i].arr[j];
		}		
	}
	
	for (int i = 0 ; i <= B; i++) 
	box[i].sz = 0;
	int blc = -1;
	for (int i = 0; i < n; i++) {
		if (i % K == 0){
			blc++;
			box[blc].sz = 0;
		}
		box[blc].insert(sorted[i]);
		inbox[sorted[i]] = blc;
	}
	for (int i= 0; i <= B; i++)	box[i].slv();
	B = blc + 1;
}                  


void init(int N, int L1, int X[]) {
  n = N; L = L1;
  for (int i = 0; i < n; i++) P[i] = X[i];
  build();
}

int update(int i, int y) {
	Q++;
	box[inbox[i]].del(i);
	box[inbox[i]].slv();
	P[i] = y;
	for (int j = 0; j <= B; j++) {
		if (((box[j+1].sz) && P[box[j+1].arr[0]] >= y) || j == B - 1) {
			box[j].insert(i);
			inbox[i] = j;
			box[j].slv();
			break;
		}
	}
	build();
	int val = INT_MIN,ans = 0;
	for (int i = 0; i <= B; i++) {
		int pos = box[i].get(val + L);
		if (pos == -1) continue;
		ans+= box[i].cost[pos];
		val = P[box[i].last[pos]];
	}    
	return ans;       
}
		   /*
int main(){
	int N,L1,q;
	cin >> N >> L1 >> q;
	int X[N];
	for (int i = 0 ; i<  N ; i ++) cin >> X[i];
	init(N,L1,X);
	while (q--) {
		int i,y;
		cin >> i >> y;
		cout << update(i,y) <<' ';
		cout << P[i] <<'\n';
	}
}         */               
/* 4 10 5 
 10 15 17 20
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 5 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 5 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 5 ms 1920 KB Output is correct
5 Correct 5 ms 1920 KB Output is correct
6 Correct 6 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 5 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 5 ms 1920 KB Output is correct
5 Correct 5 ms 1920 KB Output is correct
6 Correct 6 ms 1920 KB Output is correct
7 Correct 489 ms 2936 KB Output is correct
8 Correct 521 ms 4120 KB Output is correct
9 Correct 461 ms 5624 KB Output is correct
10 Correct 422 ms 5240 KB Output is correct
11 Correct 460 ms 5444 KB Output is correct
12 Correct 754 ms 5496 KB Output is correct
13 Correct 421 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 5 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 5 ms 1920 KB Output is correct
5 Correct 5 ms 1920 KB Output is correct
6 Correct 6 ms 1920 KB Output is correct
7 Correct 489 ms 2936 KB Output is correct
8 Correct 521 ms 4120 KB Output is correct
9 Correct 461 ms 5624 KB Output is correct
10 Correct 422 ms 5240 KB Output is correct
11 Correct 460 ms 5444 KB Output is correct
12 Correct 754 ms 5496 KB Output is correct
13 Correct 421 ms 5112 KB Output is correct
14 Correct 414 ms 4856 KB Output is correct
15 Correct 761 ms 4984 KB Output is correct
16 Correct 1270 ms 6136 KB Output is correct
17 Correct 1333 ms 6844 KB Output is correct
18 Correct 1468 ms 6776 KB Output is correct
19 Correct 787 ms 7024 KB Output is correct
20 Correct 1333 ms 6904 KB Output is correct
21 Correct 1278 ms 6808 KB Output is correct
22 Correct 746 ms 6408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 5 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 5 ms 1920 KB Output is correct
5 Correct 5 ms 1920 KB Output is correct
6 Correct 6 ms 1920 KB Output is correct
7 Correct 489 ms 2936 KB Output is correct
8 Correct 521 ms 4120 KB Output is correct
9 Correct 461 ms 5624 KB Output is correct
10 Correct 422 ms 5240 KB Output is correct
11 Correct 460 ms 5444 KB Output is correct
12 Correct 754 ms 5496 KB Output is correct
13 Correct 421 ms 5112 KB Output is correct
14 Correct 414 ms 4856 KB Output is correct
15 Correct 761 ms 4984 KB Output is correct
16 Correct 1270 ms 6136 KB Output is correct
17 Correct 1333 ms 6844 KB Output is correct
18 Correct 1468 ms 6776 KB Output is correct
19 Correct 787 ms 7024 KB Output is correct
20 Correct 1333 ms 6904 KB Output is correct
21 Correct 1278 ms 6808 KB Output is correct
22 Correct 746 ms 6408 KB Output is correct
23 Correct 3620 ms 13152 KB Output is correct
24 Correct 3915 ms 13116 KB Output is correct
25 Correct 2990 ms 13108 KB Output is correct
26 Correct 3866 ms 13104 KB Output is correct
27 Correct 3850 ms 13048 KB Output is correct
28 Correct 1703 ms 6776 KB Output is correct
29 Correct 1624 ms 6648 KB Output is correct
30 Correct 1705 ms 6648 KB Output is correct
31 Correct 1639 ms 6648 KB Output is correct
32 Correct 3359 ms 12536 KB Output is correct
33 Correct 2809 ms 11896 KB Output is correct
34 Correct 2956 ms 12664 KB Output is correct
35 Correct 2968 ms 13048 KB Output is correct
36 Correct 3091 ms 12536 KB Output is correct
37 Correct 3915 ms 12956 KB Output is correct
38 Correct 3068 ms 11756 KB Output is correct
39 Correct 3038 ms 12780 KB Output is correct
40 Correct 3158 ms 11896 KB Output is correct
41 Correct 4867 ms 12664 KB Output is correct
42 Correct 4991 ms 12792 KB Output is correct