Submission #803746

# Submission time Handle Problem Language Result Execution time Memory
803746 2023-08-03T05:46:28 Z kshitij_sodani Radio Towers (IOI22_towers) C++17
17 / 100
807 ms 6436 KB
#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#include "towers.h"

#include <vector>
int st=0;
int n;
vector<int> it;
int tree[4*200001][2];
void build(int no,int l,int r){
	if(l==r){
		tree[no][0]=it[l];
		tree[no][1]=it[l];
	}
	else{
		int mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree[no][0]=min(tree[no*2+1][0],tree[no*2+2][0]);
		tree[no][1]=max(tree[no*2+1][1],tree[no*2+2][1]);
	}
}
int query(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb or aa>bb){
		return 1e9+1;
	}
	if(aa<=l and r<=bb){
		return tree[no][0];
	}
	int mid=(l+r)/2;
	return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
int query2(int no,int l,int r,int aa,int bb){
	if(r<aa or l>bb or aa>bb){
		return -1;
	}
	if(aa<=l and r<=bb){
		return tree[no][1];
	}
	int mid=(l+r)/2;
	return max(query2(no*2+1,l,mid,aa,bb),query2(no*2+2,mid+1,r,aa,bb));
}
vector<int> mm;

void init(int nn, vector<int> ita) {
	n=nn;
	it=ita;
	st=0;
	build(0,0,n-1);
	
}
int le[100001];
int re2[100001];
void rec(int l,int r){
	mm.clear();
	vector<pair<llo,llo>> ss;
	for(int i=r;i>=l;i--){
		while(ss.size()){
			if(ss.back().a<=it[i]){
				ss.pop_back();
			}
			else{
				break;
			}
		}
		re2[i]=n-1;
		if(ss.size()){
			re2[i]=ss.back().b-1;
		}
		ss.pb({it[i],i});
	}
	ss.clear();
	for(int i=l;i<=r;i++){
		while(ss.size()){
			if(ss.back().a<it[i]){
				ss.pop_back();
			}
			else{
				break;
			}
		}
		le[i]=0;
		if(ss.size()){
			le[i]=ss.back().b+1;
		}
		ss.pb({it[i],i});
	}
	for(int i=0;i<n;i++){
		int x=it[i]-query(0,0,n-1,le[i],i);
		int y=it[i]-query(0,0,n-1,i,re2[i]);
		mm.pb(min(x,y));
	}
	sort(mm.begin(),mm.end());

}
int re[100001][2];
int par[200001][20];
set<pair<int,int>> xx[2];
vector<pair<pair<int,int>,int>> yy;
map<int,int> kk;
int max_towers(int l, int r, int d) {
	if(st==0){
		rec(0,n-1);
		st=1;
	}
	llo low=mm.size();
	for(int j=19;j>=0;j--){
		if(low-(1<<j)>=0){
			if(mm[low-(1<<j)]>=d){
				low-=(1<<j);
			}
		}
	}
	llo ans=mm.size();
	ans-=low;
	ans++;
	return ans;

	if(kk.find(d)==kk.end()){
		llo ind=n;
		kk[d]++;

		yy.clear();
		xx[0].clear();
		xx[1].clear();

		for(int i=n-1;i>=0;i--){
			llo cur=query(0,0,n-1,i+1,ind-1);
			if(cur<=it[i]-d){
				llo zz=ind-1;
				while(zz>i){
					zz--;
					if(query(0,0,n-1,i+1,zz)<=it[i]-d){
						continue;
					}
					else{
						zz++;
						break;
					}
				}
				xx[0].insert({i,yy.size()});
				yy.pb({{i,zz},0});
				re[i][0]=zz;
				ind=zz;
			}
			else{
				re[i][0]=-1;
			}
		}
		ind=n;
		for(int i=n-1;i>=0;i--){
			llo cur=query2(0,0,n-1,i+1,ind-1);
			if(cur>=it[i]+d){
				llo zz=ind-1;
				while(zz>i){
					zz--;
					if(query2(0,0,n-1,i+1,zz)>=it[i]+d){
						continue;
					}
					else{
						zz++;
						break;
					}
				}
				re[i][1]=zz;
				xx[1].insert({i,yy.size()});
				yy.pb({{i,zz},1});
				ind=zz;
			}
			else{
				re[i][1]=-1;
			}
		}
		for(int i=0;i<yy.size();i++){
			par[i][0]=-1;
			auto j=xx[1-yy[i].b].lower_bound({yy[i].a.b,-1});
			if(j!=xx[1-yy[i].b].end()){
				par[i][0]=(*j).b;
			//	cout<<i<<":"<<(*j).b<<endl;
			}
		}
		for(int j=1;j<20;j++){
			for(int i=0;i<yy.size();i++){
				if(par[i][j-1]==-1){
					par[i][j]=-1;
				}
				else{
					par[i][j]=par[par[i][j-1]][j-1];
				}
			}
		}
	/*	for(auto j:yy){
			cout<<j.a.a<<","<<j.a.b<<","<<j.b<<endl;
		}
*/
		st=1;
	}
//	cout<<l<<",,,,"<<-1<<endl;
/*	for(auto j:xx[1]){
		cout<<j.a<<"//"<<j.b<<endl;
	}*/
	auto j=xx[1].lower_bound({l,-1});
	if(j==xx[1].end()){
		return 1;
	}
	int cot=(*j).b;
	//cout<<cot<<":::::"<<yy[cot].a.b<<endl;
//	cout<<((*j).a)<<"<<"<<endl;
	if(yy[cot].a.b>r){
		return 1;
	}
	//cout<<22<<endl;
	int su=1;
//cout<<cot<<";;"<<endl;
	//cout<<par[cot][0]<<":"<<par[cot][1]<<":"<<par[cot][2]<<endl;
	//cout<<par[cot][0]<<";;"<<yy[par[cot][0]].a.b<<";;"<<endl;
	for(int j=19;j>=0;j--){
		if(par[cot][j]>=0){
		//	cout<<yy[par[cot][j]].a.b<<";"<<j<<endl;
			if(yy[par[cot][j]].a.b<=r){
				cot=par[cot][j];
				su+=(1<<j);
			}
		}
	}
	//cout<<su<<",,,,"<<endl;
    return ((su)/2)+1;
}

Compilation message

towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:178:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |   for(int i=0;i<yy.size();i++){
      |               ~^~~~~~~~~~
towers.cpp:187:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  187 |    for(int i=0;i<yy.size();i++){
      |                ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 332 ms 3516 KB 1st lines differ - on the 1st token, expected: '1', found: '2'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '13', found: '131'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '13', found: '131'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 443 ms 4776 KB 1st lines differ - on the 1st token, expected: '11903', found: '33010'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 255 ms 1360 KB Output is correct
2 Correct 675 ms 4772 KB Output is correct
3 Correct 565 ms 4736 KB Output is correct
4 Correct 705 ms 4800 KB Output is correct
5 Correct 742 ms 4836 KB Output is correct
6 Correct 802 ms 4816 KB Output is correct
7 Correct 671 ms 4796 KB Output is correct
8 Correct 807 ms 6364 KB Output is correct
9 Correct 737 ms 6340 KB Output is correct
10 Correct 778 ms 5724 KB Output is correct
11 Correct 763 ms 6436 KB Output is correct
12 Correct 60 ms 4756 KB Output is correct
13 Correct 44 ms 4836 KB Output is correct
14 Correct 50 ms 4748 KB Output is correct
15 Correct 67 ms 6308 KB Output is correct
16 Correct 45 ms 6220 KB Output is correct
17 Correct 55 ms 4656 KB Output is correct
18 Correct 68 ms 4780 KB Output is correct
19 Correct 54 ms 4816 KB Output is correct
20 Correct 48 ms 4772 KB Output is correct
21 Correct 58 ms 4836 KB Output is correct
22 Correct 48 ms 4776 KB Output is correct
23 Correct 48 ms 4760 KB Output is correct
24 Correct 42 ms 6320 KB Output is correct
25 Correct 50 ms 6308 KB Output is correct
26 Correct 47 ms 5716 KB Output is correct
27 Correct 49 ms 6328 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 404 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 2 ms 336 KB Output is correct
40 Correct 2 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB 1st lines differ - on the 1st token, expected: '13', found: '131'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 332 ms 3516 KB 1st lines differ - on the 1st token, expected: '1', found: '2'
2 Halted 0 ms 0 KB -