Submission #1019905

#TimeUsernameProblemLanguageResultExecution timeMemory
1019905beaconmcFire (BOI24_fire)C++14
100 / 100
1145 ms189240 KiB
#include <bits/stdc++.h>


typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)

using namespace std;

vector<vector<ll>> normal;
map<ll, ll> maxis;
map<ll,ll> par;

ll anc[500005][30];


ll ancc(ll x, ll k){
	if (x==1000000000) return 1000000000;
	if (anc[x][k] != -1) return anc[x][k];
	if (k==0){
		if (par[x] == x) return anc[x][k] = 1000000000;
		return anc[x][k] = par[x];
	}
	return anc[x][k] = ancc(ancc(x, k-1), k-1);
}

int main(){
	FOR(i,0,500005) FOR(j,0,30) anc[i][j] = -1;
	ll n,m;
	cin >> n >> m;

	FOR(i,0,n){
		ll a,b;
		cin >> a >> b;
		if (a<b) normal.push_back({a,b});
		else normal.push_back({a,m+b});
	}

	sort(normal.begin(), normal.end());
	set<ll> things;

	for (auto&i : normal){
		things.insert(i[0]);
		things.insert(i[1]);
	}
	ll cur = 0;
	ll maxi = -1;
	ll maxting = -1;


	for (auto&i : things){
		while (cur < normal.size()){
			if (normal[cur][0] <= i){
				if (normal[cur][1] > maxi){
					maxi = normal[cur][1];
					maxting = cur;
				}
			}else break;
			cur++;
		}
		maxis[i] = maxting;
	}

	FOR(i,0,normal.size()){
		par[i] = maxis[normal[i][1]];
	}
	ll realans = 1000000000;


	FOR(i,0,normal.size()){

		ll orig = normal[i][0];
		
		ll ans = 0;

		ll lo = 0;
		ll hi = 10000000;


		while (lo < hi){

			ll mid = (lo+hi)/2;
			ll idk = i;
			FOR(j,0,30){
				if (mid&(1<<j)){
					idk = ancc(idk, j);
				}
			}
			if (idk == 1000000000 || normal[idk][1] - orig >= m){
				hi = mid;
			}else{
				lo = mid+1;
			}
		}
		ll shit = i;
		FOR(j,0,30){
			if (lo&(1<<j)){
				shit = ancc(shit, j);
			}
		}


		if (shit < normal.size() && normal[shit][1] - orig >= m) realans = min(realans, lo+1);
		



		


	}
	if (realans == 1000000000) cout << -1 << endl;
	else cout << realans << endl;








}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:52:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |   while (cur < normal.size()){
      |          ~~~~^~~~~~~~~~~~~~~
Main.cpp:5:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define FOR(i,x,y) for(ll i=x; i<y; i++)
......
   64 |  FOR(i,0,normal.size()){
      |      ~~~~~~~~~~~~~~~~~           
Main.cpp:64:2: note: in expansion of macro 'FOR'
   64 |  FOR(i,0,normal.size()){
      |  ^~~
Main.cpp:5:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define FOR(i,x,y) for(ll i=x; i<y; i++)
......
   70 |  FOR(i,0,normal.size()){
      |      ~~~~~~~~~~~~~~~~~           
Main.cpp:70:2: note: in expansion of macro 'FOR'
   70 |  FOR(i,0,normal.size()){
      |  ^~~
Main.cpp:103:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |   if (shit < normal.size() && normal[shit][1] - orig >= m) realans = min(realans, lo+1);
      |       ~~~~~^~~~~~~~~~~~~~~
Main.cpp:74:6: warning: unused variable 'ans' [-Wunused-variable]
   74 |   ll ans = 0;
      |      ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...