Submission #861136

# Submission time Handle Problem Language Result Execution time Memory
861136 2023-10-15T12:12:18 Z sleepntsheep Mountains (NOI20_mountains) C++17
100 / 100
876 ms 215228 KB
#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;

#define N 300005
#define ALL(x) x.begin(), x.end()

#define int long long
int ans, n, a[N];

struct node
{
    node *l, *r;
    int a;
    node(int b) : a(b), l(nullptr), r(nullptr) {}
    node (node *l_, node *r_) : l(l_), r(r_), a((l_?l_->a:0) + (r_?r_->a:0)) {}
};
 
node *build(int l, int r)
{
    if (l == r) return new node(0);
    return new node(build(l, (l+r)/2), build((l+r)/2+1, r));
}
 
node *upd(node *v, int l, int r, int p)
{
    if (l == r) return new node(v->a + 1);
    if (p <= (l+r)/2) return new node(upd(v->l, l, (l+r)/2, p), v->r);
    return new node(v->l, upd(v->r, (l+r)/2+1, r, p));
}
 
int count(node *vl, node *vr, int l, int r, int x, int y)
{
    if (y < l || r < x) return 0;
    if (x <= l && r <= y) return vr->a - vl->a;
    return count(vl->l, vr->l, l, (l+r)/2, x, y) + count(vl->r, vr->r, (l+r)/2+1, r, x, y);
}
 
node *root[N];
vector<int> C;

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i], C.push_back(a[i]);
    sort(ALL(C));
    root[0] = build(1, n);
    for (int i = 1; i <= n; ++i) a[i] = lower_bound(ALL(C), a[i]) - C.begin() + 1,
         root[i] = upd(root[i-1], 1, n, a[i]);

    for (int i = 1; i <= n; ++i) 
    {
        int lft = count(root[0], root[i], 1, n, 0, a[i] - 1);
        int rgt = count(root[i], root[n], 1, n, 0, a[i] - 1);
        ans += lft * rgt;
        //cout << "E " << lft << ' ' << rgt<<endl;
    }
    cout << ans;

    return 0;
}


Compilation message

Mountains.cpp: In constructor 'node::node(long long int)':
Mountains.cpp:24:9: warning: 'node::a' will be initialized after [-Wreorder]
   24 |     int a;
      |         ^
Mountains.cpp:23:11: warning:   'node* node::l' [-Wreorder]
   23 |     node *l, *r;
      |           ^
Mountains.cpp:25:5: warning:   when initialized here [-Wreorder]
   25 |     node(int b) : a(b), l(nullptr), r(nullptr) {}
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 411 ms 206884 KB Output is correct
3 Correct 364 ms 207808 KB Output is correct
4 Correct 354 ms 207060 KB Output is correct
5 Correct 353 ms 208576 KB Output is correct
6 Correct 351 ms 207548 KB Output is correct
7 Correct 347 ms 207036 KB Output is correct
8 Correct 351 ms 207240 KB Output is correct
9 Correct 355 ms 207372 KB Output is correct
10 Correct 363 ms 207108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 213952 KB Output is correct
2 Correct 283 ms 210956 KB Output is correct
3 Correct 271 ms 211256 KB Output is correct
4 Correct 272 ms 214720 KB Output is correct
5 Correct 254 ms 211400 KB Output is correct
6 Correct 269 ms 211392 KB Output is correct
7 Correct 263 ms 210432 KB Output is correct
8 Correct 241 ms 215228 KB Output is correct
9 Correct 239 ms 210564 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 213952 KB Output is correct
2 Correct 283 ms 210956 KB Output is correct
3 Correct 271 ms 211256 KB Output is correct
4 Correct 272 ms 214720 KB Output is correct
5 Correct 254 ms 211400 KB Output is correct
6 Correct 269 ms 211392 KB Output is correct
7 Correct 263 ms 210432 KB Output is correct
8 Correct 241 ms 215228 KB Output is correct
9 Correct 239 ms 210564 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 365 ms 208636 KB Output is correct
12 Correct 357 ms 207920 KB Output is correct
13 Correct 355 ms 208132 KB Output is correct
14 Correct 362 ms 207808 KB Output is correct
15 Correct 357 ms 209088 KB Output is correct
16 Correct 358 ms 207808 KB Output is correct
17 Correct 358 ms 208316 KB Output is correct
18 Correct 312 ms 207808 KB Output is correct
19 Correct 278 ms 207548 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 0 ms 2512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 0 ms 2512 KB Output is correct
11 Correct 11 ms 7772 KB Output is correct
12 Correct 14 ms 7768 KB Output is correct
13 Correct 11 ms 7772 KB Output is correct
14 Correct 14 ms 7744 KB Output is correct
15 Correct 11 ms 7768 KB Output is correct
16 Correct 11 ms 7768 KB Output is correct
17 Correct 11 ms 7772 KB Output is correct
18 Correct 12 ms 8028 KB Output is correct
19 Correct 10 ms 7772 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 213952 KB Output is correct
2 Correct 283 ms 210956 KB Output is correct
3 Correct 271 ms 211256 KB Output is correct
4 Correct 272 ms 214720 KB Output is correct
5 Correct 254 ms 211400 KB Output is correct
6 Correct 269 ms 211392 KB Output is correct
7 Correct 263 ms 210432 KB Output is correct
8 Correct 241 ms 215228 KB Output is correct
9 Correct 239 ms 210564 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 365 ms 208636 KB Output is correct
12 Correct 357 ms 207920 KB Output is correct
13 Correct 355 ms 208132 KB Output is correct
14 Correct 362 ms 207808 KB Output is correct
15 Correct 357 ms 209088 KB Output is correct
16 Correct 358 ms 207808 KB Output is correct
17 Correct 358 ms 208316 KB Output is correct
18 Correct 312 ms 207808 KB Output is correct
19 Correct 278 ms 207548 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 764 ms 210172 KB Output is correct
22 Correct 781 ms 208832 KB Output is correct
23 Correct 800 ms 210112 KB Output is correct
24 Correct 794 ms 210128 KB Output is correct
25 Correct 805 ms 208812 KB Output is correct
26 Correct 805 ms 210176 KB Output is correct
27 Correct 812 ms 210492 KB Output is correct
28 Correct 339 ms 210188 KB Output is correct
29 Correct 325 ms 210320 KB Output is correct
30 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 411 ms 206884 KB Output is correct
3 Correct 364 ms 207808 KB Output is correct
4 Correct 354 ms 207060 KB Output is correct
5 Correct 353 ms 208576 KB Output is correct
6 Correct 351 ms 207548 KB Output is correct
7 Correct 347 ms 207036 KB Output is correct
8 Correct 351 ms 207240 KB Output is correct
9 Correct 355 ms 207372 KB Output is correct
10 Correct 363 ms 207108 KB Output is correct
11 Correct 284 ms 213952 KB Output is correct
12 Correct 283 ms 210956 KB Output is correct
13 Correct 271 ms 211256 KB Output is correct
14 Correct 272 ms 214720 KB Output is correct
15 Correct 254 ms 211400 KB Output is correct
16 Correct 269 ms 211392 KB Output is correct
17 Correct 263 ms 210432 KB Output is correct
18 Correct 241 ms 215228 KB Output is correct
19 Correct 239 ms 210564 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 365 ms 208636 KB Output is correct
22 Correct 357 ms 207920 KB Output is correct
23 Correct 355 ms 208132 KB Output is correct
24 Correct 362 ms 207808 KB Output is correct
25 Correct 357 ms 209088 KB Output is correct
26 Correct 358 ms 207808 KB Output is correct
27 Correct 358 ms 208316 KB Output is correct
28 Correct 312 ms 207808 KB Output is correct
29 Correct 278 ms 207548 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2648 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 0 ms 2512 KB Output is correct
41 Correct 11 ms 7772 KB Output is correct
42 Correct 14 ms 7768 KB Output is correct
43 Correct 11 ms 7772 KB Output is correct
44 Correct 14 ms 7744 KB Output is correct
45 Correct 11 ms 7768 KB Output is correct
46 Correct 11 ms 7768 KB Output is correct
47 Correct 11 ms 7772 KB Output is correct
48 Correct 12 ms 8028 KB Output is correct
49 Correct 10 ms 7772 KB Output is correct
50 Correct 1 ms 2392 KB Output is correct
51 Correct 764 ms 210172 KB Output is correct
52 Correct 781 ms 208832 KB Output is correct
53 Correct 800 ms 210112 KB Output is correct
54 Correct 794 ms 210128 KB Output is correct
55 Correct 805 ms 208812 KB Output is correct
56 Correct 805 ms 210176 KB Output is correct
57 Correct 812 ms 210492 KB Output is correct
58 Correct 339 ms 210188 KB Output is correct
59 Correct 325 ms 210320 KB Output is correct
60 Correct 1 ms 2392 KB Output is correct
61 Correct 851 ms 213244 KB Output is correct
62 Correct 831 ms 213076 KB Output is correct
63 Correct 826 ms 213616 KB Output is correct
64 Correct 876 ms 213152 KB Output is correct
65 Correct 820 ms 213808 KB Output is correct
66 Correct 830 ms 213268 KB Output is correct
67 Correct 837 ms 212628 KB Output is correct
68 Correct 812 ms 213544 KB Output is correct
69 Correct 839 ms 210244 KB Output is correct
70 Correct 1 ms 2396 KB Output is correct