#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
ll int cnt = 0;
struct DSU {
vector<ll int> p, s;
void init(ll int n) {
p = vector<ll int>(n);
s = vector<ll int>(n, 1);
iota(all(p), 0);
}
ll int get(ll int x) {
return p[x] = (p[x] == x ? x : get(p[x]));
}
void unite(ll int a, ll int b) {
if (a == b) {
cnt++;
return;
}
a = get(a);
b = get(b);
if (a == b) {
return;
}
if (s[a] > s[b]) swap(a, b);
p[a] = b;
cnt += s[b] * s[a];
s[b] += s[a];
}
};
void solve() {
ll int n; cin >> n;
ll int q; cin >> q;
vector<ll int> arr(n); for (ll int i = 0; i < n; i++) cin >> arr[i];
vector<pair<ll int, pair<ll int, ll int>>> edges;
for (ll int i = 1; i < n; i++) {
edges.emplace_back(max(arr[i], arr[i - 1]), make_pair(i, i - 1));
}
for (ll int i = 0; i < n; i++) {
edges.emplace_back(arr[i], make_pair(i, i));
}
sort(all(edges));
vector<pair<ll int, ll int>> qq;
for (ll int i = 0; i < q; i++) {
ll int a; cin >> a;
qq.emplace_back(a, i);
}
// cout << "Hello" << endl;
sort(all(qq));
vector<ll int> ans(q);
ll int ptr = 0;
DSU dsu;
dsu.init(n);
for (ll int i = 0; i < q; i++) {
ll int hi = qq[i].first;
// cout << pl(hi) << endl;
while (ptr < ll int(edges.size()) && edges[ptr].first <= hi) {
dsu.unite(edges[ptr].second.first, edges[ptr].second.second);
// cout << pl(edges[ptr].first) << pl(edges[ptr].second.first) << pl(edges[ptr].second.second) << pl(cnt) << endl;
ptr++;
}
ans[qq[i].second] = cnt;
}
for (ll int a : ans) {
cout << a << endl;
}
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
ll int T = 1;
// cin >> T;
while (T--)
solve();
}
Compilation message
pilot.cpp: In function 'void solve()':
pilot.cpp:7:12: error: expected primary-expression before 'long'
7 | #define ll long long
| ^~~~
pilot.cpp:81:16: note: in expansion of macro 'll'
81 | while (ptr < ll int(edges.size()) && edges[ptr].first <= hi) {
| ^~
pilot.cpp:81:15: error: expected ')' before 'long'
81 | while (ptr < ll int(edges.size()) && edges[ptr].first <= hi) {
| ~ ^
| )
pilot.cpp:7:12: error: expected primary-expression before 'long'
7 | #define ll long long
| ^~~~
pilot.cpp:81:16: note: in expansion of macro 'll'
81 | while (ptr < ll int(edges.size()) && edges[ptr].first <= hi) {
| ^~
pilot.cpp:78:10: warning: unused variable 'hi' [-Wunused-variable]
78 | ll int hi = qq[i].first;
| ^~