Submission #928282

# Submission time Handle Problem Language Result Execution time Memory
928282 2024-02-16T07:14:46 Z pcc Railway Trip 2 (JOI22_ho_t4) C++17
27 / 100
2000 ms 22596 KB
#include <bits/stdc++.h>
using namespace std;


#define pii pair<int,int>
#define fs first
#define sc second
const int mxn = 1e5+10;

int N,M,K,Q;
pii range[mxn],segtree[mxn*4];
vector<int> lop[mxn],rop[mxn];

void build(int now,int l,int r){
	if(l == r){
		segtree[now] = range[l];
		return;
	}
	int mid = (l+r)>>1;
	build(now*2+1,l,mid);
	build(now*2+2,mid+1,r);
	segtree[now].fs = min(segtree[now*2+1].fs,segtree[now*2+2].fs);
	segtree[now].sc = max(segtree[now*2+1].sc,segtree[now*2+2].sc);
}
pii getval(int now,int l,int r,int s,int e){
	if(l>=s&&e>=r)return segtree[now];
	int mid = (l+r)>>1;
	if(mid>=e)return getval(now*2+1,l,mid,s,e);
	else if(mid<s)return getval(now*2+2,mid+1,r,s,e);
	else{
		auto ta = getval(now*2+1,l,mid,s,e),tb = getval(now*2+2,mid+1,r,s,e);
		return make_pair(min(ta.fs,tb.fs),max(ta.sc,tb.sc));
	}
}

void solve(){
	int s,e;
	cin>>s>>e;
	pii now = {s,s};
	int ans = 0;
	while(now.fs>e||now.sc<e){
		ans++;
		auto nxt = getval(0,1,N,now.fs,now.sc);
		if(nxt.fs == now.fs&&nxt.sc == now.sc)break;
		now = nxt;
	}
	cout<<(now.fs>e||now.sc<e?-1:ans)<<'\n';
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>K>>M;
	for(int i = 0;i<M;i++){
		int a,b;
		cin>>a>>b;
		if(a<=b){
			int e = min(b+1,a+K);
			rop[a].push_back(b);
			rop[e].push_back(-b);
		}
		else{
			int e = max(b-1,a-K);
			lop[a].push_back(b);
			lop[e].push_back(-b);
		}
	}
	for(int i = 1;i<=N;i++)range[i].fs = range[i].sc = i;
	multiset<int> st;
	for(int i = 1;i<=N;i++){
		for(auto &j:rop[i]){
			if(j>0)st.insert(j);
			else st.erase(st.find(-j));
		}
		if(!st.empty())range[i].sc = *st.rbegin();
	}

	st.clear();
	for(int i = N;i>=1;i--){
		for(auto &j:lop[i]){
			if(j>0)st.insert(j);
			else st.erase(st.find(-j));
		}
		if(!st.empty())range[i].fs = *st.begin();
	}
	build(0,1,N);
	cin>>Q;
	while(Q--)solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8796 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8868 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8796 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8868 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 165 ms 8792 KB Output is correct
14 Correct 88 ms 8912 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 37 ms 8796 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 4 ms 8792 KB Output is correct
20 Correct 3 ms 8792 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 4 ms 8796 KB Output is correct
24 Correct 4 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 12916 KB Output is correct
2 Correct 32 ms 12892 KB Output is correct
3 Correct 33 ms 13148 KB Output is correct
4 Correct 22 ms 12636 KB Output is correct
5 Correct 99 ms 17944 KB Output is correct
6 Correct 54 ms 17492 KB Output is correct
7 Correct 75 ms 22596 KB Output is correct
8 Correct 62 ms 15204 KB Output is correct
9 Correct 46 ms 15196 KB Output is correct
10 Correct 79 ms 16944 KB Output is correct
11 Correct 69 ms 16764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2053 ms 16504 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 141 ms 18536 KB Output is correct
2 Execution timed out 2068 ms 13572 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8796 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8792 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8868 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 165 ms 8792 KB Output is correct
14 Correct 88 ms 8912 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 37 ms 8796 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 4 ms 8792 KB Output is correct
20 Correct 3 ms 8792 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 4 ms 8796 KB Output is correct
24 Correct 4 ms 8796 KB Output is correct
25 Correct 29 ms 12916 KB Output is correct
26 Correct 32 ms 12892 KB Output is correct
27 Correct 33 ms 13148 KB Output is correct
28 Correct 22 ms 12636 KB Output is correct
29 Correct 99 ms 17944 KB Output is correct
30 Correct 54 ms 17492 KB Output is correct
31 Correct 75 ms 22596 KB Output is correct
32 Correct 62 ms 15204 KB Output is correct
33 Correct 46 ms 15196 KB Output is correct
34 Correct 79 ms 16944 KB Output is correct
35 Correct 69 ms 16764 KB Output is correct
36 Execution timed out 2053 ms 16504 KB Time limit exceeded
37 Halted 0 ms 0 KB -