Submission #406256

# Submission time Handle Problem Language Result Execution time Memory
406256 2021-05-17T09:56:06 Z pure_mem Dancing Elephants (IOI11_elephants) C++14
97 / 100
9000 ms 7616 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 = 300, 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){
	vector< pair<int, int> > tmp;
	for(pair<int, int> v: cur){
		if(keep){
			if(val.X != -1 && v > val)
				tmp.push_back(val), val.X = -1;	
		}
		else{
			if(val == v)
				continue;
		}
		tmp.push_back(v);
	}
	if(keep && val.X != -1)
		tmp.push_back(val);
	cur.swap(tmp), tmp.clear();
}

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(){		
	sort(ps, ps + n, [](int x, int y){return p[x] < p[y];});
	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; 
	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){
    	p[i] = y, cntQ = 0;
    	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 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 561 ms 1300 KB Output is correct
8 Correct 624 ms 1584 KB Output is correct
9 Correct 708 ms 2780 KB Output is correct
10 Correct 1453 ms 2784 KB Output is correct
11 Correct 1435 ms 2788 KB Output is correct
12 Correct 1551 ms 2788 KB Output is correct
13 Correct 1469 ms 2788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 561 ms 1300 KB Output is correct
8 Correct 624 ms 1584 KB Output is correct
9 Correct 708 ms 2780 KB Output is correct
10 Correct 1453 ms 2784 KB Output is correct
11 Correct 1435 ms 2788 KB Output is correct
12 Correct 1551 ms 2788 KB Output is correct
13 Correct 1469 ms 2788 KB Output is correct
14 Correct 701 ms 1944 KB Output is correct
15 Correct 901 ms 2032 KB Output is correct
16 Correct 1995 ms 3016 KB Output is correct
17 Correct 2807 ms 3760 KB Output is correct
18 Correct 2890 ms 3760 KB Output is correct
19 Correct 2426 ms 3780 KB Output is correct
20 Correct 2637 ms 3752 KB Output is correct
21 Correct 2937 ms 3752 KB Output is correct
22 Correct 2842 ms 3760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 561 ms 1300 KB Output is correct
8 Correct 624 ms 1584 KB Output is correct
9 Correct 708 ms 2780 KB Output is correct
10 Correct 1453 ms 2784 KB Output is correct
11 Correct 1435 ms 2788 KB Output is correct
12 Correct 1551 ms 2788 KB Output is correct
13 Correct 1469 ms 2788 KB Output is correct
14 Correct 701 ms 1944 KB Output is correct
15 Correct 901 ms 2032 KB Output is correct
16 Correct 1995 ms 3016 KB Output is correct
17 Correct 2807 ms 3760 KB Output is correct
18 Correct 2890 ms 3760 KB Output is correct
19 Correct 2426 ms 3780 KB Output is correct
20 Correct 2637 ms 3752 KB Output is correct
21 Correct 2937 ms 3752 KB Output is correct
22 Correct 2842 ms 3760 KB Output is correct
23 Correct 5852 ms 7612 KB Output is correct
24 Correct 6741 ms 7616 KB Output is correct
25 Correct 4993 ms 7608 KB Output is correct
26 Execution timed out 9052 ms 7536 KB Time limit exceeded
27 Halted 0 ms 0 KB -