Submission #1277004

#TimeUsernameProblemLanguageResultExecution timeMemory
1277004ducdevPilot (NOI19_pilot)C++17
100 / 100
442 ms78140 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;
    const int Q = MAX_Q;

    int par[N + 5], sz[N + 5];
    ll ans[Q + 5];
    vector<int> idx[N + 5];

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

    ll unionSet(int u, int v) {
        u = findSet(u), v = findSet(v);

        if (u == v) return 0;

        if (sz[u] < sz[v]) swap(u, v);

        ll ret = (ll)sz[u] * sz[v];

        sz[u] += sz[v];
        par[v] = u;

        return ret;
    };

    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;
        ll lastAns = 0;
        for (ii query : queries) {
            int x = query.first;
            int qIdx = query.second;

            while (curVal <= x) {
                lastAns += idx[curVal].size();

                for (int i : idx[curVal]) {
                    if (i > 1 && h[i - 1] <= curVal) lastAns += unionSet(i, i - 1);
                    if (i < n && h[i + 1] <= curVal) lastAns += unionSet(i, i + 1);
                };

                curVal++;
            };

            ans[qIdx] = lastAns;
        };

        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)

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