QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625300 | #9429. Subarray | nhat2004 | RE | 0ms | 0kb | C++20 | 1.3kb | 2024-10-09 18:27:32 | 2024-10-09 18:27:33 |
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