UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#199730#484. intervalAnonyme1001561ms11224kbC++112.7kb2023-12-19 11:22:352023-12-19 12:50:47

answer

#include <bits/stdc++.h>
using namespace std;

#define QwQ330AwA return 0

const int N = 1e5 + 5;
int n;
int w[N];
int smx[N], smn[N], tmx, tmn;
struct Tree {
	int mn;
	int s;
	int tag;
} t[N << 2];

#define ls (p << 1)
#define rs ((p << 1)| 1)

void push(int p, int w) {
	t[p].mn += w;
	t[p].tag += w;
}

void pushup(int p) {
	t[p].mn = min(t[ls].mn, t[rs].mn);
	t[p].s = 0;
	if (t[p].mn == t[ls].mn) t[p].s += t[ls].s;
	if (t[p].mn == t[rs].mn) t[p].s += t[rs].s;
}

void pushdown(int p) {
	push(ls, t[p].tag);
	push(rs, t[p].tag);
	t[p].tag = 0;
}

void build(int p, int l, int r) {
	t[p].s = r - l + 1;
	if (l == r) return ;
	int mid = (l + r) / 2;
	build(ls, l, mid);
	build(rs, mid + 1, r);
	pushup(p);
}

void update(int p, int l, int r, int L, int R, int w) {
	if (L <= l && r <= R) {
		push(p, w);
		return ;
	}
	pushdown(p);
	int mid = (l + r) / 2;
	if (L <= mid) update(ls, l, mid, L, R, w);
	if (mid < R) update(rs, mid + 1, r, L, R, w);
	pushup(p);
}

int query(int p, int l, int r, int L, int R) {
	if (l == r) {
//		cout << l << endl;
		if (t[p].mn == -1) return l;
		else return -330;
	}
	pushdown(p);
	int mid = (l + r) / 2;
	if (L <= l && r <= R) {
		if (t[rs].mn == -1) return query(rs, mid + 1, r, L, R);
		else if (t[ls].mn == -1) return query(ls, l, mid, L, R);
		else return -330;
	}
	if (mid < R) {
		int ans = query(rs, mid + 1, r, L, R);
		if (ans != -330) return ans;
	}
	if (L <= mid) {
		int ans = query(ls, l, mid, L, R);
		if (ans != -330) return ans;		
	}
	return -330;
}

struct Node {
	int l, id;
	bool operator < (const Node rhs) const {
		return l < rhs.l;
	}
};

priority_queue <Node> q;
pair <int, int> ans[N];
int m;
vector <Node> ask[N];

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> w[i];
	cin >> m;
	for (int i = 1, l, r; i <= m; i++) {
		cin >> l >> r;
		ask[r].push_back({l, i});
	}
	build(1, 1, n);
	for (int i = 1; i <= n; i++) {
		while (tmx && w[smx[tmx]] < w[i]) update(1, 1, n, smx[tmx - 1] + 1, smx[tmx], -w[smx[tmx]]), tmx--;
		while (tmn && w[smn[tmn]] > w[i]) update(1, 1, n, smn[tmn - 1] + 1, smn[tmn], w[smn[tmn]]), tmn--;
		update(1, 1, n, 1, i, -1);
		smx[++tmx] = i;
		smn[++tmn] = i;
		update(1, 1, n, smx[tmx - 1] + 1, i, w[i]);
		update(1, 1, n, smn[tmn - 1] + 1, i, -w[i]);			
		for (auto qwq : ask[i]) q.push(qwq);
		while (!q.empty()) {
			int lim = q.top().l;
//			cout << lim << endl;
			int now = query(1, 1, n, 1, lim);
			if (now == -330) break;
			ans[q.top().id] = {now, i};
			q.pop();
		}
	}
	for (int i = 1; i <= m; i++) cout << ans[i].first << ' ' << ans[i].second << '\n';
	QwQ330AwA;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 4ms
memory: 4484kb

input:

1000
897 899 896 900 898 879 877 878 880 876 887 889 886 890 888 885 884 883 882 881 892 894 891 893...

output:

1 1000
126 1000
126 1000
126 1000
1 1000
126 1000
126 1000
1 1000
401 1000
126 1000
126 1000
126 100...

result:

ok 1000 lines

Test #2:

score: 10
Accepted
time: 2ms
memory: 4476kb

input:

1000
985 986 987 984 982 983 989 990 988 996 994 995 991 993 992 999 998 997 974 975 973 978 976 977...

output:

353 757
1 757
272 1000
29 757
353 757
353 757
29 1000
272 1000
272 757
29 757
272 1000
29 757
272 10...

result:

ok 1000 lines

Test #3:

score: 10
Accepted
time: 0ms
memory: 4476kb

input:

1000
196 197 199 200 198 178 179 177 176 180 181 185 184 183 182 188 186 189 187 190 193 195 194 191...

output:

1 875
1 625
1 875
1 875
251 875
1 625
1 625
376 600
376 875
251 875
251 875
1 625
376 875
1 625
251 ...

result:

ok 1000 lines

Test #4:

score: 10
Accepted
time: 208ms
memory: 11224kb

input:

100000
1 3 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 23 20 25 22 27 24 29 26 31 28 33 30 35 32 ...

output:

1 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2...

result:

ok 99999 lines

Test #5:

score: 10
Accepted
time: 221ms
memory: 11088kb

input:

100000
1 3 5 2 7 4 9 6 11 8 13 10 15 12 442 444 443 446 445 447 439 441 440 422 421 423 429 427 428 ...

output:

1 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2...

result:

ok 99999 lines

Test #6:

score: 10
Accepted
time: 223ms
memory: 11032kb

input:

100000
1 3 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 155 156 154 151 153 152 149 148 150 135 13...

output:

1 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2...

result:

ok 99999 lines

Test #7:

score: 10
Accepted
time: 210ms
memory: 11000kb

input:

100000
98727 98726 98728 98729 98730 98749 98747 98746 98748 98750 98736 98737 98739 98738 98740 987...

output:

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

result:

ok 99999 lines

Test #8:

score: 10
Accepted
time: 247ms
memory: 10140kb

input:

100000
44888 44889 44887 44891 44890 44892 44885 44884 44886 44899 44900 44901 44895 44893 44894 448...

output:

1 100000
1 100000
1 100000
1 100000
1 100000
32806 59049
1 100000
1 100000
1 100000
1 100000
1 10000...

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 221ms
memory: 11132kb

input:

100000
1 3 5 2 7 4 9 6 11 8 12 14 16 13 18 15 20 17 22 19 24 21 26 23 28 25 30 27 32 29 34 31 33 36 ...

output:

2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2...

result:

ok 100000 lines

Test #10:

score: 10
Accepted
time: 225ms
memory: 11104kb

input:

100000
1 3 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 23 20 25 22 27 24 29 26 31 28 33 30 35 32 ...

output:

2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2 100000
2...

result:

ok 100000 lines