#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=30010;
const int mxM=4300000;
const int mxK=100001;
const int MOD=1000000007;
const ll INF=9223372036854775807;
int N, M;
vector <pii> BP;
int l[mxM], r[mxM], in[mxM];
vector <int> out[mxN];
int idx;
int X, Y;
int lst[mxM], lc, rc;
int dis[4*mxM];
bool Chk[mxM];
bool cmp1(pii a, pii b)
{
if(a.sec!=b.sec) return a.sec<b.sec;
if(a.fir%a.sec!=b.fir%b.sec) return a.fir%a.sec<b.fir%b.sec;
return a.fir<b.fir;
}
void zero_one_bfs()
{
while(lc!=rc)
{
int now=lst[lc];
lc++;
if(Chk[now]) continue;
Chk[now]=true;
if(now<N)
{
for(int nxt : out[now])
{
if(dis[nxt]>dis[now])
{
dis[nxt]=dis[now];
lst[--lc]=nxt;
}
}
}
else
{
if(l[now]!=-1)
{
if(dis[l[now]]>dis[now]+1)
{
dis[l[now]]=dis[now]+1;
lst[rc++]=l[now];
//lst.push_back(l[now]);
}
}
if(r[now]!=-1)
{
if(dis[r[now]]>dis[now]+1)
{
dis[r[now]]=dis[now]+1;
lst[rc++]=r[now];
//lst.push_back(r[now]);
}
}
if(dis[in[now]]>dis[now])
{
dis[in[now]]=dis[now];
lst[--lc]=in[now];
//lst.push_front(in[now]);
}
}
}
}
int main()
{
gibon
cin >> N >> M;
idx=N;
BP.resize(M);
for(int i=0;i<M;i++)
{
cin >> BP[i].fir >> BP[i].sec;
if(i==0) X=BP[i].fir;
if(i==1) Y=BP[i].fir;
}
sort(BP.begin(), BP.end(), cmp1);
for(int i=0;i<mxM;i++) l[i]=r[i]=in[i]=-1;
int cur=0;
while(cur<M)
{
int ncur=cur;
bool jog=false;
for(int j=BP[ncur].fir%BP[ncur].sec;j<N;j+=BP[ncur].sec)
{
if(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec)
{
out[j].push_back(idx);
while(cur<M && BP[cur].fir==j && BP[cur].sec==BP[ncur].sec) cur++;
}
in[idx]=j;
if(!jog) jog=true;
else
{
l[idx]=idx-1;
r[idx-1]=idx;
}
idx++;
if(idx>=mxM) return 0;
}
}
for(int i=0;i<idx;i++)
{
dis[i]=MOD;
}
dis[X]=0;
lst[2*mxM]=X;
lc=2*mxM, rc=2*mxM+1;
zero_one_bfs();
if(dis[Y]==MOD) cout << -1;
else cout << dis[Y];
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:124:14: warning: array subscript 8600000 is above array bounds of 'int [4300000]' [-Warray-bounds]
124 | lst[2*mxM]=X;
| ~~~~~~~~~^
skyscraper.cpp:25:5: note: while referencing 'lst'
25 | int lst[mxM], lc, rc;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
51532 KB |
Output is correct |
2 |
Correct |
23 ms |
51520 KB |
Output is correct |
3 |
Correct |
24 ms |
51396 KB |
Output is correct |
4 |
Correct |
26 ms |
51432 KB |
Output is correct |
5 |
Correct |
22 ms |
51464 KB |
Output is correct |
6 |
Correct |
24 ms |
51404 KB |
Output is correct |
7 |
Correct |
24 ms |
51412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
51532 KB |
Output is correct |
2 |
Correct |
22 ms |
51508 KB |
Output is correct |
3 |
Correct |
23 ms |
51512 KB |
Output is correct |
4 |
Correct |
23 ms |
51416 KB |
Output is correct |
5 |
Correct |
23 ms |
51412 KB |
Output is correct |
6 |
Correct |
26 ms |
51448 KB |
Output is correct |
7 |
Correct |
23 ms |
51532 KB |
Output is correct |
8 |
Correct |
23 ms |
51532 KB |
Output is correct |
9 |
Correct |
21 ms |
51532 KB |
Output is correct |
10 |
Correct |
22 ms |
51568 KB |
Output is correct |
11 |
Correct |
24 ms |
51536 KB |
Output is correct |
12 |
Correct |
25 ms |
51532 KB |
Output is correct |
13 |
Correct |
24 ms |
51536 KB |
Output is correct |
14 |
Correct |
24 ms |
51508 KB |
Output is correct |
15 |
Correct |
23 ms |
51512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
51532 KB |
Output is correct |
2 |
Correct |
27 ms |
51428 KB |
Output is correct |
3 |
Correct |
23 ms |
51432 KB |
Output is correct |
4 |
Correct |
23 ms |
51508 KB |
Output is correct |
5 |
Correct |
25 ms |
51440 KB |
Output is correct |
6 |
Correct |
23 ms |
51440 KB |
Output is correct |
7 |
Correct |
23 ms |
51404 KB |
Output is correct |
8 |
Correct |
23 ms |
51508 KB |
Output is correct |
9 |
Correct |
23 ms |
51492 KB |
Output is correct |
10 |
Correct |
22 ms |
51532 KB |
Output is correct |
11 |
Correct |
23 ms |
51532 KB |
Output is correct |
12 |
Correct |
23 ms |
51456 KB |
Output is correct |
13 |
Correct |
23 ms |
51532 KB |
Output is correct |
14 |
Correct |
26 ms |
51524 KB |
Output is correct |
15 |
Correct |
23 ms |
51468 KB |
Output is correct |
16 |
Correct |
23 ms |
51544 KB |
Output is correct |
17 |
Correct |
25 ms |
51608 KB |
Output is correct |
18 |
Correct |
23 ms |
51512 KB |
Output is correct |
19 |
Correct |
23 ms |
51528 KB |
Output is correct |
20 |
Correct |
23 ms |
51520 KB |
Output is correct |
21 |
Correct |
23 ms |
51536 KB |
Output is correct |
22 |
Correct |
22 ms |
51532 KB |
Output is correct |
23 |
Correct |
23 ms |
51588 KB |
Output is correct |
24 |
Correct |
25 ms |
51620 KB |
Output is correct |
25 |
Correct |
27 ms |
51584 KB |
Output is correct |
26 |
Correct |
24 ms |
51524 KB |
Output is correct |
27 |
Correct |
24 ms |
51572 KB |
Output is correct |
28 |
Correct |
24 ms |
51620 KB |
Output is correct |
29 |
Correct |
24 ms |
51788 KB |
Output is correct |
30 |
Correct |
24 ms |
51524 KB |
Output is correct |
31 |
Correct |
24 ms |
51696 KB |
Output is correct |
32 |
Correct |
23 ms |
51624 KB |
Output is correct |
33 |
Correct |
26 ms |
52136 KB |
Output is correct |
34 |
Correct |
26 ms |
52112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
51404 KB |
Output is correct |
2 |
Correct |
23 ms |
51464 KB |
Output is correct |
3 |
Correct |
22 ms |
51396 KB |
Output is correct |
4 |
Correct |
22 ms |
51504 KB |
Output is correct |
5 |
Correct |
23 ms |
51488 KB |
Output is correct |
6 |
Correct |
23 ms |
51524 KB |
Output is correct |
7 |
Correct |
25 ms |
51424 KB |
Output is correct |
8 |
Correct |
27 ms |
51404 KB |
Output is correct |
9 |
Correct |
23 ms |
51532 KB |
Output is correct |
10 |
Correct |
26 ms |
51448 KB |
Output is correct |
11 |
Correct |
25 ms |
51552 KB |
Output is correct |
12 |
Correct |
29 ms |
51544 KB |
Output is correct |
13 |
Correct |
26 ms |
51436 KB |
Output is correct |
14 |
Correct |
24 ms |
51512 KB |
Output is correct |
15 |
Correct |
23 ms |
51560 KB |
Output is correct |
16 |
Correct |
22 ms |
51552 KB |
Output is correct |
17 |
Correct |
25 ms |
51584 KB |
Output is correct |
18 |
Correct |
28 ms |
51532 KB |
Output is correct |
19 |
Correct |
23 ms |
51532 KB |
Output is correct |
20 |
Correct |
24 ms |
51536 KB |
Output is correct |
21 |
Correct |
23 ms |
51460 KB |
Output is correct |
22 |
Correct |
23 ms |
51556 KB |
Output is correct |
23 |
Correct |
23 ms |
51532 KB |
Output is correct |
24 |
Correct |
24 ms |
51536 KB |
Output is correct |
25 |
Correct |
24 ms |
51524 KB |
Output is correct |
26 |
Correct |
24 ms |
51568 KB |
Output is correct |
27 |
Correct |
31 ms |
51568 KB |
Output is correct |
28 |
Correct |
25 ms |
51728 KB |
Output is correct |
29 |
Correct |
24 ms |
51788 KB |
Output is correct |
30 |
Correct |
24 ms |
51548 KB |
Output is correct |
31 |
Correct |
26 ms |
51660 KB |
Output is correct |
32 |
Correct |
23 ms |
51568 KB |
Output is correct |
33 |
Correct |
27 ms |
52220 KB |
Output is correct |
34 |
Correct |
27 ms |
52044 KB |
Output is correct |
35 |
Correct |
36 ms |
52408 KB |
Output is correct |
36 |
Correct |
26 ms |
51684 KB |
Output is correct |
37 |
Correct |
36 ms |
52648 KB |
Output is correct |
38 |
Correct |
40 ms |
52676 KB |
Output is correct |
39 |
Correct |
41 ms |
52680 KB |
Output is correct |
40 |
Correct |
40 ms |
52744 KB |
Output is correct |
41 |
Correct |
41 ms |
52704 KB |
Output is correct |
42 |
Correct |
33 ms |
51792 KB |
Output is correct |
43 |
Correct |
33 ms |
51788 KB |
Output is correct |
44 |
Correct |
33 ms |
51788 KB |
Output is correct |
45 |
Incorrect |
60 ms |
54164 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
51484 KB |
Output is correct |
2 |
Correct |
22 ms |
51404 KB |
Output is correct |
3 |
Correct |
23 ms |
51424 KB |
Output is correct |
4 |
Correct |
22 ms |
51404 KB |
Output is correct |
5 |
Correct |
23 ms |
51488 KB |
Output is correct |
6 |
Correct |
25 ms |
51532 KB |
Output is correct |
7 |
Correct |
23 ms |
51484 KB |
Output is correct |
8 |
Correct |
24 ms |
51468 KB |
Output is correct |
9 |
Correct |
24 ms |
51532 KB |
Output is correct |
10 |
Correct |
23 ms |
51524 KB |
Output is correct |
11 |
Correct |
25 ms |
51464 KB |
Output is correct |
12 |
Correct |
23 ms |
51444 KB |
Output is correct |
13 |
Correct |
25 ms |
51436 KB |
Output is correct |
14 |
Correct |
25 ms |
51588 KB |
Output is correct |
15 |
Correct |
26 ms |
51500 KB |
Output is correct |
16 |
Correct |
26 ms |
51520 KB |
Output is correct |
17 |
Correct |
23 ms |
51648 KB |
Output is correct |
18 |
Correct |
23 ms |
51556 KB |
Output is correct |
19 |
Correct |
23 ms |
51548 KB |
Output is correct |
20 |
Correct |
23 ms |
51604 KB |
Output is correct |
21 |
Correct |
23 ms |
51568 KB |
Output is correct |
22 |
Correct |
26 ms |
51592 KB |
Output is correct |
23 |
Correct |
23 ms |
51592 KB |
Output is correct |
24 |
Correct |
24 ms |
51576 KB |
Output is correct |
25 |
Correct |
26 ms |
51572 KB |
Output is correct |
26 |
Correct |
23 ms |
51504 KB |
Output is correct |
27 |
Correct |
24 ms |
51552 KB |
Output is correct |
28 |
Correct |
22 ms |
51660 KB |
Output is correct |
29 |
Correct |
23 ms |
51832 KB |
Output is correct |
30 |
Correct |
23 ms |
51624 KB |
Output is correct |
31 |
Correct |
27 ms |
51604 KB |
Output is correct |
32 |
Correct |
22 ms |
51588 KB |
Output is correct |
33 |
Correct |
26 ms |
52152 KB |
Output is correct |
34 |
Correct |
30 ms |
52108 KB |
Output is correct |
35 |
Correct |
34 ms |
52428 KB |
Output is correct |
36 |
Correct |
24 ms |
51660 KB |
Output is correct |
37 |
Correct |
36 ms |
52616 KB |
Output is correct |
38 |
Correct |
41 ms |
52776 KB |
Output is correct |
39 |
Correct |
40 ms |
52760 KB |
Output is correct |
40 |
Correct |
38 ms |
52736 KB |
Output is correct |
41 |
Correct |
39 ms |
52684 KB |
Output is correct |
42 |
Correct |
32 ms |
51732 KB |
Output is correct |
43 |
Correct |
32 ms |
51736 KB |
Output is correct |
44 |
Correct |
39 ms |
51816 KB |
Output is correct |
45 |
Incorrect |
56 ms |
54212 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |