Submission #406277

# Submission time Handle Problem Language Result Execution time Memory
406277 2021-05-17T10:14:18 Z pure_mem Dancing Elephants (IOI11_elephants) C++14
100 / 100
6661 ms 13612 KB
#include "elephants.h"
#include <bits/stdc++.h>

#define X first
#define Y second
#define MP make_pair
#define ll long long

using namespace std;

const int MAXN = 150000, BS = 400, INF = 1e9 + 7;

int n, w, p[MAXN], ps[MAXN], buck[MAXN];
vector< pair< int, int > > bl[MAXN / BS + 3];
int dp1[MAXN], dp2[MAXN];

void trick(vector< pair<int, int> > &cur, pair<int, int> val, bool keep){
	stack< pair<int, int> > tmp;
	while(!cur.empty()){
		if(keep){
			if(cur.back() < val){
				cur.push_back(val);
				break;	
			}
		}
		else{
			if(val == cur.back()){
				cur.pop_back();
				break;
			}
		}
		tmp.push(cur.back()), cur.pop_back();
	}
	if(keep && cur.empty())
		cur.push_back(val);
	while(!tmp.empty())
		cur.push_back(tmp.top()), tmp.pop();
	sort(cur.begin(), cur.end());
}   

void upd(int id){		
	for(int i = (int)bl[id].size() - 1, j = (int)bl[id].size();i >= 0;i--){
		int me = bl[id][i].Y, pos = bl[id][i].X;
		buck[me] = id;
		while(j - 1 >= i && bl[id][j - 1].X > pos + w) j--;
		if(j == (int)bl[id].size()) dp1[me] = 1, dp2[me] = pos + w;
		else dp1[me] = dp1[bl[id][j].Y] + 1, dp2[me] = dp2[bl[id][j].Y]; 	
	}		
}

void build(){		
	for(int i = 0, j = 0;i < n;i += BS, j++)
		bl[j].clear();
	for(int i = 0, j = 0, z = 1;i < n;i++, (z == BS ? z = 1, j++: z++))
		bl[j].push_back(MP(p[ps[i]], ps[i]));
	for(int i = 0, j = 0;i < n;i += BS, j++)
		upd(j);
}

void init(int N, int L, int X[]){
  	n = N, w = L;
  	for(int i = 0;i < n;i++)
    	p[i] = X[i], ps[i] = i; 
    sort(ps, ps + n, [](int x, int y){return p[x] < p[y];});
	build();
}

int solve(){
	int cnt = 0, pos = 0;
	for(int j = 0;;j++){
		if(bl[j].empty())
			break;
		else if(bl[j].back().X < pos)
			continue;
		pair<int, int> tmp = *upper_bound(bl[j].begin(), bl[j].end(), MP(pos, -1));
		cnt += dp1[tmp.Y], pos = dp2[tmp.Y] + 1;
	}	
	return cnt;
}

int cntQ;

int update(int i, int y){
  	cntQ++;
  	if(cntQ == BS){
  		trick(bl[buck[i]], MP(p[i], i), 0), upd(buck[i]);
    	p[i] = y, cntQ = 0;
    	for(int j = 0, it = 0;it < n;j++){
    		if(j * BS > n){
    			ps[n - 1] = i;
    			break;
    		}
    		for(pair<int, int> v: bl[j]){
    			if(i != -1 && MP(p[i], i) < v)
    				ps[it++] = i, i = -1;
    			ps[it++] = v.Y;
    		}
    	}
    	build();
  	}
  	else{ 
  		trick(bl[buck[i]], MP(p[i], i), 0), upd(buck[i]);
  		p[i] = y;
  		for(int j = 0;;j++){
  			int left = (j ? bl[j - 1].back().X: 0), right = (bl[j + 1].empty() ? INF: bl[j + 1][0].X);
  			if(left <= y && y <= right){
  				trick(bl[j], MP(p[i], i), 1), upd(j);
  				break;
  			}
  		}
  	}
  	return solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1025 ms 2352 KB Output is correct
8 Correct 1063 ms 2524 KB Output is correct
9 Correct 983 ms 4152 KB Output is correct
10 Correct 966 ms 4036 KB Output is correct
11 Correct 948 ms 3876 KB Output is correct
12 Correct 1304 ms 4164 KB Output is correct
13 Correct 994 ms 3736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1025 ms 2352 KB Output is correct
8 Correct 1063 ms 2524 KB Output is correct
9 Correct 983 ms 4152 KB Output is correct
10 Correct 966 ms 4036 KB Output is correct
11 Correct 948 ms 3876 KB Output is correct
12 Correct 1304 ms 4164 KB Output is correct
13 Correct 994 ms 3736 KB Output is correct
14 Correct 1459 ms 3512 KB Output is correct
15 Correct 1552 ms 3392 KB Output is correct
16 Correct 2011 ms 4640 KB Output is correct
17 Correct 2045 ms 5568 KB Output is correct
18 Correct 2176 ms 5488 KB Output is correct
19 Correct 1705 ms 5572 KB Output is correct
20 Correct 2029 ms 5548 KB Output is correct
21 Correct 2005 ms 5520 KB Output is correct
22 Correct 1557 ms 5104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1025 ms 2352 KB Output is correct
8 Correct 1063 ms 2524 KB Output is correct
9 Correct 983 ms 4152 KB Output is correct
10 Correct 966 ms 4036 KB Output is correct
11 Correct 948 ms 3876 KB Output is correct
12 Correct 1304 ms 4164 KB Output is correct
13 Correct 994 ms 3736 KB Output is correct
14 Correct 1459 ms 3512 KB Output is correct
15 Correct 1552 ms 3392 KB Output is correct
16 Correct 2011 ms 4640 KB Output is correct
17 Correct 2045 ms 5568 KB Output is correct
18 Correct 2176 ms 5488 KB Output is correct
19 Correct 1705 ms 5572 KB Output is correct
20 Correct 2029 ms 5548 KB Output is correct
21 Correct 2005 ms 5520 KB Output is correct
22 Correct 1557 ms 5104 KB Output is correct
23 Correct 4929 ms 9152 KB Output is correct
24 Correct 5402 ms 9156 KB Output is correct
25 Correct 4172 ms 9152 KB Output is correct
26 Correct 5137 ms 9164 KB Output is correct
27 Correct 5434 ms 12000 KB Output is correct
28 Correct 3443 ms 5192 KB Output is correct
29 Correct 3401 ms 5208 KB Output is correct
30 Correct 3583 ms 5188 KB Output is correct
31 Correct 3436 ms 5216 KB Output is correct
32 Correct 4010 ms 11572 KB Output is correct
33 Correct 4460 ms 10896 KB Output is correct
34 Correct 4438 ms 11784 KB Output is correct
35 Correct 3901 ms 13612 KB Output is correct
36 Correct 5434 ms 11624 KB Output is correct
37 Correct 6661 ms 13384 KB Output is correct
38 Correct 4799 ms 10784 KB Output is correct
39 Correct 4736 ms 11816 KB Output is correct
40 Correct 4982 ms 10820 KB Output is correct
41 Correct 6159 ms 11552 KB Output is correct
42 Correct 6082 ms 11884 KB Output is correct