Submission #1273690

#TimeUsernameProblemLanguageResultExecution timeMemory
1273690baotoan655Fortune Telling 2 (JOI14_fortune_telling2)C++20
0 / 100
2 ms568 KiB
#include <bits/stdc++.h>
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;

const int N = 2e5 + 5, L = 18;
int n, q;
pair<int, int> a[N];
bool rev[N];
int Q[N];
int bit[N];
int lst[N], sp[N][L];
int ord[N];
vector<int> ve[N];
int get_max(int l, int r) {
    if(l > r) return 0;
    int k = __lg(r - l + 1);
    return max(sp[l][k], sp[r - (1 << k) + 1][k]);
}
void upd(int i, int val) {
    for(++i; i < N; i += i & -i) bit[i] += val;
}
int get(int i) {
    int res = 0;
    for(++i; i > 0; i -= i & -i) res += bit[i];
    return res;
}
int get(int l, int r) {
    if(l > r) return 0;
    return get(r) - get(l - 1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    file("A") else file("task");
    cin >> n >> q;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i].first >> a[i].second;
        if(a[i].first < a[i].second) {
            swap(a[i].first, a[i].second);
            rev[i] = true;
        }
        ord[i] = i;
    }
    vector<int> zip;
    for(int i = 1; i <= q; ++i) {
        cin >> Q[i];
        zip.emplace_back(Q[i]);
    }
    sort(zip.begin(), zip.end());
    zip.resize(unique(zip.begin(), zip.end()) - zip.begin());
    for(int i = 1; i <= q; ++i) {
        Q[i] = lower_bound(zip.begin(), zip.end(), Q[i]) - zip.begin() + 1;
        lst[Q[i]] = i;
        ve[Q[i]].emplace_back(i);
    }
    for(int i = 1; i <= q; ++i) {
        sp[i][0]= lst[i];
    }
    for(int j = 1; j < L; ++j) {
        for(int i = 1; i + (1 << j) - 1 <= q; ++i) {
            sp[i][j] = max(sp[i][j], sp[i + (1 << j - 1)][j - 1]);
        }
    }
    long long ans = 0;
    sort(ord + 1, ord + n + 1, [&](int i, int j){
        return a[i].first > a[j].first;
    });
    for(int _ = 1; _ <= n; ++_) {
        int i = ord[_];
        if(a[i].first == a[i].second) {
            ans += a[i].first;
            continue;
        }
        int mx = lower_bound(zip.begin(), zip.end(), a[i].first) - zip.begin() + 1;
        int mn = lower_bound(zip.begin(), zip.end(), a[i].second) - zip.begin() + 1;
        for(int x : ve[mx]) upd(x, 1);
        ve[mx].clear();
        int lst = get_max(mn, mx - 1);
        int t2 = get(lst + 1, q) % 2;
        if(rev[i]) t2 ^= 1;
        ans += (t2 ? a[i].second : a[i].first);
    }
    cout << ans << '\n';
    return 0;
}

Compilation message (stderr)

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:36:5: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |     ^~~~
fortune_telling2.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:36:5: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |     ^~~~
fortune_telling2.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:36:20: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |                    ^~~~
fortune_telling2.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:36:20: note: in expansion of macro 'file'
   36 |     file("A") else file("task");
      |                    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...