답안 #345624

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
345624 2021-01-07T16:49:39 Z Matteo_Verz Mountains (NOI20_mountains) C++17
100 / 100
216 ms 15468 KB
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <random>
#include <deque>
#include <set>
#include <map>
#include <cmath>
#define debug(x) cerr << #x << " " << x << '\n'
#define debugsp(x) cerr << #x << " " << x << ' '

using namespace std;

const int INF = 2e9;
const int N = 3e5;

class Fenwick {
  private:
    vector <int> a;

    int lsb(int i) {
      return (i & (-i));
    }

  public:
    Fenwick(int n) {
      a.resize(1 + n);
    }

    void Update(int pos, int value) {
      for(int i = pos; i < a.size(); i += lsb(i))
        a[i] += value;
    }

    int Query(int pos) {
      int ans(0);
      for(int i = pos; i > 0; i -= lsb(i))
        ans += a[i];

      return ans;
    }
};

struct ura {
  int idx;
  long long init, val;
};
ura v[1 + N];

bool SortVal(ura a, ura b) {
  return a.init < b.init;
}

bool SortIdx(ura a, ura b) {
  return a.idx < b.idx;
}

void Normalize(int n) {
  int last(1);
  sort(v + 1, v + n + 1, SortVal);
  v[1].val = last;

  for(int i = 2; i <= n; i++) {
    if(v[i].init != v[i - 1].init) ++last;
    v[i].val = last;
  }

  sort(v + 1, v + n + 1, SortIdx);
}

int main() {
  int n;
  long long ans(0);
  scanf("%d", &n);

  for(int i = 1; i <= n; i++) {
    scanf("%lld", &v[i].init);
    v[i].idx = i;
  }

  Normalize(n);
  
  Fenwick aibl(n), aibr(n);
  for(int i = n; i >= 1; i--)
    aibr.Update(v[i].val, 1);

  for(int i = 1; i <= n; i++) {
    aibr.Update(v[i].val, -1);
    aibl.Update(v[i].val, 1);

    ans += 1LL * aibr.Query(v[i].val - 1) * aibl.Query(v[i].val - 1);
  }

  printf("%lld\n", ans);
  return 0;
}

Compilation message

Mountains.cpp: In member function 'void Fenwick::Update(int, int)':
Mountains.cpp:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |       for(int i = pos; i < a.size(); i += lsb(i))
      |                        ~~^~~~~~~~~~
Mountains.cpp: In function 'int main()':
Mountains.cpp:102:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  102 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
Mountains.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  105 |     scanf("%lld", &v[i].init);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 151 ms 15212 KB Output is correct
3 Correct 151 ms 15212 KB Output is correct
4 Correct 156 ms 15340 KB Output is correct
5 Correct 156 ms 15340 KB Output is correct
6 Correct 154 ms 15232 KB Output is correct
7 Correct 154 ms 15212 KB Output is correct
8 Correct 154 ms 15400 KB Output is correct
9 Correct 153 ms 15468 KB Output is correct
10 Correct 154 ms 15340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 10348 KB Output is correct
2 Correct 166 ms 10348 KB Output is correct
3 Correct 161 ms 10332 KB Output is correct
4 Correct 155 ms 10348 KB Output is correct
5 Correct 156 ms 10348 KB Output is correct
6 Correct 155 ms 10368 KB Output is correct
7 Correct 164 ms 10348 KB Output is correct
8 Correct 167 ms 10348 KB Output is correct
9 Correct 151 ms 10488 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 10348 KB Output is correct
2 Correct 166 ms 10348 KB Output is correct
3 Correct 161 ms 10332 KB Output is correct
4 Correct 155 ms 10348 KB Output is correct
5 Correct 156 ms 10348 KB Output is correct
6 Correct 155 ms 10368 KB Output is correct
7 Correct 164 ms 10348 KB Output is correct
8 Correct 167 ms 10348 KB Output is correct
9 Correct 151 ms 10488 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 171 ms 10596 KB Output is correct
12 Correct 166 ms 10604 KB Output is correct
13 Correct 165 ms 10604 KB Output is correct
14 Correct 171 ms 10604 KB Output is correct
15 Correct 168 ms 10604 KB Output is correct
16 Correct 166 ms 10604 KB Output is correct
17 Correct 175 ms 10604 KB Output is correct
18 Correct 150 ms 10604 KB Output is correct
19 Correct 134 ms 10604 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 6 ms 876 KB Output is correct
12 Correct 6 ms 876 KB Output is correct
13 Correct 6 ms 876 KB Output is correct
14 Correct 6 ms 876 KB Output is correct
15 Correct 7 ms 896 KB Output is correct
16 Correct 6 ms 876 KB Output is correct
17 Correct 6 ms 804 KB Output is correct
18 Correct 6 ms 876 KB Output is correct
19 Correct 5 ms 620 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 10348 KB Output is correct
2 Correct 166 ms 10348 KB Output is correct
3 Correct 161 ms 10332 KB Output is correct
4 Correct 155 ms 10348 KB Output is correct
5 Correct 156 ms 10348 KB Output is correct
6 Correct 155 ms 10368 KB Output is correct
7 Correct 164 ms 10348 KB Output is correct
8 Correct 167 ms 10348 KB Output is correct
9 Correct 151 ms 10488 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 171 ms 10596 KB Output is correct
12 Correct 166 ms 10604 KB Output is correct
13 Correct 165 ms 10604 KB Output is correct
14 Correct 171 ms 10604 KB Output is correct
15 Correct 168 ms 10604 KB Output is correct
16 Correct 166 ms 10604 KB Output is correct
17 Correct 175 ms 10604 KB Output is correct
18 Correct 150 ms 10604 KB Output is correct
19 Correct 134 ms 10604 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 187 ms 11372 KB Output is correct
22 Correct 185 ms 11372 KB Output is correct
23 Correct 185 ms 11372 KB Output is correct
24 Correct 186 ms 11372 KB Output is correct
25 Correct 186 ms 11392 KB Output is correct
26 Correct 189 ms 11372 KB Output is correct
27 Correct 187 ms 11628 KB Output is correct
28 Correct 133 ms 11500 KB Output is correct
29 Correct 134 ms 11372 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 151 ms 15212 KB Output is correct
3 Correct 151 ms 15212 KB Output is correct
4 Correct 156 ms 15340 KB Output is correct
5 Correct 156 ms 15340 KB Output is correct
6 Correct 154 ms 15232 KB Output is correct
7 Correct 154 ms 15212 KB Output is correct
8 Correct 154 ms 15400 KB Output is correct
9 Correct 153 ms 15468 KB Output is correct
10 Correct 154 ms 15340 KB Output is correct
11 Correct 159 ms 10348 KB Output is correct
12 Correct 166 ms 10348 KB Output is correct
13 Correct 161 ms 10332 KB Output is correct
14 Correct 155 ms 10348 KB Output is correct
15 Correct 156 ms 10348 KB Output is correct
16 Correct 155 ms 10368 KB Output is correct
17 Correct 164 ms 10348 KB Output is correct
18 Correct 167 ms 10348 KB Output is correct
19 Correct 151 ms 10488 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 171 ms 10596 KB Output is correct
22 Correct 166 ms 10604 KB Output is correct
23 Correct 165 ms 10604 KB Output is correct
24 Correct 171 ms 10604 KB Output is correct
25 Correct 168 ms 10604 KB Output is correct
26 Correct 166 ms 10604 KB Output is correct
27 Correct 175 ms 10604 KB Output is correct
28 Correct 150 ms 10604 KB Output is correct
29 Correct 134 ms 10604 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 6 ms 876 KB Output is correct
42 Correct 6 ms 876 KB Output is correct
43 Correct 6 ms 876 KB Output is correct
44 Correct 6 ms 876 KB Output is correct
45 Correct 7 ms 896 KB Output is correct
46 Correct 6 ms 876 KB Output is correct
47 Correct 6 ms 804 KB Output is correct
48 Correct 6 ms 876 KB Output is correct
49 Correct 5 ms 620 KB Output is correct
50 Correct 1 ms 364 KB Output is correct
51 Correct 187 ms 11372 KB Output is correct
52 Correct 185 ms 11372 KB Output is correct
53 Correct 185 ms 11372 KB Output is correct
54 Correct 186 ms 11372 KB Output is correct
55 Correct 186 ms 11392 KB Output is correct
56 Correct 189 ms 11372 KB Output is correct
57 Correct 187 ms 11628 KB Output is correct
58 Correct 133 ms 11500 KB Output is correct
59 Correct 134 ms 11372 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 214 ms 15272 KB Output is correct
62 Correct 211 ms 15272 KB Output is correct
63 Correct 215 ms 15340 KB Output is correct
64 Correct 213 ms 15340 KB Output is correct
65 Correct 209 ms 15340 KB Output is correct
66 Correct 212 ms 15340 KB Output is correct
67 Correct 216 ms 15260 KB Output is correct
68 Correct 207 ms 15340 KB Output is correct
69 Correct 189 ms 11628 KB Output is correct
70 Correct 1 ms 364 KB Output is correct