QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#225513#7248. Ivan DornxaphoenixWA 7ms29824kbC++142.9kb2023-10-24 19:08:112023-10-24 19:08:11

Judging History

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

  • [2023-10-24 19:08:11]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:29824kb
  • [2023-10-24 19:08:11]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repn(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = (n) - 1; i >= a; i--)
#define pern(i, a, n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;

const int N = 510000;
const int M = 2100000;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;

mt19937_64 Rand((unsigned long long)new char);
#define rand Rand

int n, m, a[N];
struct query {
	int l, r, id;
}b[N], tmp[N];
int ans[N];
int ansl[N], ansr[N];
PII st[N];
vector<query> f[N];
map<int, int> idxr;
set<int> idxl;
void work(int l, int r, int bl, int br) {
	if (bl > br) return;
	int mid = (l + r) / 2;
	int tail = bl - 1, t1, t2, t3;
	repn(i, bl, br) if (b[i].l <= mid && b[i].r >= mid) tmp[++tail] = b[i];
	t1 = tail;
	repn(i, bl, br) if (b[i].r < mid) tmp[++tail] = b[i];
	t2 = tail;
	repn(i, bl, br) if (b[i].l > mid) tmp[++tail] = b[i];
	t3 = tail;
	repn(i, bl, br) b[i] = tmp[i];
	work(l, mid - 1, t1 + 1, t2);
	work(mid + 1, r, t2 + 1, t3);
	if (t1 < bl) return;
	repn(i, l - 1, r + 1) ansl[i] = ansr[i] = 0;
	// right
	st[tail = 1] = mp(inf + inf, 0);
	repn(i, mid, r) {
		ansr[i] = ansr[i - 1];
		while (a[i] > st[tail].fi) tail--;
		if (a[i] == st[tail].fi) ansr[i] = max(ansr[i], i - st[tail].se), tail--;
		st[++tail] = mp(a[i], i);
	}
	// left
	st[tail = 1] = mp(inf + inf, 0);
	pern(i, l, mid) {
		ansl[i] = ansl[i + 1];
		while (a[i] > st[tail].fi) tail--;
		if (a[i] == st[tail].fi) ansl[i] = max(ansl[i], st[tail].se - i), tail--;
		st[++tail] = mp(a[i], i);
	}
	repn(i, bl, t1) ans[b[i].id] = max(ansl[b[i].l], ansr[b[i].r]);
	// mid
	repn(i, l, r) f[i].clear();
	repn(i, bl, t1) f[b[i].r].pb(b[i]);
	idxr.clear(), idxl.clear();
	int cur = mid, mx = -inf;
	repn(i, mid, r) {
		if (a[i] >= mx) {
			mx = a[i];
			while (cur - 1 >= l && a[cur - 1] <= mx) {
				cur--;
				if (a[cur] == mx) idxl.insert(cur);
			}
			idxr[mx] = i;
		}
		for (auto p: f[i]) {
			auto it = idxl.lower_bound(p.l);
			if (it != idxl.end()) {
				int len = idxr[a[*it]] - (*it);
				ans[p.id] = max(ans[p.id], len);
			}
		}
	}
}
int main() {
	IO;
	cin >> n >> m;
	repn(i, 1, n) cin >> a[i];
	repn(i, 1, m) {
		cin >> b[i].l >> b[i].r;
		b[i].id = i;
	}
	work(1, n, 1, m);
	repn(i, 1, m) cout << ans[i] << "\n";
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 28440kb

input:

8 5
4 3 2 2 3 3 7 3
1 7
6 8
1 3
3 6
1 8

output:

4
0
0
1
4

result:

ok 5 number(s): "4 0 0 1 4"

Test #2:

score: 0
Accepted
time: 3ms
memory: 29336kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 3ms
memory: 27892kb

input:

2 1
1 2
1 2

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 7ms
memory: 29824kb

input:

2 1
1 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 3ms
memory: 28352kb

input:

10 10
6 4 10 10 10 3 4 5 4 3
4 10
3 8
3 5
1 10
2 3
4 6
1 2
2 6
9 10
5 7

output:

1
2
2
2
0
1
0
2
0
0

result:

ok 10 numbers

Test #6:

score: 0
Accepted
time: 0ms
memory: 29156kb

input:

10 10
4 10 1 4 1 1 4 1 4 10
8 10
1 7
5 9
5 9
3 7
9 10
4 10
5 10
2 3
2 8

output:

0
3
2
2
3
0
5
2
0
3

result:

ok 10 numbers

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 27912kb

input:

10 10
5 6 6 6 6 5 6 6 6 5
1 10
2 9
3 9
3 10
5 10
3 7
2 4
1 1
1 3
3 9

output:

7
7
6
6
2
4
1
0
1
6

result:

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