QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#64739#4619. Fast Bubble Sortqinjianbin#AC ✓510ms16560kbC++171.5kb2022-11-25 14:29:172022-11-25 14:29:19

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-25 14:29:19]
  • 评测
  • 测评结果:AC
  • 用时:510ms
  • 内存:16560kb
  • [2022-11-25 14:29:17]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10, LOGN = 30, INF = 0x3f3f3f3f;
#define pii pair<int, int>
int T;
int n, q;
int lm[MAXN][LOGN];
int a[MAXN], inva[MAXN];
int dp[MAXN];
stack<int> stk;

int Get(int u, int lim) {
	int len = 20;
	for (int i = 20; i >= 0; i--) 
		if (lm[u][i] < lim) u = lm[u][i];
	u = lm[u][0];
	return u;
}

int main() {
	scanf("%d", &T);

	while (T--) {
		scanf("%d%d", &n, &q);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			inva[a[i]] = i;
		}
		stk.push(INF);
		for (int i = 1; i <= n; i++) {
			while (stk.top() < a[i]) {
				int tmp = stk.top(); stk.pop();
				lm[inva[tmp]][0] = i;
			}
			stk.push(a[i]);
		}
		while (stk.size() != 1) {
			int tmp = stk.top(); stk.pop();
			lm[inva[tmp]][0] = n + 1;
		}
		stk.pop();


		lm[n + 1][0] = n + 1;
		// for (int i = 1; i <= n; i++) 
		// 	printf("%d ", lm[i][0]);
		// puts("");
		for (int i = 1; i <= 20; i++) {
			for (int j = 1; j <= n + 1; j++) 
				lm[j][i] = lm[lm[j][i - 1]][i - 1];
		}
		dp[n + 1] = 0;
		for (int i = n; i >= 1; i--) {
			if (lm[i][0] - i != 1) 
				dp[i] = dp[lm[i][0]] + 1;
			else dp[i] = dp[lm[i][0]];
			// printf("%d %d\n", dp[i], lm[i][0]);
		}
		// for (int i = 1; i <= n; i++) 
		// 	printf("%d ", dp[i]);
		// puts("");
		int l, r;
		for (int i = 1; i <= q; i++) {
			scanf("%d%d", &l, &r);
			if (l == r) printf("%d\n", 0);
			else {
				r = Get(l, r);
				// printf("qwe%d %d\n", l, r);
				printf("%d\n", dp[l] - dp[r]);
			}
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 510ms
memory: 16560kb

input:

10
100000 100000
94437 49936 62522 1166 33452 90618 91436 81173 99242 37280 46125 44869 52811 45694 11550 29857 48941 56538 35057 40907 79925 70785 29658 19044 32804 11428 4127 92197 12680 61636 27200 56790 83389 80340 54411 15308 38952 78085 81397 37554 44186 33581 35662 73331 84991 32044 69012 822...

output:

4
7
10
16
15
8
2
9
7
8
6
13
11
13
9
9
9
5
5
8
10
11
10
17
14
6
7
5
17
8
13
17
6
12
8
13
13
12
12
10
6
8
19
10
10
19
17
8
12
5
11
20
12
7
17
5
9
12
10
8
15
6
9
9
10
6
10
9
7
16
8
7
12
11
15
16
7
6
10
11
10
7
6
11
11
11
10
9
8
11
5
15
5
6
11
3
11
13
8
13
7
15
14
6
13
10
11
10
8
10
12
3
6
6
16
11
9
9
1...

result:

ok 1000000 lines