# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277002 | ducdev | Pilot (NOI19_pilot) | C++17 | 48 ms | 30552 KiB |
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> ii;
const int MAX_N = 1e6;
const int MAX_Q = 1e6;
const int MOD = 1e9 + 7;
int n, q;
int h[MAX_N + 5];
vector<ii> queries;
namespace SUBTASK_5_8 {
const int N = MAX_N;
int st[20][N + 5];
int jump(int idx, int x) {
for (int i = 19; i >= 0; i--) {
if (idx + (1 << i) - 1 <= n && st[i][idx] <= x) {
idx += 1 << i;
};
};
return idx - 1;
};
void Solve() {
for (int i = 1; i <= n; i++) st[0][i] = h[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
};
};
for (ii query : queries) {
int x = query.first;
ll res = 0;
for (int i = 1; i <= n; i++) {
if (h[i] > x) continue;
int j = jump(i, x);
res += j - i + 1;
};
cout << res << '\n';
};
};
}; // namespace SUBTASK_5_8
namespace SUBTASK_67 {
const int N = MAX_N;
const int Q = MAX_Q;
ll ans[Q + 5];
void Solve() {
sort(all(queries));
int r = 1;
for (int i = 0; i < q; i++) {
int x = queries[i].first;
int qIdx = queries[i].second;
while (h[r] <= x && r <= n) r++;
ans[qIdx] += (ll)r * (r - 1) / 2;
};
for (int i = 1; i <= q; i++)
cout << ans[i] << '\n';
};
}; // namespace SUBTASK_67
namespace SUBTASK_10 {
const int N = MAX_N;
int par[N + 5], sz[N + 5];
ll ans[N + 5];
vector<int> idx[N + 5];
int findSet(int u) {
return u == par[u] ? u : par[u] = findSet(par[u]);
};
bool unionSet(int u, int v) {
u = findSet(u), v = findSet(v);
if (u == v) return false;
if (sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
return true;
};
void Solve() {
for (int u = 1; u <= n; u++) {
par[u] = u;
sz[u] = 1;
};
for (int i = 1; i <= n; i++) idx[h[i]].push_back(i);
sort(all(queries));
int curVal = 0;
int preQIdx = -1;
for (ii query : queries) {
int x = query.first;
int qIdx = query.second;
if (preQIdx > -1) ans[qIdx] = ans[preQIdx];
while (curVal <= x) {
ans[qIdx] += idx[curVal].size();
for (int i : idx[curVal]) {
if (i > 1 && h[i - 1] <= curVal) {
if (findSet(i) == findSet(i - 1)) continue;
ans[qIdx] += (ll)sz[findSet(i)] * sz[findSet(i - 1)];
unionSet(i, i - 1);
};
if (i < n && h[i + 1] <= curVal) {
if (findSet(i) == findSet(i + 1)) continue;
ans[qIdx] += (ll)sz[findSet(i)] * sz[findSet(i + 1)];
unionSet(i, i + 1);
};
};
curVal++;
};
preQIdx = qIdx;
};
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
};
}; // namespace SUBTASK_10
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("MAIN.INP", "r")) {
freopen("MAIN.INP", "r", stdin);
freopen("MAIN.OUT", "w", stdout);
};
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> h[i];
};
for (int i = 1; i <= q; i++) {
int y;
cin >> y;
queries.push_back(make_pair(y, i));
};
// SUBTASK_5_8::Solve();
// SUBTASK_67::Solve();
SUBTASK_10::Solve();
};
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... |