이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e6 + 5;
int n, q;
pair<int, int> y[maxn];
pair<int, int> h[maxn];
int par[maxn], sz[maxn];
long long total[maxn];
bool check[maxn];
long long ans[maxn], res[maxn];
struct dsu {
int n;
dsu() {}
dsu(int n) : n(n) {
for(int i = 1; i <= n; ++i) {
sz[i] = 1;
par[i] = i;
total[i] = 1;
}
}
int find(int u) {
return u == par[u] ? u : par[u] = find(par[u]);
}
void join(int u, int v) {
u = find(u), v = find(v);
if(u == v) return;
par[v] = u;
total[u] += total[v];
total[u] += 1ll * sz[u] * sz[v];
sz[u] += sz[v];
}
};
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> h[i].first;
h[i].second = i;
}
for(int i = 1; i <= q; ++i) {
cin >> y[i].first;
y[i].second = i;
}
sort(h + 1, h + n + 1);
sort(y + 1, y + q + 1);
dsu d(n);
int j = 0;
long long sum = 0;
for(int i = 1; i <= n; ++i) {
int cur = 0;
while(i + cur <= n and h[i].first == h[i + cur].first) {
if(check[h[i + cur].second - 1]) {
sum -= total[d.find(h[i + cur].second - 1)];
d.join(h[i + cur].second, h[i + cur].second - 1);
}
if(check[h[i + cur].second + 1]) {
sum -= total[d.find(h[i + cur].second + 1)];
d.join(h[i + cur].second, h[i + cur].second + 1);
}
sum += total[d.find(h[i + cur].second)];
check[h[i + cur].second] = 1;
++cur;
}
while(j <= q and y[j].first < h[i].first) ++j;
ans[j] = sum;
i = i + cur - 1;
}
for(int i = 1; i <= q; ++i) {
ans[i] = max(ans[i], ans[i - 1]);
res[y[i].second] = ans[i];
}
for(int i = 1; i <= q; ++i) {
cout << res[i] << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# | 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... |