Submission #1122837

#TimeUsernameProblemLanguageResultExecution timeMemory
1122837gdragonFortune Telling 2 (JOI14_fortune_telling2)C++20
100 / 100
876 ms10176 KiB
#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 2e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
void add(int &x, int y) {x += y; if (x >= mod) x -= mod;}
void sub(int &x, int y) {x -= y; if (x < 0) x += mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l, long long r) {
    return uniform_int_distribution<long long>(l, r)(rd);
}
// -----------------------------------------//

struct FenwickMax {
    int n;
    vector<int> v;
    FenwickMax(int _n = 0) {
        n = _n;
        v.assign(n + 2, 0);
    }
    void update(int x, int c) {
        for(; x > 0; x -= x & -x) v[x] = max(v[x], c);
    }
    int get(int x) {
        int ans = 0;
        for(; x <= n; x += x & -x) ans = max(ans, v[x]);
        return ans;
    }
} fw1;
struct Fenwick {
    int n;
    vector<int> v;
    Fenwick(int _n = 0) {
        n = _n;
        v.assign(n + 2, 0);
    }
    void update(int x, int c) {
        for(; x <= n; x += x & -x) v[x] += c;
    }
    int get(int x) {
        int ans = 0;
        for(; x > 0; x -= x & -x) ans += v[x];
        return ans;
    }
    int get(int l, int r) {
        return get(r) - get(l - 1);
    }
} fw;
int n, q;
int pos[N];
vector<int> val;
pair<int, int> a[N], queries[N];
void read() {
    cin >> n >> q;
    for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
    for(int i = 1; i <= q; i++) {
        cin >> queries[i].fi;
        queries[i].se = i;
    }
}
void sub1() {
    // sort(queries + 1, queries + q + 1);
    vector<int> on(n + 1, 0);
    for(int iter = 1; iter <= q; iter++) {
        int lim = queries[iter].fi;
        for(int i = 1; i <= n; i++) {
            int x = (on[i] ? a[i].se : a[i].fi);
            if (x <= lim) on[i] ^= 1;
        }
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++) ans += (on[i] ? a[i].se : a[i].fi);
    cout << ans;
}
int getPos(int x) {
    return lower_bound(ALL(val), x) - val.begin() + 1;
}
void solve() {
    // if (max(n, q) <= 1000) {
    //     sub1();
    //     return;
    // }
    for(int i = 1; i <= n; i++) val.push_back(min(a[i].fi, a[i].se));
    for(int i = 1; i <= q; i++) val.push_back(queries[i].fi);
    sort(ALL(val));
    for(int i = 1; i <= n; i++) pos[i] = i;
    sort(pos + 1, pos + n + 1, [&](int x, int y) {
        return max(a[x].fi, a[x].se) < max(a[y].fi, a[y].se);
    });
    sort(queries + 1, queries + q + 1);
    fw = Fenwick(q);
    fw1 = FenwickMax(SZ(val));
    for(int i = 1; i <= q; i++) fw.update(i, 1);
    long long ans = 0;
    for(int i = 1, j = 1; i <= n; i++) {
        int p = pos[i];
        int mx = max(a[p].fi, a[p].se);
        cerr << "i: " << p << endl;
        while(j <= q && queries[j].fi < mx) {
            // cerr << queries[j].fi << ' ' << queries[j].se << endl;
            fw.update(queries[j].se, -1);
            fw1.update(getPos(queries[j].fi), queries[j].se);
            ++j;
        }
        int tmp = fw1.get(getPos(min(a[p].fi, a[p].se)));
        if (tmp == 0) {
            int x = fw.get(q);
            if (x & 1) ans += a[p].se;
            else ans += a[p].fi;
        }
        else {
            int x = fw.get(tmp, q);
            if (x & 1) ans += min(a[p].fi, a[p].se);
            else ans += max(a[p].fi, a[p].se);
        }
        // cerr << "ANS:  " << ans << endl;
    }
    cout << ans;
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    int test = 1;
    // cin >> test;
    while(test--) {
        read();
        solve();
    }
    return 0;
}

Compilation message (stderr)

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...