Submission #132590

# Submission time Handle Problem Language Result Execution time Memory
132590 2019-07-19T07:53:19 Z tmwilliamlin168 Dancing Elephants (IOI11_elephants) C++14
100 / 100
5865 ms 12576 KB
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;

const int mxN=1.5e5, B=530, mxBC=(mxN-1)/B+1;
int n, l, *x, uc, bc, r[mxBC];
map<int, int> mp;
vector<int> v[mxBC];
vector<array<int, 2>> w[mxBC];

void init(int N, int L, int X[]) {
	n=N, l=L, x=X;
	for(int i=0; i<n; ++i)
		++mp[x[i]];
}

void bb(int i) {
	w[i]=vector<array<int, 2>>(v[i].size());
	for(int j1=(int)v[i].size()-1, j2=v[i].size(); ~j1; --j1) {
		while(v[i][j2-1]>v[i][j1]+l)
			--j2;
		w[i][j1]=j2<v[i].size()?array<int, 2>{w[i][j2][0]+1, w[i][j2][1]}:array<int, 2>{1, v[i][j1]+l};
	}
}
void bld() {
	bc=0;
	for(pair<int, int> p : mp) {
		if(!bc||v[bc-1].size()>=B)
			v[bc++].clear();
		v[bc-1].push_back(p.first);
	}
	for(int i=0; i<bc; ++i) {
		r[i]=i+1<bc?v[i+1][0]-1:1e9;
		bb(i);
	}
}

void upd(int x, bool a) {
	int c=-1;
	while(x>r[++c]);
	int p=lower_bound(v[c].begin(), v[c].end(), x)-v[c].begin();
	if(a)
		v[c].insert(v[c].begin()+p, x);
	else
		v[c].erase(v[c].begin()+p);
	bb(c);
}

int update(int i, int y) {
	if(uc++%B==0)
		bld();
	--mp[x[i]];
	if(!mp[x[i]]) {
		upd(x[i], 0);
		mp.erase(x[i]);
	}
	if(!mp[y])
		upd(y, 1);
	++mp[y];
	x[i]=y;
	int a=0;
	for(int i=0, c=-1; i<bc; ++i) {
		int p=upper_bound(v[i].begin(), v[i].end(), c)-v[i].begin();
		if(p<v[i].size()) {
			a+=w[i][p][0];
			c=w[i][p][1];
		}
	}
	return a;
}

Compilation message

elephants.cpp: In function 'void bb(int)':
elephants.cpp:22:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   w[i][j1]=j2<v[i].size()?array<int, 2>{w[i][j2][0]+1, w[i][j2][1]}:array<int, 2>{1, v[i][j1]+l};
            ~~^~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:64:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(p<v[i].size()) {
      ~^~~~~~~~~~~~
# 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 376 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 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 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 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 484 ms 1924 KB Output is correct
8 Correct 495 ms 2296 KB Output is correct
9 Correct 485 ms 4216 KB Output is correct
10 Correct 387 ms 4344 KB Output is correct
11 Correct 430 ms 4316 KB Output is correct
12 Correct 874 ms 4472 KB Output is correct
13 Correct 386 ms 4316 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 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 484 ms 1924 KB Output is correct
8 Correct 495 ms 2296 KB Output is correct
9 Correct 485 ms 4216 KB Output is correct
10 Correct 387 ms 4344 KB Output is correct
11 Correct 430 ms 4316 KB Output is correct
12 Correct 874 ms 4472 KB Output is correct
13 Correct 386 ms 4316 KB Output is correct
14 Correct 283 ms 2764 KB Output is correct
15 Correct 758 ms 3064 KB Output is correct
16 Correct 1335 ms 4728 KB Output is correct
17 Correct 1474 ms 5996 KB Output is correct
18 Correct 1594 ms 6008 KB Output is correct
19 Correct 831 ms 5888 KB Output is correct
20 Correct 1710 ms 6284 KB Output is correct
21 Correct 1486 ms 6152 KB Output is correct
22 Correct 626 ms 5880 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 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 484 ms 1924 KB Output is correct
8 Correct 495 ms 2296 KB Output is correct
9 Correct 485 ms 4216 KB Output is correct
10 Correct 387 ms 4344 KB Output is correct
11 Correct 430 ms 4316 KB Output is correct
12 Correct 874 ms 4472 KB Output is correct
13 Correct 386 ms 4316 KB Output is correct
14 Correct 283 ms 2764 KB Output is correct
15 Correct 758 ms 3064 KB Output is correct
16 Correct 1335 ms 4728 KB Output is correct
17 Correct 1474 ms 5996 KB Output is correct
18 Correct 1594 ms 6008 KB Output is correct
19 Correct 831 ms 5888 KB Output is correct
20 Correct 1710 ms 6284 KB Output is correct
21 Correct 1486 ms 6152 KB Output is correct
22 Correct 626 ms 5880 KB Output is correct
23 Correct 3733 ms 12152 KB Output is correct
24 Correct 3687 ms 12268 KB Output is correct
25 Correct 3125 ms 12120 KB Output is correct
26 Correct 3180 ms 12276 KB Output is correct
27 Correct 3343 ms 12144 KB Output is correct
28 Correct 2090 ms 2912 KB Output is correct
29 Correct 1995 ms 2796 KB Output is correct
30 Correct 2104 ms 2724 KB Output is correct
31 Correct 1999 ms 2880 KB Output is correct
32 Correct 3126 ms 12152 KB Output is correct
33 Correct 1206 ms 12312 KB Output is correct
34 Correct 2649 ms 12128 KB Output is correct
35 Correct 1264 ms 12272 KB Output is correct
36 Correct 78 ms 2680 KB Output is correct
37 Correct 2071 ms 12408 KB Output is correct
38 Correct 2789 ms 12152 KB Output is correct
39 Correct 2897 ms 12148 KB Output is correct
40 Correct 2806 ms 12188 KB Output is correct
41 Correct 5590 ms 12412 KB Output is correct
42 Correct 5865 ms 12576 KB Output is correct