# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1270172 | quangminh412 | Pilot (NOI19_pilot) | C++17 | 401 ms | 46696 KiB |
/*
Ben Watson
Quang Minh MasterDDDDD
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const string name = "concert";
void solve();
signed main()
{
if (fopen((name + ".inp").c_str(), "r"))
{
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
return 0;
}
// main program
const int maxn = 1e6 + 1;
struct DSU
{
vector<int> par;
int n;
DSU(int n) : n(n)
{
par.resize(n + 1, 0);
for (int i = 1; i <= n; i++)
par[i] = i;
}
int find(int u) { return (u == par[u] ? u : par[u] = find(par[u])); }
void merge(int u, int v)
{
u = find(u);
v = find(v);
if (u == v) return;
par[u] = v;
}
};
vector<pair<int, int>> queries, proc;
int h[maxn], mark[maxn];
DSU L(maxn), R(maxn);
ll res[maxn];
int n, q;
ll compute(int n) { return 1ll * n * (n + 1) / 2; }
ll ans = 0;
void update(int pos)
{
mark[pos] = 1;
int l = (pos == 1 || mark[pos - 1] == 0 ? pos : L.find(pos - 1));
int r = (pos == n || mark[pos + 1] == 0 ? pos : R.find(pos + 1));
if (l != pos)
{
ans -= compute(pos - l);
L.merge(pos, pos - 1);
R.merge(pos - 1, pos);
}
if (r != pos)
{
ans -= compute(r - pos);
R.merge(pos, pos + 1);
L.merge(pos + 1, pos);
}
ans += compute(r - l + 1);
}
void solve()
{
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
cin >> h[i];
proc.push_back({h[i], i});
}
for (int i = 0; i < q; i++)
{
int y; cin >> y;
queries.push_back({y, i});
}
sort(queries.begin(), queries.end());
sort(proc.begin(), proc.end());
int cur = 0;
for (pair<int, int> it : queries)
{
int lim = it.first, idx = it.second;
while (cur < n && proc[cur].first <= lim)
{
update(proc[cur].second);
cur++;
}
res[idx] = ans;
}
for (int i = 0; i < q; i++)
cout << res[i] << '\n';
}
컴파일 시 표준 에러 (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... |