제출 #1323806

#제출 시각아이디문제언어결과실행 시간메모리
1323806behrad운세 보기 2 (JOI14_fortune_telling2)C++17
0 / 100
7 ms14648 KiB
#ifdef LOCAL
#include "/home/bgopc/template/pch.hpp"
#endif
#include <bits/stdc++.h>
using namespace std;
// * No One Dies a Virgin, Life Fucks Us All
typedef long long ll;
#define nl '\n'
#define ff first
#define ss second
#define pb push_back
#define sik(x) {cout << x << nl; return;}
constexpr ll maxn = 6e5+10, mod = 1e9 + 7, inf = 1e17, SQ = 450, LG = 20;
typedef pair<int, int> pii;

int n, k, a[maxn], b[maxn], res[maxn], fen[maxn], mx[LG][maxn], t[maxn], m;
vector<int> comp, Q[maxn];

inline int GI(int x) { return lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1; }

inline int query(int l, int r) {
  if (l > r) return 0;
  int k = __lg(r - l + 1);
  return max(mx[k][l], mx[k][r - (1 << k) + 1]);
}

inline void add(int p) {
  for (; p < maxn ; p += p & -p) fen[p] ++;
}

inline int get(int p) {
  int res = 0;
  for (; p ; p -= p & -p) res += fen[p];
  return res;
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0); 
  cin >> n >> k;
  for (int i = 1 ; i <= n ; i ++) {
    cin >> a[i] >> b[i];
    if (a[i] > b[i]) {
      swap(a[i], b[i]);
      ++ res[i];
    }
    comp.pb(a[i]);
    comp.pb(b[i]);
  }
  for (int i = 1 ; i <= k ; i ++) {
    cin >> t[i];
    comp.pb(t[i]);
  }
  sort(comp.begin(), comp.end());
  comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
  m = comp.size();

  for (int i = 1 ; i <= k ; i ++) {
    t[i] = GI(t[i]);
    mx[0][t[i]] = i;
  }

  for (int j = 1 ; j < LG ; j ++) {
    for (int i = 1 ; i + (1 << j) - 1 <= m ; i ++) mx[j][i] = max(mx[j - 1][i], mx[j - 1][i + (1 << (j - 1))]);
  }
  

  for (int i = 1 ; i <= n ; i ++) {
    a[i] = GI(a[i]);
    b[i] = GI(b[i]);
    int j = query(a[i], b[i] - 1);
    if (j && !res[i]) ++ res[i];
    Q[j + 1].pb(i);
  }
  for (int i = k + 1 ; i >= 1 ; i --) {
    if (i <= k) add(t[i]);
    for (int j : Q[i]) res[j] += (i - k + 1) - get(b[j] - 1);
  }
  ll sum = 0;
  for (int i = 1 ; i <= n ; i ++) {
    sum += (res[i] % 2 == 1 ? comp[b[i] - 1] : comp[a[i] - 1]);
  }
  cout << sum << nl;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...