Submission #1219048

#TimeUsernameProblemLanguageResultExecution timeMemory
1219048phuonglinhn09Pilot (NOI19_pilot)C++20
3 / 100
69 ms56140 KiB
/**
 *   author:   phuonglinhn09
 *   created:  17.06.2025
 */

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int nmax = 1e6;

int n, q, a[nmax + 5], ans[nmax + 5];
int h[nmax + 5], y[nmax + 5];
vector<int> vt[nmax + 5], query[nmax + 5];

namespace DSU{
    int par[nmax + 5], sz[nmax + 5];

    ll get_val(int cnt) {
        ll ans = cnt;
        return ans * (ans + 1) / 2;
    }

    void preprocess() {
        for (int i = 1; i <= n; i++) {
            par[i] = i;
            sz[i] = 1;
        }
    }

    int find_set(int u) {
        return u == par[u] ? u : par[u] = find_set(par[u]);
    }

    ll Union (int u, int v) {
        u = find_set(u);
        v = find_set(v);
        if (u == v) return 0;

        if (sz[v] > sz[u]) swap(u, v);
        ll cur_ans = get_val(sz[u] + sz[v]) - get_val(sz[u]) - get_val(sz[v]);
        par[v] = u, sz[u] += sz[v];
        return cur_ans;
    }
}

int main() {
    ios_base::sync_with_stdio(0); 
    cin.tie(0); cout.tie(0);
    
    if (fopen (".INP", "r")) {
        freopen (".INP", "r", stdin);
        freopen (".OUT", "w", stdout);
    }

    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
        vt[h[i]].emplace_back(i);
    }
    for (int i = 1; i <= q; i++) {
        cin >> y[i];
        query[y[i]].emplace_back(i);
    }

    sort(h + 1, h + 1 + n);
    sort(y + 1, y + 1 + q);

    DSU::preprocess();
    ll cur_ans = 0;
    int j = 0;

    for (int i = 1; i <= q; i++) {
        while (j < n && h[j + 1] <= y[i]) {
            j++;
            for (auto pos:vt[h[j]]) {
                a[pos] = h[j];
                cur_ans++;
                if (pos > 1 && a[pos - 1]) cur_ans += DSU::Union(pos, pos - 1);
                if (pos < n && a[pos + 1]) cur_ans += DSU::Union(pos, pos + 1);
            }
            vt[h[j]].clear();
        }
        ans[i] = cur_ans;
    }

    for (int i = 1; i <= q; i++) {
        cout << ans[i] << "\n";
    }
    
    return 0;
}

Compilation message (stderr)

pilot.cpp: In function 'int main()':
pilot.cpp:54:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         freopen (".INP", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
pilot.cpp:55:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen (".OUT", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...