Submission #304654

# Submission time Handle Problem Language Result Execution time Memory
304654 2020-09-21T16:49:40 Z AlexPop28 Fortune Telling 2 (JOI14_fortune_telling2) C++11
100 / 100
394 ms 35424 KB
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

template<class T> void Max(T &a, const T &b) { a = max(a, b); }

struct SegmTree {
  int n;
  vector<int> t;
  SegmTree(int n_) : n(n_), t(2 * n, -1) {}
  
  void Update(int pos, int val) {
    for (Max(t[pos += n], val); pos /= 2; ) {
      t[pos] = max(t[2 * pos], t[2 * pos + 1]);
    }
  }

  int Query(int l, int r) {
    int ans = -1;
    for (l += n, r += n; l < r; l /= 2, r /= 2) {
      if (l & 1) Max(ans, t[l++]);
      if (r & 1) Max(ans, t[--r]);
    }
    return ans;
  }
};

struct Fenwick {
  int n;
  vector<int> t;
  Fenwick(int n_) : n(n_), t(n + 1) {}
  inline int lsb(int x) {
    return x & -x;
  }
  void Update(int pos, int val) {
    for (++pos; pos; pos -= lsb(pos))
      t[pos] += val;
  }
  int Query(int pos) {
    int ans = 0;
    for (++pos; pos <= n; pos += lsb(pos))
      ans += t[pos];
    return ans;
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k; cin >> n >> k;
  vector<int> all;
  vector<vector<int>> v(n, vector<int>(2));
  for (int i = 0; i < n; ++i) {
    int a, b; cin >> a >> b;
    v[i] = {a, b};
    all.emplace_back(a);
    all.emplace_back(b);
  }

  vector<int> qrs(k);
  for (int i = 0; i < k; ++i) {
    int x; cin >> x;
    all.emplace_back(x);
    qrs[i] = x;
  }

  sort(all.begin(), all.end());
  all.erase(unique(all.begin(), all.end()), all.end());

  auto Norm = [&](int x) {
    return lower_bound(all.begin(), all.end(), x) - all.begin();
  };

  SegmTree st(all.size());
  for (int i = 0; i < k; ++i) {
    qrs[i] = Norm(qrs[i]);
    st.Update(qrs[i], i);
  }

  vector<vector<pair<int, int>>> query(k + 1);
  for (int i = 0; i < n; ++i) {
    int l = v[i][0], r = v[i][1];
    if (l > r) swap(l, r);
    l = Norm(l), r = Norm(r);

    int pos = st.Query(l, r) + 1;
    query[pos].emplace_back(i, r);
    if (pos > 0 && v[i][0] < v[i][1]) swap(v[i][0], v[i][1]);

//    dbg() name(i) name(v[i][0]) name(v[i][1]) name(pos) endl;
  }
  
  int64_t ans = 0;
  Fenwick fw(all.size());
  for (int i = k; i >= 0; --i) {
    if (i != k)
      fw.Update(qrs[i], +1);
    for (auto &p : query[i]) {
      int idx, r; tie(idx, r) = p;
      int cnt = fw.Query(r);
      ans += v[idx][cnt % 2];
//      dbg() name(idx) name(cnt) name(v[idx][cnt % 2]) endl;
    }
  }

  cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 13 ms 1792 KB Output is correct
15 Correct 28 ms 3196 KB Output is correct
16 Correct 43 ms 4596 KB Output is correct
17 Correct 62 ms 6000 KB Output is correct
18 Correct 60 ms 6128 KB Output is correct
19 Correct 64 ms 5956 KB Output is correct
20 Correct 59 ms 6028 KB Output is correct
21 Correct 57 ms 5872 KB Output is correct
22 Correct 42 ms 5488 KB Output is correct
23 Correct 42 ms 5360 KB Output is correct
24 Correct 44 ms 5744 KB Output is correct
25 Correct 45 ms 6384 KB Output is correct
26 Correct 45 ms 6000 KB Output is correct
27 Correct 53 ms 6768 KB Output is correct
28 Correct 51 ms 6896 KB Output is correct
29 Correct 57 ms 7024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 13 ms 1792 KB Output is correct
15 Correct 28 ms 3196 KB Output is correct
16 Correct 43 ms 4596 KB Output is correct
17 Correct 62 ms 6000 KB Output is correct
18 Correct 60 ms 6128 KB Output is correct
19 Correct 64 ms 5956 KB Output is correct
20 Correct 59 ms 6028 KB Output is correct
21 Correct 57 ms 5872 KB Output is correct
22 Correct 42 ms 5488 KB Output is correct
23 Correct 42 ms 5360 KB Output is correct
24 Correct 44 ms 5744 KB Output is correct
25 Correct 45 ms 6384 KB Output is correct
26 Correct 45 ms 6000 KB Output is correct
27 Correct 53 ms 6768 KB Output is correct
28 Correct 51 ms 6896 KB Output is correct
29 Correct 57 ms 7024 KB Output is correct
30 Correct 114 ms 12140 KB Output is correct
31 Correct 166 ms 16876 KB Output is correct
32 Correct 233 ms 22632 KB Output is correct
33 Correct 383 ms 34656 KB Output is correct
34 Correct 102 ms 10992 KB Output is correct
35 Correct 387 ms 34648 KB Output is correct
36 Correct 384 ms 34272 KB Output is correct
37 Correct 382 ms 34272 KB Output is correct
38 Correct 387 ms 34400 KB Output is correct
39 Correct 390 ms 34336 KB Output is correct
40 Correct 378 ms 35424 KB Output is correct
41 Correct 389 ms 34144 KB Output is correct
42 Correct 394 ms 34276 KB Output is correct
43 Correct 248 ms 35088 KB Output is correct
44 Correct 241 ms 34916 KB Output is correct
45 Correct 246 ms 34912 KB Output is correct
46 Correct 235 ms 29148 KB Output is correct
47 Correct 227 ms 27364 KB Output is correct
48 Correct 299 ms 31968 KB Output is correct
49 Correct 290 ms 32352 KB Output is correct