Submission #264719

#TimeUsernameProblemLanguageResultExecution timeMemory
264719jeqchoJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1096 ms5096 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<pair<int,int>> vpi; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define pb push_back #define rsz resize #define sz(x) int(x.size()) #define all(x) begin(x), end(x) #define f first #define s second int N,M; int const m=3e4, n=3e4; int B[m]; int P[m]; vi sky[n]; int dist[m]; bitset<n> vis; bitset<m> pro; int sqrtn; void activate(int loc, map<int,int> &adjnow) { int newloc; trav(now, sky[loc]) { int power; if(now<0) { power = -now; } else power = P[now]; if(power>0) { for(int i=1; loc+i*power<N; ++i) { newloc = loc+i*power; if(vis[newloc])continue; if(adjnow.find(newloc)!=adjnow.end()) { adjnow[newloc] = min(adjnow[newloc], i); } else adjnow.insert({newloc, i}); } for(int i=-1; loc+i*power>=0; --i) { newloc = loc+i*power; if(vis[newloc])continue; if(adjnow.find(newloc)!=adjnow.end()) { adjnow[newloc] = min(adjnow[newloc], -i); } else adjnow.insert({newloc, -i}); } } else { newloc = loc+power; if(newloc<N&&!vis[newloc]) { if(adjnow.find(newloc)!=adjnow.end()) { adjnow[newloc] = min(adjnow[newloc], 1); } else adjnow.insert({newloc, 1}); } newloc = loc-power; if(newloc>=0&&!vis[newloc]) { if(adjnow.find(newloc)!=adjnow.end()) { adjnow[newloc] = min(adjnow[newloc], 1); } else adjnow.insert({newloc, 1}); } } } } void dij(int x) { priority_queue<pair<int,pii>>q; dist[x]=0; q.push({0,{x,0}}); int b,w; map<int,int> adjnow; while(!q.empty()) { int a = q.top().s.f; int gift = q.top().s.s; q.pop(); if(vis[a])continue; vis[a]=1; if(a==B[1])return; // if(gift!=0&&gift<=sqrtn)sky[a].pb(-gift); adjnow.clear(); activate(a, adjnow); trav(e, adjnow) { b=e.f; w=e.s; if(dist[a]+w<dist[b]) { dist[b]=dist[a]+w; q.push({-dist[b],{b,w}}); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>N>>M; sqrtn=sqrt(N); F0R(i,M) { cin>>B[i]>>P[i]; sky[B[i]].pb(i); } int INF=1e9; F0R(loc,N) { dist[loc]=INF; } dij(B[0]); if(dist[B[1]]==INF)cout<<-1<<'\n'; else cout<<dist[B[1]]<<'\n'; return 0; }

Compilation message (stderr)

skyscraper.cpp: In function 'void dij(int)':
skyscraper.cpp:102:13: warning: unused variable 'gift' [-Wunused-variable]
  102 |         int gift = q.top().s.s;
      |             ^~~~
#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...