Submission #94057

# Submission time Handle Problem Language Result Execution time Memory
94057 2019-01-15T12:38:53 Z wzy Dancing Elephants (IOI11_elephants) C++11
100 / 100
7323 ms 11236 KB
#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
int sqt = 0;
int n , L;
int V[150005];

vector<int> bucket[390];
int DP[150005];
int G[150005];
int pos[150005];
int lastbucket = 0;
int pot = 0;
int v[150005];
void rebuild(bool C){
	pot = 0;
	if(C){
		for(int i = 0 ; i < n ; i ++) v[i] = i;
	}
	else{
		int CC = 0;
		for(int i = 0 ; i <= lastbucket; i ++){
			for(int j = 0 ; j < bucket[i].size() ; j++){
				v[CC++] = bucket[i][j];
			}
			bucket[i].clear();
		}
	}
	int currbucket = 0;
	for(int i = 0 ; i < n; i++){
		if(bucket[currbucket].size() <= sqt){
			bucket[currbucket].push_back(v[i]);
		}
		else{
			bucket[++currbucket].push_back(v[i]);
		}
		pos[v[i]] = currbucket;
		lastbucket = currbucket;
	}
	for(int i = 0 ; i <= lastbucket ; i++){
		int curr = 0;
		int X = bucket[i].size();
		curr = X-1;
		for(int j = X - 1  ; j >=0  ; j--){		
			DP[bucket[i][j]] = 1;
			while(curr > j && V[bucket[i][curr - 1]] > (V[bucket[i][j]] + L)){
				curr--;
			}
			G[bucket[i][j]] = j;
			if(V[bucket[i][curr]] > (V[bucket[i][j]] + L)){
				DP[bucket[i][j]] += DP[bucket[i][curr]];
				G[bucket[i][j]] = G[bucket[i][curr]];
			}
		}
	}
}

void erase_elem(int currbucket, int vl){
	vector<int> w;
	for(int i = 0 ; i < bucket[currbucket].size() ; i++){
		if(bucket[currbucket][i] == vl){
			continue;
		}
		w.push_back(bucket[currbucket][i]);
	}
	bucket[currbucket] = w;
	int i = currbucket;
	int X = bucket[i].size();
	int curr = X-1;
	for(int j = X - 1  ; j >=0  ; j--){		
		DP[bucket[i][j]] = 1;
		while(curr > j && V[bucket[i][curr - 1]] > (V[bucket[i][j]] + L)){
			curr--;
		}
		G[bucket[i][j]] = j;
		if(V[bucket[i][curr]] > (V[bucket[i][j]] + L)){
			DP[bucket[i][j]] += DP[bucket[i][curr]];
			G[bucket[i][j]] = G[bucket[i][curr]];
		}
	}
	// recalculate DP now
}

void add_elem(int currbucket , int vl){
	vector<int> w;
	bool jafoi = false;
	pos[vl] = currbucket;
	for(int i = 0 ; i < bucket[currbucket].size() ; i++){
		if(V[bucket[currbucket][i]] >= V[vl] && !jafoi){
			jafoi = true;
			w.push_back(vl);
		}
		w.push_back(bucket[currbucket][i]);
	}
	if(!jafoi) w.push_back(vl);
	swap(bucket[currbucket] , w);
	int i = currbucket;
	int X = bucket[i].size();
	int curr = X - 1;
	for(int j = X - 1  ; j >=0  ; j--){		
		DP[bucket[i][j]] = 1;
		while(curr > j && V[bucket[i][curr - 1]] > (V[bucket[i][j]] + L)){
			curr--;
		}
		G[bucket[i][j]] = j;
		if(V[bucket[i][curr]] > (V[bucket[i][j]] + L)){
			DP[bucket[i][j]] += DP[bucket[i][curr]];
			G[bucket[i][j]] = G[bucket[i][curr]];
		}
	}	
}

void init(int N, int Lx, int X[])
{
  sqt = 850;
  n = N;
  L = Lx;
  for(int i = 0 ; i < N ; i ++) V[i] = X[i];
  rebuild(1);
}

int update(int ix, int yx)
{
	erase_elem(pos[ix],ix);
	int oldd = pos[ix];
	V[ix] = yx;
	int newbucket = lastbucket;
	for(int i = 0 ; i <= lastbucket -1 ; i++){
		if(bucket[i+1].size() && V[bucket[i+1].front()] >= yx){
			newbucket = i;
			break;
		}
	}
	add_elem(newbucket , ix);
	pot++;
	if(bucket[oldd].size() == 0 || pot >= 680){
		rebuild(0);
	}
	int currpos = 0;
	int currbucket = 0;
	int ans = 0;
	while(true){
 		ans += DP[bucket[currbucket][currpos]];
		currpos = G[bucket[currbucket][currpos]];
		int ansj = -1;
		int R = V[bucket[currbucket][currpos]] + L;
		for(int j = currbucket + 1 ; j <= lastbucket ; j++){
			if(V[bucket[j].back()] <= R){
				continue;
			}
			else{
				ansj = j;
				break;
			}
		}
		if(ansj == -1) break;
		else{
			currbucket = ansj;
			int l= 0 , r = bucket[currbucket].size();
			r--;
			int ansj2 = -1;
			while(l<=r){
				int mid = (l+r)/2;
				if(V[bucket[ansj][mid]] > R){
					r = mid - 1;
					ansj2 = mid;
				}
				else l = mid + 1;
			}
			currpos = ansj2;
		}
 	}
 	return ans;

}

Compilation message

elephants.cpp: In function 'void rebuild(bool)':
elephants.cpp:23:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j = 0 ; j < bucket[i].size() ; j++){
                    ~~^~~~~~~~~~~~~~~~~~
elephants.cpp:31:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(bucket[currbucket].size() <= sqt){
      ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
elephants.cpp: In function 'void erase_elem(int, int)':
elephants.cpp:60:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < bucket[currbucket].size() ; i++){
                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'void add_elem(int, int)':
elephants.cpp:88:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0 ; i < bucket[currbucket].size() ; i++){
                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 536 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 536 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 424 KB Output is correct
7 Correct 893 ms 2312 KB Output is correct
8 Correct 913 ms 2516 KB Output is correct
9 Correct 856 ms 3728 KB Output is correct
10 Correct 754 ms 3704 KB Output is correct
11 Correct 768 ms 3720 KB Output is correct
12 Correct 1572 ms 3752 KB Output is correct
13 Correct 763 ms 3568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 536 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 424 KB Output is correct
7 Correct 893 ms 2312 KB Output is correct
8 Correct 913 ms 2516 KB Output is correct
9 Correct 856 ms 3728 KB Output is correct
10 Correct 754 ms 3704 KB Output is correct
11 Correct 768 ms 3720 KB Output is correct
12 Correct 1572 ms 3752 KB Output is correct
13 Correct 763 ms 3568 KB Output is correct
14 Correct 1050 ms 3296 KB Output is correct
15 Correct 1329 ms 3320 KB Output is correct
16 Correct 2563 ms 3832 KB Output is correct
17 Correct 2520 ms 4544 KB Output is correct
18 Correct 2701 ms 4580 KB Output is correct
19 Correct 1016 ms 4572 KB Output is correct
20 Correct 2482 ms 4864 KB Output is correct
21 Correct 2411 ms 4700 KB Output is correct
22 Correct 929 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 376 KB Output is correct
3 Correct 2 ms 536 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 424 KB Output is correct
7 Correct 893 ms 2312 KB Output is correct
8 Correct 913 ms 2516 KB Output is correct
9 Correct 856 ms 3728 KB Output is correct
10 Correct 754 ms 3704 KB Output is correct
11 Correct 768 ms 3720 KB Output is correct
12 Correct 1572 ms 3752 KB Output is correct
13 Correct 763 ms 3568 KB Output is correct
14 Correct 1050 ms 3296 KB Output is correct
15 Correct 1329 ms 3320 KB Output is correct
16 Correct 2563 ms 3832 KB Output is correct
17 Correct 2520 ms 4544 KB Output is correct
18 Correct 2701 ms 4580 KB Output is correct
19 Correct 1016 ms 4572 KB Output is correct
20 Correct 2482 ms 4864 KB Output is correct
21 Correct 2411 ms 4700 KB Output is correct
22 Correct 929 ms 4728 KB Output is correct
23 Correct 4514 ms 7916 KB Output is correct
24 Correct 4917 ms 7900 KB Output is correct
25 Correct 3684 ms 7672 KB Output is correct
26 Correct 3168 ms 7780 KB Output is correct
27 Correct 4064 ms 7800 KB Output is correct
28 Correct 4756 ms 3832 KB Output is correct
29 Correct 4668 ms 3704 KB Output is correct
30 Correct 4742 ms 3732 KB Output is correct
31 Correct 4685 ms 3728 KB Output is correct
32 Correct 3866 ms 7688 KB Output is correct
33 Correct 2107 ms 7800 KB Output is correct
34 Correct 2998 ms 7784 KB Output is correct
35 Correct 2850 ms 8576 KB Output is correct
36 Correct 4260 ms 7868 KB Output is correct
37 Correct 5660 ms 7940 KB Output is correct
38 Correct 3102 ms 10204 KB Output is correct
39 Correct 3343 ms 11128 KB Output is correct
40 Correct 3298 ms 10232 KB Output is correct
41 Correct 7161 ms 10880 KB Output is correct
42 Correct 7323 ms 11236 KB Output is correct