답안 #200872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
200872 2020-02-08T11:05:34 Z quocnguyen1012 운세 보기 2 (JOI14_fortune_telling2) C++14
35 / 100
145 ms 16880 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 2e5 + 5;

class segment_tree
{
#define lc id << 1
#define rc id << 1 | 1
  vector<int> ST;
public:
  segment_tree(int n)
  {
    ST.assign(4 * n + 5, 0);
  }
  void update(int id, int l, int r, int pos, int val)
  {
    if (l > pos || r < pos) return;
    if (l == r){
      ST[id] = val;
      return;
    }
    int mid = (l + r) / 2;
    update(lc, l, mid, pos, val); update(rc, mid + 1, r, pos, val);
    ST[id] = max(ST[lc], ST[rc]);
  }
  int query(int id, int l, int r, int L, int R)
  {
    if (l > R || L > r) return 0;
    if (L <= l && r <= R) return ST[id];
    int mid = (l + r) / 2;
    return max(query(lc, l, mid, L, R), query(rc, mid + 1, r, L, R));
  }
};

class fenwick_tree
{
  vector<int> cnt; int n;
public:
  fenwick_tree(int _n)
  {
    n = _n;
    cnt.assign(n + 5, 0);
  }
  void update(int i, int v)
  {
    for (; i; i -= i & -i){
      cnt[i] += v;
    }
  }
  int sum(int i)
  {
    int res = 0;
    for (; i <= n; i += i & -i){
      res += cnt[i];
    }
    return res;
  }
};

int N, M;
int a[maxn], b[maxn], c[maxn];
vector<int> val;
vector<int> query[maxn];

int compress(int x)
{
  return lower_bound(val.begin(), val.end(), x) - val.begin() + 1;
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N >> M;
  for (int i = 1; i <= N; ++i){
    cin >> a[i] >> b[i];
    val.pb(a[i]); val.pb(b[i]);
  }
  for (int i = 1; i <= M; ++i){
    cin >> c[i];
    val.pb(c[i]);
  }
  segment_tree ST(2 * maxn);
  fenwick_tree FT(2 * maxn);
  sort(val.begin(), val.end()); val.erase(unique(val.begin(), val.end()), val.end());
  for (int i = 1; i <= M; ++i){
    c[i] = compress(c[i]);
    ST.update(1, 1, maxn, c[i], i);
  }
  ll res = 0;
  for (int i = 1; i <= N; ++i){
    if (a[i] == b[i]){
      res += a[i];
      continue;
    }
    a[i] = compress(a[i]);
    b[i] = compress(b[i]);
    int t = ST.query(1, 1, maxn, min(a[i], b[i]), max(a[i], b[i]) - 1);
    if (t && a[i] < b[i]) swap(a[i], b[i]);
    query[t].pb(i);
  }
  for (int i = M; i >= 0; --i){
    for (auto id : query[i]){
      int v = FT.sum(max(a[id], b[id]));
      if (v & 1) swap(a[id], b[id]);
      res += val[a[id] - 1];
    }
    if (i) FT.update(c[i], 1);
  }
  cout << res << '\n';
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:82:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12920 KB Output is correct
2 Correct 14 ms 12924 KB Output is correct
3 Correct 14 ms 12920 KB Output is correct
4 Correct 14 ms 12924 KB Output is correct
5 Correct 15 ms 12920 KB Output is correct
6 Correct 14 ms 13048 KB Output is correct
7 Correct 14 ms 12920 KB Output is correct
8 Correct 15 ms 12920 KB Output is correct
9 Correct 14 ms 12920 KB Output is correct
10 Correct 14 ms 12924 KB Output is correct
11 Correct 14 ms 12920 KB Output is correct
12 Correct 13 ms 12920 KB Output is correct
13 Correct 14 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12920 KB Output is correct
2 Correct 14 ms 12924 KB Output is correct
3 Correct 14 ms 12920 KB Output is correct
4 Correct 14 ms 12924 KB Output is correct
5 Correct 15 ms 12920 KB Output is correct
6 Correct 14 ms 13048 KB Output is correct
7 Correct 14 ms 12920 KB Output is correct
8 Correct 15 ms 12920 KB Output is correct
9 Correct 14 ms 12920 KB Output is correct
10 Correct 14 ms 12924 KB Output is correct
11 Correct 14 ms 12920 KB Output is correct
12 Correct 13 ms 12920 KB Output is correct
13 Correct 14 ms 12920 KB Output is correct
14 Correct 28 ms 13560 KB Output is correct
15 Correct 45 ms 14072 KB Output is correct
16 Correct 64 ms 14704 KB Output is correct
17 Correct 86 ms 15216 KB Output is correct
18 Correct 85 ms 15220 KB Output is correct
19 Correct 84 ms 15220 KB Output is correct
20 Correct 81 ms 15220 KB Output is correct
21 Correct 84 ms 15348 KB Output is correct
22 Correct 64 ms 14708 KB Output is correct
23 Correct 66 ms 14836 KB Output is correct
24 Correct 62 ms 14708 KB Output is correct
25 Correct 65 ms 14960 KB Output is correct
26 Correct 66 ms 14964 KB Output is correct
27 Correct 75 ms 15216 KB Output is correct
28 Correct 69 ms 15220 KB Output is correct
29 Correct 76 ms 15348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 12920 KB Output is correct
2 Correct 14 ms 12924 KB Output is correct
3 Correct 14 ms 12920 KB Output is correct
4 Correct 14 ms 12924 KB Output is correct
5 Correct 15 ms 12920 KB Output is correct
6 Correct 14 ms 13048 KB Output is correct
7 Correct 14 ms 12920 KB Output is correct
8 Correct 15 ms 12920 KB Output is correct
9 Correct 14 ms 12920 KB Output is correct
10 Correct 14 ms 12924 KB Output is correct
11 Correct 14 ms 12920 KB Output is correct
12 Correct 13 ms 12920 KB Output is correct
13 Correct 14 ms 12920 KB Output is correct
14 Correct 28 ms 13560 KB Output is correct
15 Correct 45 ms 14072 KB Output is correct
16 Correct 64 ms 14704 KB Output is correct
17 Correct 86 ms 15216 KB Output is correct
18 Correct 85 ms 15220 KB Output is correct
19 Correct 84 ms 15220 KB Output is correct
20 Correct 81 ms 15220 KB Output is correct
21 Correct 84 ms 15348 KB Output is correct
22 Correct 64 ms 14708 KB Output is correct
23 Correct 66 ms 14836 KB Output is correct
24 Correct 62 ms 14708 KB Output is correct
25 Correct 65 ms 14960 KB Output is correct
26 Correct 66 ms 14964 KB Output is correct
27 Correct 75 ms 15216 KB Output is correct
28 Correct 69 ms 15220 KB Output is correct
29 Correct 76 ms 15348 KB Output is correct
30 Incorrect 145 ms 16880 KB Output isn't correct
31 Halted 0 ms 0 KB -