# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1123786 | tuannm | Pilot (NOI19_pilot) | C++20 | 507 ms | 70412 KiB |
#include<bits/stdc++.h>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 1e6 + 1;
const string NAME = "";
int n, q, a[N], L[N], R[N];
long long ans[N];
vector<int> pos[N];
void inp(){
cin >> n >> q;
for(int i = 1; i <= n; ++i)
cin >> a[i];
}
void solve(){
stack<int> stk;
for(int i = 1; i <= n; ++i){
while(!stk.empty() && a[stk.top()] <= a[i])
stk.pop();
L[i] = (stk.empty() ? 1 : stk.top() + 1);
stk.push(i);
pos[a[i]].pb(i);
}
while(!stk.empty())
stk.pop();
for(int i = n; i > 0; --i){
while(!stk.empty() && a[stk.top()] < a[i])
stk.pop();
R[i] = (stk.empty() ? n : stk.top() - 1);
stk.push(i);
}
for(int i = 1; i < N; ++i){
ans[i] = ans[i - 1];
for(int j : pos[i])
ans[i] += 1LL * (j - L[j] + 1) * (R[j] - j + 1);
}
while(q--){
int y;
cin >> y;
cout << ans[y] << "\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
inp();
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |