Submission #1369558

#TimeUsernameProblemLanguageResultExecution timeMemory
1369558dex111222333444555Fortune Telling 2 (JOI14_fortune_telling2)C++20
100 / 100
216 ms14996 KiB
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) ((int)(v).size())
#define all(v) begin((v)), end(v)
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define dbg(x) "[" #x " = " << x << "]"
#define lli pair<long long, int>
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define FOD(i, a, b) for (int (i) = (b); (i) >= (a); (i)--)
#define REP(i, n) for(int (i) = 0; (i) < n; (i)++)

using namespace std;

bool M1;
const int infINT = 1e9 + 123;
const long long inf = 1e18 + 12;
const int mod = 998244353;

template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}

void add(int &a, const int &b){
    a += b;
    if (a >= mod) a -= mod;
}

int mul(const int &a, const int &b){
    return 1LL * a * 1LL * b % mod;
}

const int dx[4] = {-1, +1, +2, +2};
const int dy[4] = {+2, +2, +1, -1};

const int MAXN = 2e5 + 5;

struct segmentTree{
    int n;
    vector<int > mx;

    segmentTree(int _n = 0): n(_n){
        mx.assign(n << 1 | 1, 0);
    }

    void update(int pos, const int &val){
        pos--;
        pos += n;
        mx[pos] = val;
        while(pos > 1){
            pos >>= 1;
            mx[pos] = max(mx[pos << 1], mx[pos << 1 | 1]);
        }
    }

    int get(int l, int r){
        l--;
        l += n; r += n;
        int best = 0;
        while(l < r){
            if (l & 1) maximize(best, mx[l++]);
            if (r & 1) maximize(best, mx[--r]);
            l >>= 1; r >>= 1;
        }
        return best;
    }
};

struct fenwickTree{
    int n;
    vector<int > bit;

    fenwickTree(int _n = 0): n(_n){
        bit.assign(n + 1, 0);
    }

    void update(int pos, const int &delta){
        for(; pos > 0; pos ^= pos & -pos) bit[pos] += delta;
    }

    int get(int pos){
        int sum = 0;
        for(; pos <= n; pos += pos & -pos) sum += bit[pos];
        return sum;
    }
};

int numVal, numQuery, A[MAXN], B[MAXN], query[MAXN], cnt[MAXN];
bool special[MAXN];
vector<int > comp, can[MAXN * 3];

void input(){
    cin >> numVal >> numQuery;

    FOR(i, 1, numVal){
        cin >> A[i] >> B[i];
        special[i] = 1;
        if (A[i] > B[i]) swap(A[i], B[i]), special[i] = 0;
        comp.push_back(A[i]);
        comp.push_back(B[i] - 1);
    }

    FOR(q, 1, numQuery){
        cin >> query[q];
        comp.push_back(query[q]);
    }
}

int getPos(const int &pos){
    return lower_bound(all(comp), pos) - begin(comp) + 1;
}

void solve(){
    sort(all(comp));
    comp.resize(unique(all(comp)) - begin(comp));

    segmentTree it(siz(comp));

    FOR(q, 1, numQuery){
        query[q] = getPos(query[q]);
        it.update(query[q], q);
    }

    FOR(i, 1, numVal){
        A[i] = getPos(A[i]);
        B[i] = getPos(B[i] - 1);
        can[it.get(A[i], B[i]) + 1].push_back(i);
    }

    fenwickTree bit(siz(comp));
    FOD(q, 1, numQuery){
        bit.update(query[q], +1);
        for(int id: can[q]){
            cnt[id] = bit.get(B[id] + 1);
        }
    }

    long long sum = 0;
    FOR(id, 1, numVal){
        if (it.get(A[id], B[id]) == 0) cnt[id] += special[id];
        if (cnt[id] % 2) sum += comp[A[id] - 1];
        else sum += comp[B[id] - 1] + 1;;
    }
    cout << sum << '\n';
    cerr << sum << '\n';
}

bool M2;
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int t = 1;
//     cin >> t;
    while(t--){
        input();
        solve();
    }
    cerr << (1.0  * clock()) / CLOCKS_PER_SEC << ".s\n";
    cerr << (&M2 - &M1) / 1048576 << " mb\n";
}

Compilation message (stderr)

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