QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625297#9429. Subarraynhat2004RE 0ms0kbC++141.3kb2024-10-09 18:26:522024-10-09 18:26:52

Judging History

This is the latest submission verdict.

  • [2024-10-09 18:26:52]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 0kb
  • [2024-10-09 18:26:52]
  • Submitted

answer

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

const int MOD = 998244353;

long long count_good_subarrays(const vector<int>& arr, int n) {
    long long result = 0;
    vector<long long> counts(n + 1, 0);


    stack<int> s;
    vector<int> last_pos(n, -1);


    for (int i = 0; i < n; ++i) {

        int count = 0;
        while (!s.empty() && arr[s.top()] <= arr[i]) {
            int idx = s.top();
            s.pop();
            count = i - last_pos[arr[idx]];
            counts[count] += (i - idx) * (idx - last_pos[arr[idx]]);
            last_pos[arr[idx]] = idx;
        }
        s.push(i);
    }

    while (!s.empty()) {
        int idx = s.top();
        s.pop();
        counts[n] += (n - idx) * (idx - last_pos[arr[idx]]);
        last_pos[arr[idx]] = idx;
    }


    for (int k = 1; k <= n; ++k) {
        result += (k * counts[k] * counts[k]) % MOD;
        result %= MOD;
    }

    return result;
}

void solve() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        vector<int> arr(n);
        for (int i = 0; i < n; ++i) {
            cin >> arr[i];
        }
        cout << count_good_subarrays(arr, n) << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

3
11
1 1 2 1 2 2 3 3 2 3 1
3
2024 5 26
3
1000000000 1000000000 1000000000

output:

2065
14

result: