#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
const int N = 1e6 + 17;
int n, q, l[N], r[N];
bool ex[N];
long long res, ans[N];
struct qrs
{
int a, t, id;
bool operator < (const qrs &o) const
{
if (a != o.a)
{
return a < o.a;
}
return t < o.t;
}
} qr[2 * N];
inline int rootl (int u)
{
if (u == l[u])
{
return u;
}
return l[u] = rootl(l[u]);
}
inline void joinl (int u, int v)
{
u = rootl(u), v = rootl(v);
if (u == v)
{
return;
}
if (u > v)
{
swap (u, v);
}
l[v] = u;
}
inline int rootr (int u)
{
if (u == r[u])
{
return u;
}
return r[u] = rootr(r[u]);
}
inline void joinr (int u, int v)
{
u = rootr(u), v = rootr(v);
if (u == v)
{
return;
}
if (u < v)
{
swap (u, v);
}
r[v] = u;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1, x; i <= n; ++i)
{
cin >> x;
qr[i] = {x, 0, i};
l[i] = r[i] = i;
}
for (int i = 1, x; i <= q; ++i)
{
cin >> x;
qr[n + i] = {x, 1, i};
}
sort (qr + 1, qr + n + q + 1);
for (int i = 1; i <= n + q; ++i)
{
auto [a, t, id] = qr[i];
if (t)
{
ans[id] = res;
continue;
}
ex[id] = 1;
res += (id > 1 && ex[id - 1]) * (id - rootl (id - 1)) + (id < n && ex[id + 1]) * (rootr (id + 1) - id) + 1;
res += 1LL * (id > 1 && ex[id - 1]) * (id - rootl (id - 1)) * (id < n && ex[id + 1]) * (rootr (id + 1) - id);
if (id > 1 && ex[id - 1])
{
joinl (id, id - 1);
joinr (id, id - 1);
}
if (id < n && ex[id + 1])
{
joinl (id, id + 1);
joinr (id, id + 1);
}
}
for (int i = 1; i <= q; ++i)
{
cout << ans[i] << "\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... |