QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190339#3676. Joined VesselsIsaacMoris#WA 1ms5716kbC++172.6kb2023-09-28 18:28:372024-01-17 03:30:13

Judging History

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

  • [2024-01-17 03:30:13]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5716kb
  • [2023-09-28 18:28:37]
  • 提交

answer

#include<iostream>
#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 2e5 + 5, M = 18, mod = 998244353;
int n, a[N];
pair<ll, int> dp1[N][M], dp2[N][M];

void preprocess() {
    stack<int> st;
    for (int i = n - 1; i >= 1; i--) {
        while (!st.empty() && a[st.top()] < a[i])st.pop();
        int nxt = st.empty() ? n : st.top();
        st.push(i);
        dp1[i][0] = {1ll * (nxt - i) * a[i], nxt};
        for (int j = 1; j < M; j++) {
            int nxt = dp1[i][j - 1].second;
            if (nxt) {
                dp1[i][j] = make_pair(dp1[i][j - 1].first + dp1[nxt][j - 1].first, dp1[nxt][j - 1].second);
            }
        }
    }

    while (!st.empty())st.pop();

    for (int i = 1; i < n; i++) {
        while (!st.empty() && a[st.top()] < a[i])st.pop();
        int nxt = st.empty() ? 0 : st.top();
        st.push(i);
        dp2[i][0] = {1ll * (i - nxt) * a[i], nxt};
        for (int j = 1; j < M; j++) {
            int nxt = dp2[i][j - 1].second;
            if (nxt) {
                dp2[i][j] = make_pair(dp2[i][j - 1].first + dp2[nxt][j - 1].first, dp2[nxt][j - 1].second);
            }
        }
    }
}

ll solve1(int l, int r) {
    ll ans = 0;
    for (int j = M - 1; j >= 0; j--) {
        if (dp1[l][j].second && dp1[l][j].second <= r) {
            ans += dp1[l][j].first;
            l = dp1[l][j].second;
        }
    }
    return ans + 1ll * (r - l + 1) * a[l];
}

ll solve2(int l, int r) {
    ll ans = 0;
    for (int j = M - 1; j >= 0; j--) {
        if (dp2[r][j].second && dp2[r][j].second >= l) {
            ans += dp2[r][j].first;
            r = dp2[r][j].second;
        }
    }
    return ans + 1ll * (r - l + 1) * a[r];
}

void doWork() {
    cin >> n;
    a[0] = a[n] = 2e9;
    for (int i = 1; i < n; i++) {
        cin >> a[i];
    }

    preprocess();

    int q;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        if (l > r) {
            swap(l, r);
            r--;
            int nxt = dp1[r][0].second;
            if (nxt == 0)nxt = n;
            r = nxt - 1;
            cout << solve1(l, r) << "\n";
        } else {
            int nxt = dp2[l][0].second;
            l = nxt + 1;
            cout << solve2(l, r - 1) << "\n";
        }
    }
}

int main() {
    IO
    preprocess();
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        //  cout << "Case #" << i << ": ";
        doWork();
    }
}

詳細信息

Test #1:

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

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: 1ms
memory: 5620kb

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: 5612kb

input:

2
17
2
1 2
2 1

output:

17
17

result:

ok 2 number(s): "17 17"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5612kb

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: 0ms
memory: 5716kb

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:

119
60
232
180
275
20
232
147
71
146
235
57
133
204
184
90
12
152
65
24
278
328
54
88
83
157
164
72
39
9
90
56
169
247
340
159
129
15
312
195
28
242
80
114
157
100
31
235
47
116
146
202
147
160
48
264
176
229
137
309
138
39
167
221
255
280
208
133
81
14
27
202
80
122
57
96
52
95
51
270
2
160
1
229
1...

result:

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