Submission #728490

#TimeUsernameProblemLanguageResultExecution timeMemory
728490nguyentunglamFortune Telling 2 (JOI14_fortune_telling2)C++17
100 / 100
546 ms59792 KiB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10, M = 6e5;
int a[N], b[N], c[N], T[M * 4 + 10], bit[N], ans[N];
int n, m;
vector<int> rrh, lst[M + 10];
vector<pair<int, int> > query[M + 10];

int id(int x) {
    return upper_bound(rrh.begin(), rrh.end(), x) - rrh.begin();
}

void up(int s, int l, int r, int pos, int val) {
    if (l == r) {
        T[s] = max(T[s], val);
        return;
    }
    int mid = l + r >> 1;
    if (pos <= mid) up(s << 1, l, mid, pos, val);
    else up(s << 1 | 1, mid + 1, r, pos, val);
    T[s] = max(T[s << 1], T[s << 1 | 1]);
}

int get(int s, int l, int r, int from, int to) {
    if (l > to || r < from) return 0;
    if (from <= l && r <= to) return T[s];
    int mid = l + r >> 1;
    return max(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}

void upbit(int pos, int val) {
    while (pos) {
        bit[pos] += val;
        pos -= pos & -pos;
    }
}

int getbit(int pos) {
    int ret = 0;
    while (pos <= m) {
        ret += bit[pos];
        pos += pos & -pos;
    }
    return ret;
}

int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        rrh.push_back(a[i]);
        rrh.push_back(b[i]);
    }
    for(int i = 1; i <= m; i++) {
        cin >> c[i];
        rrh.push_back(c[i]);
    }

    sort(rrh.begin(), rrh.end());
    rrh.resize(unique(rrh.begin(), rrh.end()) - rrh.begin());
    int sz = rrh.size();

    for(int i = 1; i <= m; i++) {
        lst[id(c[i])].push_back(i);
        up(1, 1, sz, id(c[i]), i);
    }

    for(int i = 1; i <= n; i++) {
        int pos = get(1, 1, sz, id(min(a[i], b[i])), id(max(a[i], b[i])) - 1);
        if (pos && a[i] < b[i]) ans[i] = 1;
        query[id(max(a[i], b[i]))].emplace_back(pos + 1, i);
    }

    for(int i = sz; i >= 1; i--) {
        for(int &j : lst[i]) upbit(j, 1);

        for(auto &it : query[i]) {
            int pos, idx; tie(pos, idx) = it;
            ans[idx] += getbit(pos);
        }
    }
    long long res = 0;
    for(int i = 1; i <= n; i++) {
        if (ans[i] % 2) res += b[i];
        else res += a[i];
    }
    cout << res;
}

Compilation message (stderr)

fortune_telling2.cpp: In function 'void up(int, int, int, int, int)':
fortune_telling2.cpp:22:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |     int mid = l + r >> 1;
      |               ~~^~~
fortune_telling2.cpp: In function 'int get(int, int, int, int, int)':
fortune_telling2.cpp:31:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   31 |     int mid = l + r >> 1;
      |               ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:55:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:56:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:59:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:60:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...