답안 #386672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386672 2021-04-07T07:53:57 Z ogibogi2004 Road Construction (JOI21_road_construction) C++14
0 / 100
10000 ms 76528 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=250006;
const ll INF=4e9;
ll n,k;
vector<ll>output;
struct point
{
	ll x,y;
	bool operator<(point const& other)const
	{
		return make_pair(x,y)<make_pair(other.x,other.y);
	}
};
struct BIT
{
	ll t[MAXN];
	void init()
	{
		for(ll i=0;i<MAXN;i++)t[i]=0;
	}
	void update(ll idx,ll val)
	{
		++idx;
		for(;idx<MAXN;idx+=idx&(-idx))
		{
			t[idx]+=val;
		}
	}
	ll query(ll idx)
	{
		++idx;
		ll ret=0;
		for(;idx>0;idx-=idx&(-idx))
		{
			ret+=t[idx];
		}
		return ret;
	}
	ll query(ll l,ll r)
	{
		return query(r)-query(l-1);
	}
}fen;
struct event
{
	ll time,idx,flag;
	bool operator<(event const& other)const
	{
		return make_pair(make_pair(time,flag),idx)<make_pair(make_pair(other.time,other.flag),other.idx);
	}
};
vector<point>v;
map<ll,ll>compressed_y;
map<ll,ll>compressed_x;
set<ll>xs;
set<ll>ys;
vector<event>e;
bool ok(ll d)
{
	fen.init();
	e.clear();
	for(ll i=0;i<v.size();++i)
	{
		e.push_back({compressed_x[v[i].x],i,1});
		auto it=xs.lower_bound(v[i].x+d+1);
		e.push_back({compressed_x[(*it)],i,-1});
	}
	ll cnt=0;
	sort(e.begin(),e.end());
	for(ll i=0;i<e.size();++i)
	{
		//cout<<e[i].time<<" "<<e[i].idx<<" "<<e[i].flag<<" "<<v[e[i].idx].x<<" "<<v[e[i].idx].y<<endl;
		if(e[i].flag==1)
		{
			auto l=ys.lower_bound(v[e[i].idx].y-d);
			auto r=ys.lower_bound(v[e[i].idx].y+d+1);
			r--;
			//cout<<"#$$#%$  "<<(*l)<<" "<<(*r)<<endl;
			cnt+=fen.query(compressed_y[(*l)],compressed_y[(*r)]);
			//cout<<cnt<<endl;
			fen.update(compressed_y[v[e[i].idx].y],+1);
		}
		else
		{
			fen.update(compressed_y[v[e[i].idx].y],-1);
		}
	}
	//cout<<"---------------\n";
	//cout<<d<<" "<<cnt<<endl;
	return cnt>=k;
}
void find(ll d)
{
	set<pair<ll,ll> >s;
	e.clear();
	for(ll i=0;i<v.size();++i)
	{
		e.push_back({compressed_x[v[i].x],i,1});
		auto it=xs.lower_bound(v[i].x+d+1);
		e.push_back({compressed_x[(*it)],i,-1});
	}
	sort(e.begin(),e.end());
	for(ll i=0;i<e.size();++i)
	{
		if(e[i].flag==1)
		{
			auto it=s.lower_bound({v[e[i].idx].y-d,-INF});
			for(;it!=s.end();++it)
			{
				if(abs((*it).first-v[e[i].idx].y)>d)
				{
					break;
				}
				output.push_back(max(abs((*it).first-v[e[i].idx].y),v[e[i].idx].x-(*it).second));
			}
			s.insert({v[e[i].idx].y,v[e[i].idx].x});
		}
		else
		{
			s.erase({v[e[i].idx].y,v[e[i].idx].x});
		}
	}
	sort(output.begin(),output.end());
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	cin>>n>>k;
	for(ll i=1;i<=n;++i)
	{
		ll x,y;
		cin>>x>>y;
		v.push_back({x+y,x-y});
		xs.insert(v.back().x);
		ys.insert(v.back().y);
	}
	xs.insert(-INF);
	xs.insert(INF);
	ys.insert(-INF);
	ys.insert(INF);
	ll cnt=0;
	for(auto xd:xs)
	{
		++cnt;
		compressed_x[xd]=cnt;
	}
	cnt=0;
	for(auto xd:ys)
	{
		++cnt;
		compressed_y[xd]=cnt;
	}
	sort(v.begin(),v.end());
	/*cout<<"v:\n";
	for(auto xd:v)
	{
		cout<<xd.x<<" "<<xd.y<<endl;
	}
	cout<<endl<<endl;*/
	ll low=0,high=INF,mid,ans=-1;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(ok(mid))
		{
			ans=mid;
			high=mid-1;
		}
		else low=mid+1;
	}
	//cout<<ans<<endl;
	find(ans);
	//cout<<output.size()<<endl;
	for(ll i=0;i<output.size()&&i<k;i++)cout<<output[i]<<'\n';
	for(ll i=0;i<k-output.size();i++)cout<<ans<<endl;
return 0;
}

Compilation message

road_construction.cpp: In function 'bool ok(long long int)':
road_construction.cpp:64:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |  for(ll i=0;i<v.size();++i)
      |             ~^~~~~~~~~
road_construction.cpp:72:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for(ll i=0;i<e.size();++i)
      |             ~^~~~~~~~~
road_construction.cpp: In function 'void find(long long int)':
road_construction.cpp:98:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |  for(ll i=0;i<v.size();++i)
      |             ~^~~~~~~~~
road_construction.cpp:105:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |  for(ll i=0;i<e.size();++i)
      |             ~^~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:177:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  177 |  for(ll i=0;i<output.size()&&i<k;i++)cout<<output[i]<<'\n';
      |             ~^~~~~~~~~~~~~~
road_construction.cpp:178:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
  178 |  for(ll i=0;i<k-output.size();i++)cout<<ans<<endl;
      |             ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 7260 KB Output is correct
2 Correct 89 ms 7260 KB Output is correct
3 Incorrect 564 ms 3436 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10039 ms 76496 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10105 ms 76528 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10105 ms 76528 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 7260 KB Output is correct
2 Correct 89 ms 7260 KB Output is correct
3 Incorrect 564 ms 3436 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 7260 KB Output is correct
2 Correct 89 ms 7260 KB Output is correct
3 Incorrect 564 ms 3436 KB Output isn't correct
4 Halted 0 ms 0 KB -