Submission #398426

#TimeUsernameProblemLanguageResultExecution timeMemory
398426kylych03Jakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
252 ms65332 KiB
// Restart
// += O(logn) ; + = O(n)
 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
 
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fst first
#define snd second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz size()
#define FORN(i,j,n) for(int i=j; i<(int)n;i++) 
#define FOR(i,n) FORN(i,0,n)
#define FORIT(i,x) for( auto i = x.begin() ; i != x.end() ; i++ )
#define MOD 998244353LL
#define MOD1 1000000007LL
#define LIM 262150
#define ones(x) __builtin_popcount(x)
#define trace(x)    cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y,z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define INF 1000000000
#define in(x)  cin>>x;
#define out(x) cout<<x;
#define MAXIMO 1LL<<60
 
#define loga2(x) __builtin_ctzll(x)
//SOLO PARA POTENCIAS DE 2
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef pair <int,ii> iii;
typedef vector <string> vs;
typedef vector <ii> vii;
typedef vector <iii> viii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
 
int n,m;
 
set<int> walks[30005];
 
vii G[30005];
int D[30005];
 
void Dijkstra(int start) {
    priority_queue<ii, vii, greater<ii> > pq;
 	D[start] = 0;
 
    pq.push({0, start});
 
    while(!pq.empty()) {
        int v = pq.top().snd; int d = pq.top().fst;
 
        pq.pop();
        //trace2(v,d);
        if(d <= D[v]){
	        for(auto uw : G[v]) {
	            int u = uw.fst;
	            int w = uw.snd;
	            if(D[u] > w + d) {
	            	D[u] = w + d;
	                pq.push({D[u], u});
	            }
	        }
	    }
    }
}
 
 
void go(){
	cin>>n>>m;
 
	int pos , jump;
	int t0, t1;
 
	cin>> t0 >> jump; walks[t0].insert(jump);//0
	cin>> t1 >> jump; //1
 
	for(int i=2; i<m; i++){
		cin>> pos >> jump;
		walks[pos].insert(jump);
	}
 
	FOR(i,n) D[i] = INF;
 
	FOR(i,n){
		for(auto j : walks[i]){
			int cont = 1;
			while(1) {
                int tower = i + cont * j;
                if( tower >= n )break;
 
                G[i].pb({tower, cont});
                if(walks[tower].count(j))break;
                cont += 1;
            }
		}
 
		for(auto j : walks[i]){
			int cont = 1;
			while(1) {
                int tower = i - cont * j;
                if( tower < 0 )break;
 
                G[i].pb({tower, cont});
                if(walks[tower].count(j))break;
                cont += 1;
            }
		}
 
	}
 
	Dijkstra(t0);
    cout << (D[t1] == INF ? -1 : D[t1]) << "\n";
 
 
}
int main() {
  go();
}
#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...