# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277687 | wedonttalkanymore | Pilot (NOI19_pilot) | C++20 | 398 ms | 63124 KiB |
#include <bits/stdc++.h>
/*
Brute to win
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 1e6 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n, q;
pii a[N];
vector <pii> query;
int res = 0;
int ans[N];
bool on[N];
struct DSU {
int par[N], sz[N];
void make() {
for (int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
}
}
int find(int u) {
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void join(int u, int v) {
u = find(u), v = find(v);
if (u != v) {
if (sz[u] < sz[v]) swap(u, v);
res -= sz[u] * (sz[u] + 1) / 2;
res -= sz[v] * (sz[v] + 1) / 2;
sz[u] += sz[v];
par[v] = u;
res += sz[u] * (sz[u] + 1) / 2;
}
}
} dsu;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> q;
dsu.make();
for (int i = 1; i <= n; i++) {
cin >> a[i].fi;
a[i].se = i;
}
for (int i = 1; i <= q; i++) {
int x;
cin >> x;
query.emplace_back(x, i);
}
sort(a + 1, a + n + 1);
sort(query.begin(), query.end());
int l = 1;
for (int i = 0; i < q; i++) {
while(l <= n && a[l].fi <= query[i].fi) {
on[a[l].se] = 1;
res++;
if (a[l].se != 1 && on[a[l].se - 1]) dsu.join(a[l].se - 1, a[l].se);
if (a[l].se != n && on[a[l].se + 1]) dsu.join(a[l].se, a[l].se + 1);
l++;
}
ans[query[i].se] = res;
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
return 0;
}
Compilation message (stderr)
# | 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... |