Submission #1257138

#TimeUsernameProblemLanguageResultExecution timeMemory
1257138CrabCNHPilot (NOI19_pilot)C++20
100 / 100
391 ms70884 KiB
#include <bits/stdc++.h>

#define task     "BriantheCrab"

#define int    long long
#define pii    pair <int, int>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using namespace std;

template <class T> void minimize (T &t, T f) {if (t > f) t = f;}
template <class T> void maximize (T &t, T f) {if (t < f) t = f;}

const int maxN = 1e6 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;

// khong tu code thi khong kha len duoc dau
// biet sol roi thi tu lam not di


int n, q;
int h[maxN];
int par[maxN], sz[maxN];
int res[maxN];
bool visited[maxN];
int sum = 0;

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

int getRoot (int u) {
    if (par[u] == u) {
        return u;
    }
    return (par[u] = getRoot (par[u]));
}

inline void merge (int u, int v) {
    u = getRoot (u);
    v = getRoot (v);
    if (u == v) {
        return;
    }
    if (sz[u] < sz[v]) {
        swap (u, v);
    }
    par[v] = u;
    sum -= sz[u] * (sz[u] + 1) / 2;
    sum -= sz[v] * (sz[v] + 1) / 2; 
    sz[u] += sz[v];
    sum += sz[u] * (sz[u] + 1) / 2;
    //cout << "add "  << sum << '\n';
    return;
}

void solve () {
    cin >> n >> q;
    vector <pii> cc;
    for (int i = 1; i <= n; i ++) {
        cin >> h[i];
        cc.push_back ({h[i], i});
    }
    vector <pii> que;
    for (int i = 1; i <= q; i ++) {
        int t;
        cin >> t;
        que.push_back ({t, i});
    }
    sort (all (que));
    sort (all (cc));
    init ();
    int j = 0;
    for (int i = 0; i < q; i ++) {
        while (j < n && cc[j].fi <= que[i].fi) {
            int id = cc[j].se;
            visited[id] = 1;
            sum += sz[id] * (sz[id] + 1) / 2;
            //cout << "add " << sum << '\n';
            if (id >= 2 && visited[id - 1]) {
                merge (id - 1, id);
            } 
            if (id <= n - 1 && visited[id + 1]) {
                merge (id, id + 1);
            }
            j ++;
        }
        res[que[i].se] = sum;
    }
    for (int i = 1; i <= q; i ++) {
        cout << res[i] << '\n';
    }
    return;
}

signed main () {
    cin.tie (nullptr) -> sync_with_stdio (false);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int t = 1;
    //cin >> t;
    while (t --) {
        solve ();
    } 
    return 0;
}
// thfv

Compilation message (stderr)

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