제출 #1359172

#제출 시각아이디문제언어결과실행 시간메모리
1359172eweirdf274rPilot (NOI19_pilot)C++20
100 / 100
329 ms39684 KiB
#include <bits/stdc++.h>
using namespace std;

int parent[1000001], sz[1000001];
bool active[1000001];
long long total = 0;
int N, Q;

int find(int x) {
    while (parent[x] != x) x = parent[x] = parent[parent[x]];
    return x;
}

void unite(int a, int b) {
    a = find(a); b = find(b);
    if (a == b) return;
    // Remove old contributions
    total -= (long long)sz[a]*(sz[a]+1)/2;
    total -= (long long)sz[b]*(sz[b]+1)/2;
    if (sz[a] < sz[b]) swap(a, b);
    parent[b] = a;
    sz[a] += sz[b];
    // Add new contribution
    total += (long long)sz[a]*(sz[a]+1)/2;
}

void activate(int i) {
    active[i] = true;
    parent[i] = i;
    sz[i] = 1;
    total += 1;
    if (i > 1 && active[i-1]) unite(i, i-1);
    if (i < N && active[i+1]) unite(i, i+1);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> N >> Q;
    vector<int> H(N+1);
    for (int i = 1; i <= N; i++) cin >> H[i];
    
    vector<int> Y(Q);
    for (int i = 0; i < Q; i++) cin >> Y[i];
    
    // Sort mountains by height
    vector<int> mtn_order(N);
    iota(mtn_order.begin(), mtn_order.end(), 1);
    sort(mtn_order.begin(), mtn_order.end(), [&](int a, int b){ return H[a] < H[b]; });
    
    // Sort queries by Y, keep original index
    vector<int> q_order(Q);
    iota(q_order.begin(), q_order.end(), 0);
    sort(q_order.begin(), q_order.end(), [&](int a, int b){ return Y[a] < Y[b]; });
    
    vector<long long> ans(Q);
    int mi = 0;
    for (int qi = 0; qi < Q; qi++) {
        int idx = q_order[qi];
        int y = Y[idx];
        while (mi < N && H[mtn_order[mi]] <= y) {
            activate(mtn_order[mi]);
            mi++;
        }
        ans[idx] = total;
    }
    
    for (int i = 0; i < Q; i++) cout << ans[i] << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...