Submission #1082848

# Submission time Handle Problem Language Result Execution time Memory
1082848 2024-09-01T21:15:33 Z MrPavlito Mountains (NOI20_mountains) C++17
100 / 100
367 ms 41668 KB
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>

using namespace std;

const int MAXN = 3e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;

vector<int> niz(MAXN);
vector<int> preseg(MAXN<<2);
vector<int> sufseg(MAXN<<2);


void update(int nod, int tl, int tr,int index, int v, vector<int>&seg)
{
    if(tl == tr)
    {
        seg[nod] += v;
        return;
    }
    int mid = tl+tr>>1;
    if(index<=mid)update(nod<<1, tl, mid, index, v, seg);
    else update(nod<<1|1, mid+1, tr, index, v, seg);
    seg[nod] = seg[nod<<1] + seg[nod<<1|1];
}


int query(int nod, int tl, int tr, int l, int r, vector<int>&seg)
{
    if(tl>tr || tl>r || tr<l)return 0;
    if(tl>=l && tr<=r)return seg[nod];
    int mid = tl+tr>>1;
    return query(nod<<1, tl, mid, l, r, seg) + query(nod<<1|1, mid+1,  tr, l, r, seg);
}


signed main()
{
    ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
    int tt=1;
    //cin >> tt;
    while(tt--)
    {
        int n;
        cin >> n;
        vector<int> compress;
        unordered_map<int,int> vals;
        for(int i=1; i<=n; i++)
        {
            cin >> niz[i];
            compress.pb(niz[i]);
        }
        sort(all(compress));
        //compress.erase(unique(all(compress), compress.end()));
        int c = 0;
        for(int i=0; i<(int)compress.size(); i++)if(!vals.count(compress[i]))vals[compress[i]] = ++c;
        for(int i=1; i<=n; i++)niz[i] = vals[niz[i]];
        for(int i=3; i<=n; i++)update(1,1,n,niz[i], 1, sufseg);
        update(1,1,n, niz[1], 1,preseg);
        long long rez = 0;

        for(int i=2; i<n; i++)
        {
            int q1 = query(1,1,n,1,niz[i]-1, preseg);
            int q2 = query(1,1,n,1,niz[i]-1, sufseg);
            rez+=q1*q2;
            update(1,1,n, niz[i],1, preseg);
            update(1,1,n, niz[i+1],-1, sufseg);
        }

        cout << rez << endl;




    }
}

/*
6
500 20 900 0 900 70
 */

Compilation message

Mountains.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int, std::vector<long long int>&)':
Mountains.cpp:29:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |     int mid = tl+tr>>1;
      |               ~~^~~
Mountains.cpp: In function 'long long int query(long long int, long long int, long long int, long long int, long long int, std::vector<long long int>&)':
Mountains.cpp:40:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |     int mid = tl+tr>>1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 240 ms 41388 KB Output is correct
3 Correct 226 ms 41636 KB Output is correct
4 Correct 230 ms 41408 KB Output is correct
5 Correct 220 ms 41600 KB Output is correct
6 Correct 225 ms 41416 KB Output is correct
7 Correct 239 ms 41588 KB Output is correct
8 Correct 229 ms 41408 KB Output is correct
9 Correct 177 ms 35664 KB Output is correct
10 Correct 171 ms 35528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 26312 KB Output is correct
2 Correct 132 ms 26312 KB Output is correct
3 Correct 164 ms 26324 KB Output is correct
4 Correct 136 ms 26312 KB Output is correct
5 Correct 135 ms 26324 KB Output is correct
6 Correct 136 ms 26260 KB Output is correct
7 Correct 133 ms 26312 KB Output is correct
8 Correct 124 ms 26324 KB Output is correct
9 Correct 129 ms 26372 KB Output is correct
10 Correct 8 ms 21592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 26312 KB Output is correct
2 Correct 132 ms 26312 KB Output is correct
3 Correct 164 ms 26324 KB Output is correct
4 Correct 136 ms 26312 KB Output is correct
5 Correct 135 ms 26324 KB Output is correct
6 Correct 136 ms 26260 KB Output is correct
7 Correct 133 ms 26312 KB Output is correct
8 Correct 124 ms 26324 KB Output is correct
9 Correct 129 ms 26372 KB Output is correct
10 Correct 8 ms 21592 KB Output is correct
11 Correct 155 ms 26580 KB Output is correct
12 Correct 156 ms 26580 KB Output is correct
13 Correct 169 ms 26580 KB Output is correct
14 Correct 156 ms 26572 KB Output is correct
15 Correct 155 ms 26568 KB Output is correct
16 Correct 159 ms 26576 KB Output is correct
17 Correct 156 ms 26560 KB Output is correct
18 Correct 135 ms 26568 KB Output is correct
19 Correct 136 ms 26572 KB Output is correct
20 Correct 8 ms 21596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 8 ms 21596 KB Output is correct
3 Correct 9 ms 21628 KB Output is correct
4 Correct 8 ms 21516 KB Output is correct
5 Correct 8 ms 21596 KB Output is correct
6 Correct 8 ms 21596 KB Output is correct
7 Correct 8 ms 21596 KB Output is correct
8 Correct 9 ms 21596 KB Output is correct
9 Correct 11 ms 21596 KB Output is correct
10 Correct 9 ms 21476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 8 ms 21596 KB Output is correct
3 Correct 9 ms 21628 KB Output is correct
4 Correct 8 ms 21516 KB Output is correct
5 Correct 8 ms 21596 KB Output is correct
6 Correct 8 ms 21596 KB Output is correct
7 Correct 8 ms 21596 KB Output is correct
8 Correct 9 ms 21596 KB Output is correct
9 Correct 11 ms 21596 KB Output is correct
10 Correct 9 ms 21476 KB Output is correct
11 Correct 19 ms 22108 KB Output is correct
12 Correct 15 ms 22108 KB Output is correct
13 Correct 16 ms 22108 KB Output is correct
14 Correct 16 ms 22112 KB Output is correct
15 Correct 16 ms 22108 KB Output is correct
16 Correct 20 ms 22504 KB Output is correct
17 Correct 15 ms 22108 KB Output is correct
18 Correct 15 ms 21852 KB Output is correct
19 Correct 12 ms 21728 KB Output is correct
20 Correct 8 ms 21596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 26312 KB Output is correct
2 Correct 132 ms 26312 KB Output is correct
3 Correct 164 ms 26324 KB Output is correct
4 Correct 136 ms 26312 KB Output is correct
5 Correct 135 ms 26324 KB Output is correct
6 Correct 136 ms 26260 KB Output is correct
7 Correct 133 ms 26312 KB Output is correct
8 Correct 124 ms 26324 KB Output is correct
9 Correct 129 ms 26372 KB Output is correct
10 Correct 8 ms 21592 KB Output is correct
11 Correct 155 ms 26580 KB Output is correct
12 Correct 156 ms 26580 KB Output is correct
13 Correct 169 ms 26580 KB Output is correct
14 Correct 156 ms 26572 KB Output is correct
15 Correct 155 ms 26568 KB Output is correct
16 Correct 159 ms 26576 KB Output is correct
17 Correct 156 ms 26560 KB Output is correct
18 Correct 135 ms 26568 KB Output is correct
19 Correct 136 ms 26572 KB Output is correct
20 Correct 8 ms 21596 KB Output is correct
21 Correct 265 ms 30160 KB Output is correct
22 Correct 256 ms 30152 KB Output is correct
23 Correct 257 ms 30148 KB Output is correct
24 Correct 262 ms 30144 KB Output is correct
25 Correct 255 ms 30172 KB Output is correct
26 Correct 261 ms 30152 KB Output is correct
27 Correct 259 ms 30148 KB Output is correct
28 Correct 147 ms 30144 KB Output is correct
29 Correct 147 ms 30152 KB Output is correct
30 Correct 8 ms 21596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 240 ms 41388 KB Output is correct
3 Correct 226 ms 41636 KB Output is correct
4 Correct 230 ms 41408 KB Output is correct
5 Correct 220 ms 41600 KB Output is correct
6 Correct 225 ms 41416 KB Output is correct
7 Correct 239 ms 41588 KB Output is correct
8 Correct 229 ms 41408 KB Output is correct
9 Correct 177 ms 35664 KB Output is correct
10 Correct 171 ms 35528 KB Output is correct
11 Correct 130 ms 26312 KB Output is correct
12 Correct 132 ms 26312 KB Output is correct
13 Correct 164 ms 26324 KB Output is correct
14 Correct 136 ms 26312 KB Output is correct
15 Correct 135 ms 26324 KB Output is correct
16 Correct 136 ms 26260 KB Output is correct
17 Correct 133 ms 26312 KB Output is correct
18 Correct 124 ms 26324 KB Output is correct
19 Correct 129 ms 26372 KB Output is correct
20 Correct 8 ms 21592 KB Output is correct
21 Correct 155 ms 26580 KB Output is correct
22 Correct 156 ms 26580 KB Output is correct
23 Correct 169 ms 26580 KB Output is correct
24 Correct 156 ms 26572 KB Output is correct
25 Correct 155 ms 26568 KB Output is correct
26 Correct 159 ms 26576 KB Output is correct
27 Correct 156 ms 26560 KB Output is correct
28 Correct 135 ms 26568 KB Output is correct
29 Correct 136 ms 26572 KB Output is correct
30 Correct 8 ms 21596 KB Output is correct
31 Correct 8 ms 21596 KB Output is correct
32 Correct 8 ms 21596 KB Output is correct
33 Correct 9 ms 21628 KB Output is correct
34 Correct 8 ms 21516 KB Output is correct
35 Correct 8 ms 21596 KB Output is correct
36 Correct 8 ms 21596 KB Output is correct
37 Correct 8 ms 21596 KB Output is correct
38 Correct 9 ms 21596 KB Output is correct
39 Correct 11 ms 21596 KB Output is correct
40 Correct 9 ms 21476 KB Output is correct
41 Correct 19 ms 22108 KB Output is correct
42 Correct 15 ms 22108 KB Output is correct
43 Correct 16 ms 22108 KB Output is correct
44 Correct 16 ms 22112 KB Output is correct
45 Correct 16 ms 22108 KB Output is correct
46 Correct 20 ms 22504 KB Output is correct
47 Correct 15 ms 22108 KB Output is correct
48 Correct 15 ms 21852 KB Output is correct
49 Correct 12 ms 21728 KB Output is correct
50 Correct 8 ms 21596 KB Output is correct
51 Correct 265 ms 30160 KB Output is correct
52 Correct 256 ms 30152 KB Output is correct
53 Correct 257 ms 30148 KB Output is correct
54 Correct 262 ms 30144 KB Output is correct
55 Correct 255 ms 30172 KB Output is correct
56 Correct 261 ms 30152 KB Output is correct
57 Correct 259 ms 30148 KB Output is correct
58 Correct 147 ms 30144 KB Output is correct
59 Correct 147 ms 30152 KB Output is correct
60 Correct 8 ms 21596 KB Output is correct
61 Correct 367 ms 41636 KB Output is correct
62 Correct 352 ms 41404 KB Output is correct
63 Correct 356 ms 41636 KB Output is correct
64 Correct 353 ms 41408 KB Output is correct
65 Correct 357 ms 41668 KB Output is correct
66 Correct 364 ms 41408 KB Output is correct
67 Correct 348 ms 41660 KB Output is correct
68 Correct 286 ms 35508 KB Output is correct
69 Correct 280 ms 31168 KB Output is correct
70 Correct 9 ms 21596 KB Output is correct