답안 #287356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287356 2020-08-31T16:22:12 Z Namnamseo 코끼리 (Dancing Elephants) (IOI11_elephants) C++17
100 / 100
7713 ms 13916 KB
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cstring>
#define BS 400
using namespace std;

int n;
int l;

struct buck {
	int pos[1010];
	int dp [1010];
	int ep [1010];
	int sz;
	buck(){ sz=0; }

	void recalcDP(int until){
		for(int i=until; 0<=i; --i){
			auto it=upper_bound(pos, pos+sz, pos[i]+l);

			if(it == pos+sz){
				dp[i] = 0;
				ep[i] = pos[i];
			} else {
				dp[i] = 1+dp[it-pos];
				ep[i] = ep[it-pos];
			}
		}
	}

	void ins(int x){
		if(sz == 0){
			pos[0] = x;
			dp [0] = 0;
			ep [0] = x;
			sz = 1;
			return;
		}
		auto it=upper_bound(pos, pos+sz, x);
		if(it != pos+sz){
			rotate(it, pos+sz, pos+sz+1);
			rotate(dp+(it-pos), dp+sz, dp+sz+1);
			rotate(ep+(it-pos), ep+sz, ep+sz+1);
		}
		*it = x;
		++sz;

		recalcDP(it-pos);
	}

	void rem(int x){
		int p = lower_bound(pos, pos+sz, x)-pos;
		rotate(pos+p, pos+p+1, pos+sz);
		rotate(dp +p, dp +p+1, dp +sz);
		rotate(ep +p, ep +p+1, ep +sz);
		--sz;

		if(sz) recalcDP(min(sz-1,p));
	}
} bucks[510];
int bn;

int findBuck(int x){
	if(x < bucks[0].pos[0]) return 0;
	for(int i=0; i<bn; ++i){
		if(bucks[i].pos[0]<=x && (i==bn-1 || x<bucks[i+1].pos[0])) return i;
	}
    for(;;);
	return -1;
}

int sqrtN;

int pos   [150001];
int sorted[150001];

void recreateBuck(bool init){
	if(init){
		memcpy(sorted, pos, n*sizeof(int));
	} else {
		int ind = 0;
		for(int i=0; i<bn; ++i){
			int *arr = bucks[i].pos;
			int sz=bucks[i].sz;
			for(int j=0; j<sz; ++j){
				sorted[ind++] = arr[j];
			}
			bucks[i].sz = 0;
		}
	}

	bn = 0;
	for(int i=0; i<n; ++i){
		int cb=i/BS;
		if(cb >= bn) ++bn;
		int& sz=bucks[cb].sz;
		bucks[cb].pos[sz++] = sorted[i];
	}
	for(int i=0; i<bn; ++i){
		bucks[i].recalcDP(bucks[i].sz-1);
	}
}

void init(int N, int L, int X[])
{
	n = N; l = L;

	for(int i=0; i<n; ++i) pos[i] = X[i];

	recreateBuck(1);
}

int timer = 0;

int getAns(){
	int lastP = bucks[0].ep[0];
	int ans = bucks[0].dp[0];

	for(int i=1; i<bn; ++i){
		if(lastP + l >= bucks[i].pos[bucks[i].sz-1]) continue;
		++ans;
		int ind = upper_bound(bucks[i].pos, bucks[i].pos+bucks[i].sz, lastP+l)-bucks[i].pos;
		ans += bucks[i].dp[ind];
		lastP = bucks[i].ep[ind];
	}
	++ans;
	return ans;
}

int update(int i, int y)
{
	int t = findBuck(pos[i]);
	int nt = findBuck(y);
	bucks[t].rem(pos[i]);

	pos[i]=y;

	bucks[nt].ins(y);

	++timer;
	if(timer >= BS){
		recreateBuck(0);
		timer = 0;
	}
	return getAns();
}

Compilation message

elephants.cpp: In function 'int findBuck(int)':
elephants.cpp:69:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   69 |     for(;;);
      |     ^~~
elephants.cpp:70:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   70 |  return -1;
      |  ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2432 KB Output is correct
2 Correct 1 ms 2432 KB Output is correct
3 Correct 1 ms 2432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2432 KB Output is correct
2 Correct 1 ms 2432 KB Output is correct
3 Correct 1 ms 2432 KB Output is correct
4 Correct 2 ms 2432 KB Output is correct
5 Correct 2 ms 2432 KB Output is correct
6 Correct 1 ms 2432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2432 KB Output is correct
2 Correct 1 ms 2432 KB Output is correct
3 Correct 1 ms 2432 KB Output is correct
4 Correct 2 ms 2432 KB Output is correct
5 Correct 2 ms 2432 KB Output is correct
6 Correct 1 ms 2432 KB Output is correct
7 Correct 707 ms 4344 KB Output is correct
8 Correct 761 ms 4600 KB Output is correct
9 Correct 998 ms 6264 KB Output is correct
10 Correct 1206 ms 6008 KB Output is correct
11 Correct 1313 ms 5880 KB Output is correct
12 Correct 1353 ms 6136 KB Output is correct
13 Correct 1234 ms 5752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2432 KB Output is correct
2 Correct 1 ms 2432 KB Output is correct
3 Correct 1 ms 2432 KB Output is correct
4 Correct 2 ms 2432 KB Output is correct
5 Correct 2 ms 2432 KB Output is correct
6 Correct 1 ms 2432 KB Output is correct
7 Correct 707 ms 4344 KB Output is correct
8 Correct 761 ms 4600 KB Output is correct
9 Correct 998 ms 6264 KB Output is correct
10 Correct 1206 ms 6008 KB Output is correct
11 Correct 1313 ms 5880 KB Output is correct
12 Correct 1353 ms 6136 KB Output is correct
13 Correct 1234 ms 5752 KB Output is correct
14 Correct 703 ms 5308 KB Output is correct
15 Correct 1185 ms 5496 KB Output is correct
16 Correct 1894 ms 6648 KB Output is correct
17 Correct 2173 ms 7544 KB Output is correct
18 Correct 2120 ms 7416 KB Output is correct
19 Correct 1062 ms 7672 KB Output is correct
20 Correct 2175 ms 7544 KB Output is correct
21 Correct 2111 ms 7544 KB Output is correct
22 Correct 1908 ms 7160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2432 KB Output is correct
2 Correct 1 ms 2432 KB Output is correct
3 Correct 1 ms 2432 KB Output is correct
4 Correct 2 ms 2432 KB Output is correct
5 Correct 2 ms 2432 KB Output is correct
6 Correct 1 ms 2432 KB Output is correct
7 Correct 707 ms 4344 KB Output is correct
8 Correct 761 ms 4600 KB Output is correct
9 Correct 998 ms 6264 KB Output is correct
10 Correct 1206 ms 6008 KB Output is correct
11 Correct 1313 ms 5880 KB Output is correct
12 Correct 1353 ms 6136 KB Output is correct
13 Correct 1234 ms 5752 KB Output is correct
14 Correct 703 ms 5308 KB Output is correct
15 Correct 1185 ms 5496 KB Output is correct
16 Correct 1894 ms 6648 KB Output is correct
17 Correct 2173 ms 7544 KB Output is correct
18 Correct 2120 ms 7416 KB Output is correct
19 Correct 1062 ms 7672 KB Output is correct
20 Correct 2175 ms 7544 KB Output is correct
21 Correct 2111 ms 7544 KB Output is correct
22 Correct 1908 ms 7160 KB Output is correct
23 Correct 6514 ms 13908 KB Output is correct
24 Correct 6787 ms 13908 KB Output is correct
25 Correct 5136 ms 13908 KB Output is correct
26 Correct 7713 ms 13916 KB Output is correct
27 Correct 4700 ms 13780 KB Output is correct
28 Correct 2660 ms 7296 KB Output is correct
29 Correct 2569 ms 7296 KB Output is correct
30 Correct 2566 ms 7292 KB Output is correct
31 Correct 2544 ms 7288 KB Output is correct
32 Correct 5756 ms 13364 KB Output is correct
33 Correct 5201 ms 12700 KB Output is correct
34 Correct 5870 ms 13708 KB Output is correct
35 Correct 3184 ms 13856 KB Output is correct
36 Correct 1819 ms 13432 KB Output is correct
37 Correct 6782 ms 13756 KB Output is correct
38 Correct 5898 ms 12536 KB Output is correct
39 Correct 3972 ms 13604 KB Output is correct
40 Correct 5428 ms 12664 KB Output is correct
41 Correct 7091 ms 13348 KB Output is correct
42 Correct 7119 ms 13580 KB Output is correct