Submission #966953

# Submission time Handle Problem Language Result Execution time Memory
966953 2024-04-20T17:28:41 Z stefanneagu Pilot (NOI19_pilot) C++17
100 / 100
493 ms 84608 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int nmax = 1e6 + 1;

struct str {
  int aa, i;
} a[nmax], ord[nmax];

int v[nmax], ans[nmax], sz[nmax], root[nmax];

bool f[nmax];

int cmp(str x, str y) {
  return x.aa < y.aa;
}

int find(int a) {
  if(root[a] == a) {
    return a;
  }
  return root[a] = find(root[a]);
}

int curr = 0;

void unite(int a, int b) {
  if(a == b) {
    cout << a << " " << b << '\n';
    exit(0);
    // assert(0); // nu cred ca ar tb sa aj aici
  }
  if(sz[a] < sz[b]) {
    swap(a, b);
  }
  curr += sz[a] * sz[b];
  // cout << "+ " << sz[a] << " " << sz[b] << '\n';
  root[b] = a;
  sz[a] += sz[b];
}

void upd(int a) {
  f[a] = 1;
  curr++;
  if(f[a - 1] == 1) {
    // cout << "? " << a << " " << a - 1 << '\n';
    unite(find(a), find(a - 1));
  }
  if(f[a + 1] == 1) {
    // cout << "! " << a << " " << a + 1 << '\n';
    unite(find(a), find(a + 1));
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  int n, q;
  cin >> n >> q;
  for(int i = 1; i <= n; i++) {
    sz[i] = 1, root[i] = i;
    cin >> v[i];
    ord[i].aa = v[i];
    ord[i].i = i;
  }
  sort(ord + 1, ord + n + 1, cmp);
  for(int i = 1; i <= q; i++) {
    cin >> a[i].aa;
    a[i].i = i;
  }
  sort(a + 1, a + q + 1, cmp);
  int j = 1;
  for(int i = 1; i <= q; i++) { // rainboy orz
    while(j <= n && ord[j].aa <= a[i].aa) {
      // cout << "~ " << ord[j].i << "\n";
      upd(ord[j].i);
      j++;
    }
    ans[a[i].i] = curr;
    // cout << '\n';
  }
  for(int i = 1; i <= q; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12632 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12632 KB Output is correct
19 Correct 2 ms 12632 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12632 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12632 KB Output is correct
19 Correct 2 ms 12632 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 2 ms 12756 KB Output is correct
23 Correct 2 ms 12636 KB Output is correct
24 Correct 2 ms 12636 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 16732 KB Output is correct
2 Correct 19 ms 16732 KB Output is correct
3 Correct 18 ms 16964 KB Output is correct
4 Correct 18 ms 16728 KB Output is correct
5 Correct 18 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 18160 KB Output is correct
2 Correct 34 ms 19932 KB Output is correct
3 Correct 34 ms 19792 KB Output is correct
4 Correct 33 ms 17748 KB Output is correct
5 Correct 32 ms 17744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 17744 KB Output is correct
2 Correct 34 ms 17880 KB Output is correct
3 Correct 35 ms 19804 KB Output is correct
4 Correct 33 ms 17756 KB Output is correct
5 Correct 35 ms 19928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 18 ms 16732 KB Output is correct
12 Correct 19 ms 16732 KB Output is correct
13 Correct 18 ms 16964 KB Output is correct
14 Correct 18 ms 16728 KB Output is correct
15 Correct 18 ms 16732 KB Output is correct
16 Correct 18 ms 16984 KB Output is correct
17 Correct 17 ms 16724 KB Output is correct
18 Correct 17 ms 16724 KB Output is correct
19 Correct 15 ms 16728 KB Output is correct
20 Correct 17 ms 16724 KB Output is correct
21 Correct 18 ms 16732 KB Output is correct
22 Correct 17 ms 16868 KB Output is correct
23 Correct 19 ms 16868 KB Output is correct
24 Correct 16 ms 16732 KB Output is correct
25 Correct 17 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12632 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12632 KB Output is correct
19 Correct 2 ms 12632 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 2 ms 12756 KB Output is correct
23 Correct 2 ms 12636 KB Output is correct
24 Correct 2 ms 12636 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
26 Correct 18 ms 16732 KB Output is correct
27 Correct 19 ms 16732 KB Output is correct
28 Correct 18 ms 16964 KB Output is correct
29 Correct 18 ms 16728 KB Output is correct
30 Correct 18 ms 16732 KB Output is correct
31 Correct 32 ms 18160 KB Output is correct
32 Correct 34 ms 19932 KB Output is correct
33 Correct 34 ms 19792 KB Output is correct
34 Correct 33 ms 17748 KB Output is correct
35 Correct 32 ms 17744 KB Output is correct
36 Correct 32 ms 17744 KB Output is correct
37 Correct 34 ms 17880 KB Output is correct
38 Correct 35 ms 19804 KB Output is correct
39 Correct 33 ms 17756 KB Output is correct
40 Correct 35 ms 19928 KB Output is correct
41 Correct 18 ms 16984 KB Output is correct
42 Correct 17 ms 16724 KB Output is correct
43 Correct 17 ms 16724 KB Output is correct
44 Correct 15 ms 16728 KB Output is correct
45 Correct 17 ms 16724 KB Output is correct
46 Correct 18 ms 16732 KB Output is correct
47 Correct 17 ms 16868 KB Output is correct
48 Correct 19 ms 16868 KB Output is correct
49 Correct 16 ms 16732 KB Output is correct
50 Correct 17 ms 16732 KB Output is correct
51 Correct 48 ms 19700 KB Output is correct
52 Correct 48 ms 19532 KB Output is correct
53 Correct 40 ms 19796 KB Output is correct
54 Correct 40 ms 19612 KB Output is correct
55 Correct 39 ms 17748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12632 KB Output is correct
9 Correct 2 ms 12632 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12632 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12632 KB Output is correct
19 Correct 2 ms 12632 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 2 ms 12756 KB Output is correct
23 Correct 2 ms 12636 KB Output is correct
24 Correct 2 ms 12636 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
26 Correct 18 ms 16732 KB Output is correct
27 Correct 19 ms 16732 KB Output is correct
28 Correct 18 ms 16964 KB Output is correct
29 Correct 18 ms 16728 KB Output is correct
30 Correct 18 ms 16732 KB Output is correct
31 Correct 32 ms 18160 KB Output is correct
32 Correct 34 ms 19932 KB Output is correct
33 Correct 34 ms 19792 KB Output is correct
34 Correct 33 ms 17748 KB Output is correct
35 Correct 32 ms 17744 KB Output is correct
36 Correct 32 ms 17744 KB Output is correct
37 Correct 34 ms 17880 KB Output is correct
38 Correct 35 ms 19804 KB Output is correct
39 Correct 33 ms 17756 KB Output is correct
40 Correct 35 ms 19928 KB Output is correct
41 Correct 18 ms 16984 KB Output is correct
42 Correct 17 ms 16724 KB Output is correct
43 Correct 17 ms 16724 KB Output is correct
44 Correct 15 ms 16728 KB Output is correct
45 Correct 17 ms 16724 KB Output is correct
46 Correct 18 ms 16732 KB Output is correct
47 Correct 17 ms 16868 KB Output is correct
48 Correct 19 ms 16868 KB Output is correct
49 Correct 16 ms 16732 KB Output is correct
50 Correct 17 ms 16732 KB Output is correct
51 Correct 48 ms 19700 KB Output is correct
52 Correct 48 ms 19532 KB Output is correct
53 Correct 40 ms 19796 KB Output is correct
54 Correct 40 ms 19612 KB Output is correct
55 Correct 39 ms 17748 KB Output is correct
56 Correct 470 ms 82788 KB Output is correct
57 Correct 470 ms 82888 KB Output is correct
58 Correct 432 ms 81748 KB Output is correct
59 Correct 453 ms 82380 KB Output is correct
60 Correct 493 ms 84608 KB Output is correct