#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define ppf pop_front
#define pf push_front
#define bk back()
#define frnt front()
#define ins insert
#define er erase
#define sc second
#define fr first
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define REP(i,n) for (int i = 0; i < n; ++i)
#define REP1(i,n) for (int i = 1; i <= n; ++i)
#define REPV(i,n) for (int i = n-1; i >= 0; --i)
#define REPV1(i, n) for (int i = n; i > 0; --i)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define MNTO(x,y) x = min(x, (__typeof__(x))y)
#define MXTO(x,y) x = max(x, (__typeof__(x))y)
struct DSU {
vi parent, sz;
DSU(int n) : parent(n), sz(n, 1) {
iota(ALL(parent), 0);
}
int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
int unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return a;
if (sz[a] > sz[b]) swap(a, b);
parent[a] = b;
sz[b] += sz[a];
return b;
}
ll size(int x) {
return sz[find(x)];
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
vi h(n);
REP(i, n) cin >> h[i];
vector<pii> queries(q);
REP(i, q) {
cin >> queries[i].fr;
queries[i].sc = i;
}
vector<pii> mountains(n);
REP(i, n) mountains[i] = {h[i], i};
SORT(mountains);
vector<pii> qs = queries;
SORT(qs);
vll ans(q);
DSU dsu(n);
vector<bool> active(n, 0);
ll cur = 0;
int mi = 0;
for (auto &q : qs) {
int y = q.fr, qi = q.sc;
while (mi < n && mountains[mi].fr <= y) {
int idx = mountains[mi].sc;
active[idx] = 1;
ll comp_sz = 1;
cur ++;
if (idx > 0 && active[idx-1]) {
ll s1 = dsu.size(idx-1);
ll s2 = dsu.size(idx);
cur -= s1*(s1+1)/2;
cur -= s2*(s2+1)/2;
int nb = dsu.unite(idx-1, idx);
ll ns = dsu.size(nb);
cur += ns*(ns+1)/2;
}
if (idx+1 < n && active[idx+1]) {
ll s1 = dsu.size(idx);
ll s2 = dsu.size(idx+1);
cur -= s1*(s1+1)/2;
cur -= s2*(s2+1)/2;
int nb = dsu.unite(idx, idx+1);
ll ns = dsu.size(nb);
cur += ns*(ns+1)/2;
}
mi++;
}
ans[qi] = cur;
}
REP(i, q) cout << ans[i] << '\n';
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... |