답안 #315986

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
315986 2020-10-24T16:57:33 Z JovanK26 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
1150 ms 103148 KB
#include <cstdio>
#include <algorithm>
#include <vector>

#include <iostream>

using namespace std;

const int MAXN = 200050, MAXK = 200050, MAXV = 2 * MAXN + MAXK;
int N, K, M;
int A[MAXN], B[MAXN], T[MAXK], X[MAXV];
pair<int, int> J[MAXN];
vector<int> S[MAXV * 4 + 50];

void insert(int x, int u, int a, int b, int L, int R) {
  if (a == L && b == R) {
    S[x].push_back(u);
    return;
  }
  int m = (L + R) / 2;
  if (a < m) insert(2 * x + 0, u, a, min(b, m), L, m);
  if (b > m) insert(2 * x + 1, u, max(a, m), b, m, R);
}

void check(int x, int j, int t, int L, int R) {
  for (int i = 0; i < S[x].size(); ++i) {
    if (J[S[x][i]].first < 0) {
      J[S[x][i]].first = j;
    }
  }
  S[x].clear();
  if (R - L == 1) {
    return;
  }
  int m = (L + R) / 2;
  if (t < m) check(2 * x + 0, j, t, L, m);
  else       check(2 * x + 1, j, t, m, R);
  return;
}

int F[MAXV];

void update(int x) {
  while (x <= M) {
    ++F[x];
    x += x & -x;
  }
}

int sum(int x) {
  int s = 0;
  while (x > 0) {
    s += F[x];
    x -= x & -x;
  }
  return s;
}


int main() {
  scanf("%d%d", &N, &K);

  int xs = 0;
  for (int i = 0; i < N; ++i) {
    scanf("%d%d", &A[i], &B[i]);
    X[xs++] = A[i];
    X[xs++] = B[i];
  }
  for (int j = 0; j < K; ++j) {
    scanf("%d", &T[j]);
    X[xs++] = T[j];
  }

  sort(X, X + xs);
  M = unique(X, X + xs) - X;

  for (int i = 0; i < N; ++i) {
    A[i] = lower_bound(X, X + M, A[i]) - X;
    B[i] = lower_bound(X, X + M, B[i]) - X;
    J[i] = make_pair(-1, i);
    insert(1, i, min(A[i], B[i]), max(A[i], B[i]), 0, M);
  }
  for (int j = K - 1; j >= 0; --j) {
    T[j] = lower_bound(X, X + M, T[j]) - X;
    check(1, j, T[j], 0, M);
  }

  sort(J, J + N);
  reverse(J, J + N);

  int cur = K - 1, all = 0;
  long long res = 0;
  for (int i = 0; i < N; ++i) {
    while (cur > J[i].first) {
      update(T[cur--] + 1);
      ++all;
    }

    int u = J[i].second;
    int flip = (all - sum(B[u])) % 2;
    int start = cur >= 0 ? (A[u] < B[u] ? 1 : 0) : 0;
    res += X[(start + flip) % 2 ? B[u] : A[u]];
  }

  printf("%lld\n", res);

  return 0;
}

Compilation message

fortune_telling2.cpp: In function 'void check(int, int, int, int, int)':
fortune_telling2.cpp:26:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |   for (int i = 0; i < S[x].size(); ++i) {
      |                   ~~^~~~~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   61 |   scanf("%d%d", &N, &K);
      |   ~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |     scanf("%d%d", &A[i], &B[i]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |     scanf("%d", &T[j]);
      |     ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 56696 KB Output is correct
2 Correct 40 ms 56824 KB Output is correct
3 Correct 40 ms 56832 KB Output is correct
4 Correct 41 ms 56832 KB Output is correct
5 Correct 40 ms 56824 KB Output is correct
6 Correct 44 ms 56824 KB Output is correct
7 Correct 42 ms 56824 KB Output is correct
8 Correct 40 ms 56824 KB Output is correct
9 Correct 42 ms 56824 KB Output is correct
10 Correct 40 ms 56832 KB Output is correct
11 Correct 41 ms 56832 KB Output is correct
12 Correct 41 ms 56832 KB Output is correct
13 Correct 41 ms 56824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 56696 KB Output is correct
2 Correct 40 ms 56824 KB Output is correct
3 Correct 40 ms 56832 KB Output is correct
4 Correct 41 ms 56832 KB Output is correct
5 Correct 40 ms 56824 KB Output is correct
6 Correct 44 ms 56824 KB Output is correct
7 Correct 42 ms 56824 KB Output is correct
8 Correct 40 ms 56824 KB Output is correct
9 Correct 42 ms 56824 KB Output is correct
10 Correct 40 ms 56832 KB Output is correct
11 Correct 41 ms 56832 KB Output is correct
12 Correct 41 ms 56832 KB Output is correct
13 Correct 41 ms 56824 KB Output is correct
14 Correct 64 ms 58616 KB Output is correct
15 Correct 100 ms 60536 KB Output is correct
16 Correct 139 ms 62456 KB Output is correct
17 Correct 187 ms 64504 KB Output is correct
18 Correct 180 ms 64544 KB Output is correct
19 Correct 187 ms 63992 KB Output is correct
20 Correct 182 ms 64260 KB Output is correct
21 Correct 164 ms 63224 KB Output is correct
22 Correct 125 ms 63400 KB Output is correct
23 Correct 114 ms 62328 KB Output is correct
24 Correct 114 ms 62584 KB Output is correct
25 Correct 117 ms 63184 KB Output is correct
26 Correct 114 ms 60664 KB Output is correct
27 Correct 130 ms 60920 KB Output is correct
28 Correct 127 ms 61944 KB Output is correct
29 Correct 119 ms 59772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 56696 KB Output is correct
2 Correct 40 ms 56824 KB Output is correct
3 Correct 40 ms 56832 KB Output is correct
4 Correct 41 ms 56832 KB Output is correct
5 Correct 40 ms 56824 KB Output is correct
6 Correct 44 ms 56824 KB Output is correct
7 Correct 42 ms 56824 KB Output is correct
8 Correct 40 ms 56824 KB Output is correct
9 Correct 42 ms 56824 KB Output is correct
10 Correct 40 ms 56832 KB Output is correct
11 Correct 41 ms 56832 KB Output is correct
12 Correct 41 ms 56832 KB Output is correct
13 Correct 41 ms 56824 KB Output is correct
14 Correct 64 ms 58616 KB Output is correct
15 Correct 100 ms 60536 KB Output is correct
16 Correct 139 ms 62456 KB Output is correct
17 Correct 187 ms 64504 KB Output is correct
18 Correct 180 ms 64544 KB Output is correct
19 Correct 187 ms 63992 KB Output is correct
20 Correct 182 ms 64260 KB Output is correct
21 Correct 164 ms 63224 KB Output is correct
22 Correct 125 ms 63400 KB Output is correct
23 Correct 114 ms 62328 KB Output is correct
24 Correct 114 ms 62584 KB Output is correct
25 Correct 117 ms 63184 KB Output is correct
26 Correct 114 ms 60664 KB Output is correct
27 Correct 130 ms 60920 KB Output is correct
28 Correct 127 ms 61944 KB Output is correct
29 Correct 119 ms 59772 KB Output is correct
30 Correct 216 ms 61528 KB Output is correct
31 Correct 382 ms 69496 KB Output is correct
32 Correct 626 ms 82680 KB Output is correct
33 Correct 1120 ms 103148 KB Output is correct
34 Correct 178 ms 60408 KB Output is correct
35 Correct 1127 ms 102896 KB Output is correct
36 Correct 1146 ms 102984 KB Output is correct
37 Correct 1150 ms 103064 KB Output is correct
38 Correct 1097 ms 99900 KB Output is correct
39 Correct 1101 ms 101028 KB Output is correct
40 Correct 977 ms 99152 KB Output is correct
41 Correct 1098 ms 99832 KB Output is correct
42 Correct 1106 ms 99580 KB Output is correct
43 Correct 515 ms 98780 KB Output is correct
44 Correct 519 ms 98592 KB Output is correct
45 Correct 519 ms 98444 KB Output is correct
46 Correct 563 ms 95344 KB Output is correct
47 Correct 449 ms 83448 KB Output is correct
48 Correct 681 ms 89592 KB Output is correct
49 Correct 663 ms 90780 KB Output is correct