QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#643719#365. Railway TripQOJ114514ancestor0 2ms13928kbC++142.7kb2024-10-15 23:21:122024-10-15 23:21:12

Judging History

你现在查看的是最新测评结果

  • [2024-10-15 23:21:12]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:13928kb
  • [2024-10-15 23:21:12]
  • 提交

answer

#include<bits/stdc++.h>
#define N 100005
#define S 20 
using namespace std;
int n,k,q,a[N],nxt[N][S],lst[N][S],mxlpos[S][N],mxrpos[S][N],tp,stk[N],lg2[N];
int mxl(int u,int v){
	if(u>v)swap(u,v); 
	if(a[u]<a[v])return v; 
	return u;
}
int mxr(int u,int v){
	if(u>v)swap(u,v);
	if(a[u]<=a[v])return v; 
	return u;
}
int qryl(int l,int r){
	int kk=lg2[r-l+1];
	return mxl(mxlpos[kk][l],mxlpos[kk][r-(1<<kk)+1]);
}
int qryr(int l,int r){
	int kk=lg2[r-l+1];
	return mxr(mxrpos[kk][l],mxrpos[kk][r-(1<<kk)+1]);
}
int qlp(int l,int pos){
	int ans=0,r=l;
	if(l>pos){
		ans=114514;
	}
	if(l<pos){
		++ans;
		for(int i=S-1;i>=0;i--)
			if(max(nxt[l][i],nxt[r][i])<pos){
				int tmp=max(nxt[l][i],nxt[r][i]);
				l=min(lst[l][i],lst[r][i]),r=tmp,ans+=(1<<i);
			}
	}
	return ans;
}
int qpr(int pos,int r){
	int ans=0,l=r;
	if(r<pos){
		ans=114514;
	}
	if(r>pos){
		++ans;
		for(int i=S-1;i>=0;i--)
			if(min(lst[l][i],lst[r][i])>pos){
				int tmp=min(lst[l][i],lst[r][i]);
				r=max(nxt[l][i],nxt[r][i]),l=tmp,ans+=(1<<i);
			}
	}
	return ans;
}
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>k>>q;
	for(int i=1;i<=n;i++)cin>>a[i],mxlpos[0][i]=mxrpos[0][i]=i;
	for(int i=2;i<=n;i++)lg2[i]=lg2[i>>1]+1;
	for(int i=1;i<=n;i++){
		while(tp&&a[stk[tp]]<=a[i])nxt[stk[tp--]][0]=i;
		if(i<n)stk[++tp]=i;
	}
	nxt[n][0]=n;
	for(int i=n;i>=1;i--){
		while(tp&&a[stk[tp]]<=a[i])lst[stk[tp--]][0]=i;
		if(i>1)stk[++tp]=i;
	}
	lst[1][0]=1;
//	for(int i=1;i<=n;i++)cout<<nxt[i][0]<<' ';cout<<'\n';
//	for(int i=1;i<=n;i++)cout<<lst[i][0]<<' ';cout<<'\n';
	for(int i=1;(1<<i)<=n;i++)
		for(int j=1;j+(1<<i)-1<=n;j++)
			mxlpos[i][j]=mxl(mxlpos[i-1][j],mxlpos[i-1][j+(1<<(i-1))]);
	for(int i=1;(1<<i)<=n;i++)
		for(int j=1;j+(1<<i)-1<=n;j++)
			mxrpos[i][j]=mxr(mxrpos[i-1][j],mxrpos[i-1][j+(1<<(i-1))]);
	for(int i=1;i<S;i++)
		for(int j=1;j<=n;j++)
			nxt[j][i]=max(nxt[nxt[j][i-1]][i-1],nxt[lst[j][i-1]][i-1]),
			lst[j][i]=min(lst[lst[j][i-1]][i-1],lst[nxt[j][i-1]][i-1]);
	for(int i=1,l,r;i<=q;i++){
		cin>>l>>r;
		if(l>r)swap(l,r);
		int pos=qryl(l,r),ans=qlp(l,pos)+qpr(pos,r)-1;
		int lpos=lst[qryl(l,r)][0];
		if(lpos<l)
		ans=min(ans,qpr(lpos,l)+qpr(lpos,r)-1);
		int rpos=nxt[qryr(l,r)][0];
		if(rpos>r)
		ans=min(ans,qlp(l,rpos)+qlp(r,rpos)-1);
		if(i==4&&a[2]==85){
			cout<<":"<<qlp(l,rpos)<<' '<<qlp(r,rpos)<<'\n';
			cout<<":"<<lpos<<' '<<l<<' '<<pos<<' '<<r<<' '<<rpos<<'\n';
			cout<<":"<<a[lpos]<<' '<<a[l]<<' '<<a[pos]<<' '<<a[r]<<' '<<a[rpos]<<'\n';
			cout<<":";
			int nw=a[l];
			for(int i=l;i<=rpos;i++)if(a[i]>=nw)nw=a[i],cout<<a[i]<<' ';
			nw=a[r];
			for(int i=r;i<=rpos;i++)if(a[i]>=nw)nw=a[i],cout<<a[i]<<' ';cout<<'\n';
		}
		cout<<ans<<'\n';
	}
	return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 0ms
memory: 13856kb

input:

100 100 50
100
86
39
28
49
22
79
14
83
100
15
26
37
51
53
18
74
15
96
72
47
80
10
46
62
88
20
36
46
29
40
28
37
88
91
41
24
63
14
92
24
31
99
61
62
96
94
51
51
21
72
97
59
96
97
94
66
88
32
96
58
26
53
1
100
31
85
30
42
69
40
62
54
94
49
62
13
20
82
74
20
44
54
69
65
34
78
64
48
69
19
35
8
92
100
87...

output:

3
1
3
5
3
3
3
0
2
5
2
6
1
3
1
3
5
5
3
5
4
7
3
6
3
4
4
4
7
0
2
3
2
4
5
5
6
5
5
6
4
3
2
4
5
2
5
4
2
2

result:

ok 50 lines

Test #2:

score: 0
Wrong Answer
time: 2ms
memory: 13928kb

input:

100 100 50
100
85
82
7
50
49
51
45
30
3
29
99
71
93
5
68
70
52
12
44
1
35
99
80
76
34
23
28
62
91
80
77
59
57
30
15
23
13
16
21
58
23
38
49
44
73
7
47
24
53
97
83
14
71
16
75
61
24
17
96
51
41
74
53
25
2
42
36
73
57
53
45
10
12
11
79
68
2
78
44
47
67
21
99
25
68
60
71
23
92
9
2
97
37
43
64
32
28
7
1...

output:

2
0
5
:2 2
:1 2 12 75 84
:100 85 99 11 99
:85 99 99 99 11 79 99 
3
2
4
2
4
3
5
4
3
3
6
4
4
3
3
3
4
2
3
3
3
4
5
4
2
3
2
5
3
3
4
2
2
3
5
3
6
2
5
4
2
2
4
4
3
4
7

result:

wrong answer 4th lines differ - expected: '4', found: ':2 2'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%