답안 #152181

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
152181 2019-09-06T17:05:31 Z karma 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
585 ms 44360 KB
#include<bits/stdc++.h>
#define pb      emplace_back

using namespace std;

const int N = int(2e5) + 5;
const int M = N * 3;

vector<int> v, c[N];
int n, m, a[N], b[N], t[N], p[M];
int l[M << 2], h[M << 2], st[M << 2];

void Build(int x, int low, int high) {
     l[x] = low, h[x] = high;
     if(l[x] == h[x]) {
        st[x] = p[l[x]];
        return;
     }
     int mid = (low + high) >> 1;
     Build(x << 1, low, mid);
     Build(x << 1 | 1, mid + 1, high);
     st[x] = max(st[x << 1], st[x << 1 | 1]);
}

int Query(int x, int low, int high) {
    if(high < low) return -1;
    if(l[x] > high || h[x] < low) return -1;
    if(l[x] >= low && h[x] <= high) return st[x];
    return max(Query(x << 1, low, high), Query(x << 1 | 1, low, high));
}

int Pos(int x) {return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;}
void Update(int x) {
    for(; x > 0; x -= (x & -x)) ++l[x];
}
int Get(int x) {
    int res = 0;
    for(; x <= int(v.size()); x += (x & -x)) res += l[x];
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    if(fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    cin >> n >> m;
    for(int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        v.pb(a[i]), v.pb(b[i]);
    }
    for(int i = 0; i < m; ++i) cin >> t[i], v.pb(t[i]);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    memset(&p, -1, sizeof p);
    for(int i = 0; i < m; ++i) p[t[i] = Pos(t[i])] = i;
    Build(1, 1, int(v.size()));
    for(int i = 0; i < n; ++i) {
       a[i] = Pos(a[i]), b[i] = Pos(b[i]);
       c[Query(1, min(a[i], b[i]), max(a[i], b[i]) - 1) + 1].pb(i);
    }
    fill(l, l + int(v.size()) + 1, 0);
    long long res = 0;
    for(int i = m; i >= 0; --i) {
       if(i - m) Update(t[i]);
       for(int id: c[i]) {
           h[id] = Get(max(a[id], b[id]));
           if(i == 0 && a[id] < b[id]) ++h[id];
           if(h[id] & 1) res += v[min(a[id], b[id]) - 1];
           else res += v[max(a[id], b[id]) - 1];
       }
    }
    cout << res;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("test.inp", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("test.out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7576 KB Output is correct
3 Correct 10 ms 7544 KB Output is correct
4 Correct 10 ms 7544 KB Output is correct
5 Correct 10 ms 7544 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 10 ms 7548 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 9 ms 7544 KB Output is correct
13 Correct 9 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7576 KB Output is correct
3 Correct 10 ms 7544 KB Output is correct
4 Correct 10 ms 7544 KB Output is correct
5 Correct 10 ms 7544 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 10 ms 7548 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 9 ms 7544 KB Output is correct
13 Correct 9 ms 7544 KB Output is correct
14 Correct 25 ms 8568 KB Output is correct
15 Correct 45 ms 9656 KB Output is correct
16 Correct 65 ms 11504 KB Output is correct
17 Correct 84 ms 11684 KB Output is correct
18 Correct 87 ms 11764 KB Output is correct
19 Correct 85 ms 11636 KB Output is correct
20 Correct 83 ms 11636 KB Output is correct
21 Correct 81 ms 11928 KB Output is correct
22 Correct 58 ms 11812 KB Output is correct
23 Correct 55 ms 10224 KB Output is correct
24 Correct 53 ms 10100 KB Output is correct
25 Correct 58 ms 11892 KB Output is correct
26 Correct 61 ms 11488 KB Output is correct
27 Correct 75 ms 11808 KB Output is correct
28 Correct 64 ms 11760 KB Output is correct
29 Correct 76 ms 11820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7576 KB Output is correct
3 Correct 10 ms 7544 KB Output is correct
4 Correct 10 ms 7544 KB Output is correct
5 Correct 10 ms 7544 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 10 ms 7548 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 9 ms 7516 KB Output is correct
12 Correct 9 ms 7544 KB Output is correct
13 Correct 9 ms 7544 KB Output is correct
14 Correct 25 ms 8568 KB Output is correct
15 Correct 45 ms 9656 KB Output is correct
16 Correct 65 ms 11504 KB Output is correct
17 Correct 84 ms 11684 KB Output is correct
18 Correct 87 ms 11764 KB Output is correct
19 Correct 85 ms 11636 KB Output is correct
20 Correct 83 ms 11636 KB Output is correct
21 Correct 81 ms 11928 KB Output is correct
22 Correct 58 ms 11812 KB Output is correct
23 Correct 55 ms 10224 KB Output is correct
24 Correct 53 ms 10100 KB Output is correct
25 Correct 58 ms 11892 KB Output is correct
26 Correct 61 ms 11488 KB Output is correct
27 Correct 75 ms 11808 KB Output is correct
28 Correct 64 ms 11760 KB Output is correct
29 Correct 76 ms 11820 KB Output is correct
30 Correct 120 ms 15472 KB Output is correct
31 Correct 208 ms 25320 KB Output is correct
32 Correct 306 ms 27384 KB Output is correct
33 Correct 540 ms 43908 KB Output is correct
34 Correct 106 ms 17268 KB Output is correct
35 Correct 562 ms 43940 KB Output is correct
36 Correct 546 ms 43872 KB Output is correct
37 Correct 530 ms 43628 KB Output is correct
38 Correct 547 ms 43876 KB Output is correct
39 Correct 537 ms 43756 KB Output is correct
40 Correct 515 ms 44360 KB Output is correct
41 Correct 545 ms 43616 KB Output is correct
42 Correct 585 ms 43548 KB Output is correct
43 Correct 311 ms 43616 KB Output is correct
44 Correct 310 ms 43760 KB Output is correct
45 Correct 312 ms 43748 KB Output is correct
46 Correct 295 ms 29668 KB Output is correct
47 Correct 259 ms 23140 KB Output is correct
48 Correct 378 ms 31584 KB Output is correct
49 Correct 358 ms 31684 KB Output is correct