Submission #833706

# Submission time Handle Problem Language Result Execution time Memory
833706 2023-08-22T08:06:05 Z ttamx Dancing Elephants (IOI11_elephants) C++14
100 / 100
3526 ms 16424 KB
#include "elephants.h"
#include<bits/stdc++.h>

using namespace std;

const int N=150005;
const int K=390;
const int inf=1e9;

int n,L;
int X[N];

struct elephant{
	int id,pos,needed,covered;
	elephant(){}
	elephant(int id,int pos):id(id),pos(pos){}
	bool operator<(const elephant &o)const{
		return pos<o.pos;
	}
}elep[N];

vector<elephant> block[K];
int blockid[N];

void computeblock(int i){
	deque<elephant> dq;
	for(int j=block[i].size()-1;j>=0;j--){
		if(dq.empty()||block[i][j].pos+L>=dq.back().pos){
			block[i][j].needed=1;
			block[i][j].covered=block[i][j].pos+L;
		}else{
			while(dq.size()>1&&dq.end()[-2].pos>block[i][j].pos+L)dq.pop_back();
			block[i][j].needed=dq.back().needed+1;
			block[i][j].covered=dq.back().covered;
		}
		dq.emplace_front(block[i][j]);
	}
}

void build(){
	for(int i=0;i<K;i++)block[i].clear();
	for(int i=0;i<n;i++){
		block[i/K].emplace_back(elep[i]);
		blockid[elep[i].id]=i/K;
	}
	for(int i=0;i<K;i++)computeblock(i);
}

void init(int N, int _L, int _X[]){
	n=N,L=_L;
	for(int i=0;i<n;i++)X[i]=_X[i];
	for(int i=0;i<n;i++)elep[i]=elephant(i,X[i]);
	build();
}

int findblock(int pos){
	int l=0,r=K-1;
	while(l<r){
		int m=(l+r+1)/2;
		if(!block[m].empty()&&block[m][0].pos<=pos)l=m;
		else r=m-1;
	}
	return l;
}

int counter=0;

int update(int i, int y){
	if(counter==K){
		counter=0;
		int idx=0;
		for(int j=0;j<K;j++)for(auto e:block[j])elep[idx++]=e;
		build();
	}
	counter++;
	int id=blockid[i];
	auto it=block[id].begin();
	while(it->id!=i)it++;
	auto tmp=*it;
	block[id].erase(it);
	computeblock(id);
	X[i]=y;
	tmp.pos=y;
	id=findblock(X[i]);
	blockid[i]=id;
	it=block[id].begin();
	while(it!=block[id].end()&&it->pos<=X[i])it++;
	block[id].insert(it,tmp);
	computeblock(id);
	int ans=0;
	int pos=-inf;
	for(int i=0;i<K;i++){
		auto it=upper_bound(block[i].begin(),block[i].end(),elephant(-1,pos));
		if(it==block[i].end())continue;
		ans+=it->needed;
		pos=it->covered;
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 500 ms 1528 KB Output is correct
8 Correct 523 ms 1780 KB Output is correct
9 Correct 646 ms 3324 KB Output is correct
10 Correct 441 ms 3348 KB Output is correct
11 Correct 428 ms 4616 KB Output is correct
12 Correct 712 ms 4800 KB Output is correct
13 Correct 447 ms 4476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 500 ms 1528 KB Output is correct
8 Correct 523 ms 1780 KB Output is correct
9 Correct 646 ms 3324 KB Output is correct
10 Correct 441 ms 3348 KB Output is correct
11 Correct 428 ms 4616 KB Output is correct
12 Correct 712 ms 4800 KB Output is correct
13 Correct 447 ms 4476 KB Output is correct
14 Correct 702 ms 3708 KB Output is correct
15 Correct 782 ms 3704 KB Output is correct
16 Correct 1120 ms 5384 KB Output is correct
17 Correct 1202 ms 6580 KB Output is correct
18 Correct 1215 ms 6520 KB Output is correct
19 Correct 918 ms 6700 KB Output is correct
20 Correct 1173 ms 6604 KB Output is correct
21 Correct 1067 ms 6552 KB Output is correct
22 Correct 769 ms 6220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 500 ms 1528 KB Output is correct
8 Correct 523 ms 1780 KB Output is correct
9 Correct 646 ms 3324 KB Output is correct
10 Correct 441 ms 3348 KB Output is correct
11 Correct 428 ms 4616 KB Output is correct
12 Correct 712 ms 4800 KB Output is correct
13 Correct 447 ms 4476 KB Output is correct
14 Correct 702 ms 3708 KB Output is correct
15 Correct 782 ms 3704 KB Output is correct
16 Correct 1120 ms 5384 KB Output is correct
17 Correct 1202 ms 6580 KB Output is correct
18 Correct 1215 ms 6520 KB Output is correct
19 Correct 918 ms 6700 KB Output is correct
20 Correct 1173 ms 6604 KB Output is correct
21 Correct 1067 ms 6552 KB Output is correct
22 Correct 769 ms 6220 KB Output is correct
23 Correct 3157 ms 14324 KB Output is correct
24 Correct 3162 ms 14348 KB Output is correct
25 Correct 2779 ms 14432 KB Output is correct
26 Correct 2712 ms 14336 KB Output is correct
27 Correct 2825 ms 14176 KB Output is correct
28 Correct 1677 ms 5272 KB Output is correct
29 Correct 1644 ms 5300 KB Output is correct
30 Correct 1653 ms 5272 KB Output is correct
31 Correct 1666 ms 5304 KB Output is correct
32 Correct 2549 ms 13772 KB Output is correct
33 Correct 2525 ms 13104 KB Output is correct
34 Correct 2525 ms 13976 KB Output is correct
35 Correct 2350 ms 14268 KB Output is correct
36 Correct 2616 ms 13756 KB Output is correct
37 Correct 3058 ms 16424 KB Output is correct
38 Correct 2501 ms 13100 KB Output is correct
39 Correct 2777 ms 14012 KB Output is correct
40 Correct 2514 ms 13004 KB Output is correct
41 Correct 3524 ms 13980 KB Output is correct
42 Correct 3526 ms 14072 KB Output is correct