This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int>par((int)1e6+5);
vector<long long>sz((int)1e6+5);
int UF(int u) {
if(par[u] == u) return u;
return par[u] = UF(par[u]);
}
signed main() {
int n,q; cin>>n>>q;
vector<int>v(n);
for(int i = 0 ; i < n ; i++)
par[i] = i, sz[i] = 1;
vector<vector<int>>p((int)1e6+1);
int X = 0;
for(auto &z : v) {
cin>>z;
p[z].push_back(X);
X++;
}
auto f = [&] (long long X) -> long long {
return (X * (X+1))/2;
};
vector<long long>Q((int)1e6+5);
long long ans = 0;
for(int i = 1 ; i <= (int)1e6 ; i++) {
for(auto z : p[i]) {
if(z != 0 && UF(z-1) != UF(z) && v[z-1] <= i) {
int A = UF(z-1), B = UF(z);
ans -= f(sz[A]);
ans += f(sz[A] + sz[B]);
par[A] = B;
sz[B] += sz[A];
sz[A] = 0;
} else ans++;
if(z != n-1 && UF(z) != UF(z+1) && v[z+1] < i) {
int A = UF(z), B = UF(z+1);
ans -= f(sz[A]) + f(sz[B]);
ans += f(sz[A] + sz[B]);
par[A] = B;
sz[B] += sz[A];
sz[A] = 0;
}
}
Q[i] = ans;
}
while(q--) {
int h; cin>>h;
cout << Q[h] << '\n';
}
}
# | 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... |