QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#61094#3676. Joined Vesselsabdelrahman001#WA 3ms7504kbC++201.2kb2022-11-09 21:00:132022-11-09 21:00:16

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-09 21:00:16]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:7504kb
  • [2022-11-09 21:00:13]
  • 提交

answer

#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
const int N = 2e5 + 5;
int n, q, a[N];
int prv[N], nxt[N];
ll ans[N], ansR[N];
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 1;i < n;i++)
		cin >> a[i];
    stack<int> st;
    for(int i = 1;i < n;i++) {
		while(!st.empty() && a[st.top()] < a[i])
			st.pop();
		if(st.empty())
			prv[i] = 0;
		else
			prv[i] = st.top();
		st.push(i);
	}
	st = {};
	for(int i = n - 1;i >= 1;i--) {
		while(!st.empty() && a[st.top()] < a[i])
			st.pop();
		if(st.empty())
			nxt[i] = n;
		else
			nxt[i] = st.top();
		st.push(i);
	}
	for(int i = 1;i < n;i++)
		ans[i] = ans[prv[i]] + (i - prv[i]) * 1ll * a[i];
	for(int i = n - 1;i >= 1;i--)
		ansR[i] = ansR[nxt[i]] + (nxt[i] - i) * 1ll * a[i];
	cin >> q;
	while(q--) {
		int l, r;
		cin >> l >> r;
		if(l <= r)
			cout << ans[r - 1] - ans[prv[l]] << '\n';
		else
			cout << ansR[r] - ansR[nxt[l] - 1] << '\n';
	}
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7504kb

input:

6
1 4 2 3 5
4
1 6
6 1
2 5
5 2

output:

25
18
14
12

result:

ok 4 number(s): "25 18 14 12"

Test #2:

score: 0
Accepted
time: 2ms
memory: 5424kb

input:

2
1
2
2 1
1 2

output:

1
1

result:

ok 2 number(s): "1 1"

Test #3:

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

input:

2
17
2
1 2
2 1

output:

17
17

result:

ok 2 number(s): "17 17"

Test #4:

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

input:

2
639986533
2
1 2
2 1

output:

639986533
639986533

result:

ok 2 number(s): "639986533 639986533"

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 5544kb

input:

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

output:

139
60
247
325
281
20
247
162
71
161
235
67
135
204
188
90
12
110
65
24
263
340
54
98
85
159
166
121
39
9
100
56
192
247
340
179
192
15
312
195
19
248
84
114
159
117
31
235
85
116
161
202
153
183
48
264
196
229
60
321
148
50
167
236
255
298
353
137
81
14
27
202
136
132
57
102
44
110
57
274
2
160
1
1...

result:

wrong answer 1st numbers differ - expected: '195', found: '139'