답안 #390304

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390304 2021-04-15T19:45:05 Z KrisjanisP Mountains (NOI20_mountains) C++14
100 / 100
1000 ms 31688 KB
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;

typedef pair<ll, ll> pii;

const ll N_mx = 3e5;
ll N;
ll H[N_mx];
ll H_c[N_mx];//compressed H

struct Segment{
    ll i;
    ll l, r;
};

class Tree{
public:
    Tree(ll _n){
        n = _n;
        t.resize(4*n,0);
    }
    void update(ll i, ll v){
        updateSegment(rootSegment(), i, v);
    }
    ll sum(ll l, ll r){
        return sumSegment(rootSegment(), l, r);
    }
private:
    ll n;
    vector<ll> t;
    Segment rootSegment(){
        Segment res;
        res.i = 1;
        res.l = 0;
        res.r = n-1;
        return res;
    }
    Segment getLeft(Segment pos){
        return {
            i: pos.i*2,
            l: pos.l,
            r: (pos.l+pos.r)/2
        };
    }
    Segment getRight(Segment pos){
        return {
            i: pos.i*2 + 1,
            l: (pos.l+pos.r)/2 + 1,
            r: pos.r
        };
    }
    void updateSegment(Segment pos, ll i, ll v){
        if(i<pos.l||i>pos.r) return;
        t[pos.i] += v;
        if(pos.l==pos.r) return;
        updateSegment(getLeft(pos), i, v);
        updateSegment(getRight(pos), i, v);
    }
    ll sumSegment(Segment pos, ll l, ll r){
        if(pos.l>=l&&pos.r<=r) return t[pos.i];
        if(pos.l>r||pos.r<l) return 0;
        ll sum_left = sumSegment(getLeft(pos), l, r);
        ll sum_right = sumSegment(getRight(pos), l, r);
        return sum_left+sum_right;
    }
};

void input(){
    cin>>N;
    for(ll i=0;i<N;i++)
        cin>>H[i];
}

void solve(){
    pii H2[N];
    for(ll i=0;i<N;i++){
        H2[i] = {H[i], i};
    }
    sort(H2, H2+N);
    ll id=0;
    for(ll i=0;i<N;i++){
        if(i&&H2[i].f>H2[i-1].f) id++; 
        H[H2[i].s] = id;
    }
    Tree left(N_mx);
    Tree right(N_mx);
    for(ll i=0;i<N;i++){
        right.update(H[i], 1);
    }
    ll res = 0;
    for(ll i=0;i<N;i++){
        //process this position...
        //1) remove from right at this position
        //2) process this position
        //3) add to left at this position
        right.update(H[i], -1);
        if(H[i]!=0){
            ll res_l = left.sum(0, H[i]-1);
            ll res_r = right.sum(0, H[i]-1);
            res += res_l*res_r;
        }
        left.update(H[i], 1);
    }
    cout<<res;
}

int main(){
    input();
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19020 KB Output is correct
2 Correct 776 ms 31672 KB Output is correct
3 Correct 754 ms 31672 KB Output is correct
4 Correct 718 ms 31680 KB Output is correct
5 Correct 719 ms 31676 KB Output is correct
6 Correct 722 ms 31688 KB Output is correct
7 Correct 712 ms 31676 KB Output is correct
8 Correct 719 ms 31672 KB Output is correct
9 Correct 723 ms 31684 KB Output is correct
10 Correct 742 ms 31608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 498 ms 26796 KB Output is correct
2 Correct 511 ms 26816 KB Output is correct
3 Correct 507 ms 26692 KB Output is correct
4 Correct 501 ms 26820 KB Output is correct
5 Correct 504 ms 26692 KB Output is correct
6 Correct 513 ms 26720 KB Output is correct
7 Correct 501 ms 26692 KB Output is correct
8 Correct 478 ms 26608 KB Output is correct
9 Correct 482 ms 26604 KB Output is correct
10 Correct 10 ms 19020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 498 ms 26796 KB Output is correct
2 Correct 511 ms 26816 KB Output is correct
3 Correct 507 ms 26692 KB Output is correct
4 Correct 501 ms 26820 KB Output is correct
5 Correct 504 ms 26692 KB Output is correct
6 Correct 513 ms 26720 KB Output is correct
7 Correct 501 ms 26692 KB Output is correct
8 Correct 478 ms 26608 KB Output is correct
9 Correct 482 ms 26604 KB Output is correct
10 Correct 10 ms 19020 KB Output is correct
11 Correct 606 ms 26980 KB Output is correct
12 Correct 608 ms 26980 KB Output is correct
13 Correct 606 ms 26948 KB Output is correct
14 Correct 601 ms 27024 KB Output is correct
15 Correct 610 ms 26984 KB Output is correct
16 Correct 596 ms 26948 KB Output is correct
17 Correct 606 ms 26976 KB Output is correct
18 Correct 536 ms 27076 KB Output is correct
19 Correct 558 ms 26960 KB Output is correct
20 Correct 12 ms 19064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19108 KB Output is correct
2 Correct 12 ms 19096 KB Output is correct
3 Correct 12 ms 19020 KB Output is correct
4 Correct 12 ms 19020 KB Output is correct
5 Correct 12 ms 19048 KB Output is correct
6 Correct 12 ms 19024 KB Output is correct
7 Correct 14 ms 19020 KB Output is correct
8 Correct 14 ms 19072 KB Output is correct
9 Correct 11 ms 19124 KB Output is correct
10 Correct 10 ms 19020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19108 KB Output is correct
2 Correct 12 ms 19096 KB Output is correct
3 Correct 12 ms 19020 KB Output is correct
4 Correct 12 ms 19020 KB Output is correct
5 Correct 12 ms 19048 KB Output is correct
6 Correct 12 ms 19024 KB Output is correct
7 Correct 14 ms 19020 KB Output is correct
8 Correct 14 ms 19072 KB Output is correct
9 Correct 11 ms 19124 KB Output is correct
10 Correct 10 ms 19020 KB Output is correct
11 Correct 37 ms 19516 KB Output is correct
12 Correct 40 ms 19520 KB Output is correct
13 Correct 36 ms 19532 KB Output is correct
14 Correct 37 ms 19532 KB Output is correct
15 Correct 37 ms 19500 KB Output is correct
16 Correct 36 ms 19404 KB Output is correct
17 Correct 36 ms 19508 KB Output is correct
18 Correct 36 ms 19532 KB Output is correct
19 Correct 30 ms 19276 KB Output is correct
20 Correct 10 ms 19020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 498 ms 26796 KB Output is correct
2 Correct 511 ms 26816 KB Output is correct
3 Correct 507 ms 26692 KB Output is correct
4 Correct 501 ms 26820 KB Output is correct
5 Correct 504 ms 26692 KB Output is correct
6 Correct 513 ms 26720 KB Output is correct
7 Correct 501 ms 26692 KB Output is correct
8 Correct 478 ms 26608 KB Output is correct
9 Correct 482 ms 26604 KB Output is correct
10 Correct 10 ms 19020 KB Output is correct
11 Correct 606 ms 26980 KB Output is correct
12 Correct 608 ms 26980 KB Output is correct
13 Correct 606 ms 26948 KB Output is correct
14 Correct 601 ms 27024 KB Output is correct
15 Correct 610 ms 26984 KB Output is correct
16 Correct 596 ms 26948 KB Output is correct
17 Correct 606 ms 26976 KB Output is correct
18 Correct 536 ms 27076 KB Output is correct
19 Correct 558 ms 26960 KB Output is correct
20 Correct 12 ms 19064 KB Output is correct
21 Correct 773 ms 27840 KB Output is correct
22 Correct 749 ms 27856 KB Output is correct
23 Correct 742 ms 27820 KB Output is correct
24 Correct 755 ms 27972 KB Output is correct
25 Correct 753 ms 27856 KB Output is correct
26 Correct 749 ms 27804 KB Output is correct
27 Correct 754 ms 27860 KB Output is correct
28 Correct 581 ms 27852 KB Output is correct
29 Correct 595 ms 27856 KB Output is correct
30 Correct 10 ms 19020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19020 KB Output is correct
2 Correct 776 ms 31672 KB Output is correct
3 Correct 754 ms 31672 KB Output is correct
4 Correct 718 ms 31680 KB Output is correct
5 Correct 719 ms 31676 KB Output is correct
6 Correct 722 ms 31688 KB Output is correct
7 Correct 712 ms 31676 KB Output is correct
8 Correct 719 ms 31672 KB Output is correct
9 Correct 723 ms 31684 KB Output is correct
10 Correct 742 ms 31608 KB Output is correct
11 Correct 498 ms 26796 KB Output is correct
12 Correct 511 ms 26816 KB Output is correct
13 Correct 507 ms 26692 KB Output is correct
14 Correct 501 ms 26820 KB Output is correct
15 Correct 504 ms 26692 KB Output is correct
16 Correct 513 ms 26720 KB Output is correct
17 Correct 501 ms 26692 KB Output is correct
18 Correct 478 ms 26608 KB Output is correct
19 Correct 482 ms 26604 KB Output is correct
20 Correct 10 ms 19020 KB Output is correct
21 Correct 606 ms 26980 KB Output is correct
22 Correct 608 ms 26980 KB Output is correct
23 Correct 606 ms 26948 KB Output is correct
24 Correct 601 ms 27024 KB Output is correct
25 Correct 610 ms 26984 KB Output is correct
26 Correct 596 ms 26948 KB Output is correct
27 Correct 606 ms 26976 KB Output is correct
28 Correct 536 ms 27076 KB Output is correct
29 Correct 558 ms 26960 KB Output is correct
30 Correct 12 ms 19064 KB Output is correct
31 Correct 11 ms 19108 KB Output is correct
32 Correct 12 ms 19096 KB Output is correct
33 Correct 12 ms 19020 KB Output is correct
34 Correct 12 ms 19020 KB Output is correct
35 Correct 12 ms 19048 KB Output is correct
36 Correct 12 ms 19024 KB Output is correct
37 Correct 14 ms 19020 KB Output is correct
38 Correct 14 ms 19072 KB Output is correct
39 Correct 11 ms 19124 KB Output is correct
40 Correct 10 ms 19020 KB Output is correct
41 Correct 37 ms 19516 KB Output is correct
42 Correct 40 ms 19520 KB Output is correct
43 Correct 36 ms 19532 KB Output is correct
44 Correct 37 ms 19532 KB Output is correct
45 Correct 37 ms 19500 KB Output is correct
46 Correct 36 ms 19404 KB Output is correct
47 Correct 36 ms 19508 KB Output is correct
48 Correct 36 ms 19532 KB Output is correct
49 Correct 30 ms 19276 KB Output is correct
50 Correct 10 ms 19020 KB Output is correct
51 Correct 773 ms 27840 KB Output is correct
52 Correct 749 ms 27856 KB Output is correct
53 Correct 742 ms 27820 KB Output is correct
54 Correct 755 ms 27972 KB Output is correct
55 Correct 753 ms 27856 KB Output is correct
56 Correct 749 ms 27804 KB Output is correct
57 Correct 754 ms 27860 KB Output is correct
58 Correct 581 ms 27852 KB Output is correct
59 Correct 595 ms 27856 KB Output is correct
60 Correct 10 ms 19020 KB Output is correct
61 Correct 961 ms 31672 KB Output is correct
62 Correct 950 ms 31688 KB Output is correct
63 Correct 1000 ms 31656 KB Output is correct
64 Correct 972 ms 31688 KB Output is correct
65 Correct 962 ms 31684 KB Output is correct
66 Correct 968 ms 31684 KB Output is correct
67 Correct 987 ms 31672 KB Output is correct
68 Correct 885 ms 31652 KB Output is correct
69 Correct 773 ms 27980 KB Output is correct
70 Correct 10 ms 19020 KB Output is correct