Submission #554553

# Submission time Handle Problem Language Result Execution time Memory
554553 2022-04-28T17:46:47 Z AA_Surely Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
267 ms 14984 KB
#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cout << "WTF" << endl

#define IOS 		ios::sync_with_stdio(false); cin.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()

using namespace std;
typedef long long 		LL;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;

const int N = 2e5 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;

struct Card {
    int a, b, id;
    bool ch = 0;

    inline bool operator < (const Card other) {
        return a < other.a;
    }
} ns[N];

int n, k;
int stree[N << 2], mtree[N << 2];
PII ks[N];

#define lc now << 1
#define rc now << 1 | 1 

void mchange(int id, int val, int now = 1, int ls = 0, int rs = k - 1) {
    if (ls == rs) {
        mtree[now] = val;
        return;
    }

    int mid = (ls + rs) >> 1;
    if (id <= mid) mchange(id, val, lc, ls, mid);
    else mchange(id, val, rc, mid + 1, rs);
    
    mtree[now] = min(mtree[lc], mtree[rc]);
    return;
}

int descend(int val, int now = 1, int ls = 0, int rs = k - 1) {
    if (mtree[now] >= val) return INF;
    if (ls == rs) return ls;
    int mid = (ls + rs) >> 1;
    int case1 = descend(val, rc, mid + 1, rs);
    return (case1 == INF ? descend(val, lc, ls, mid) : case1);
}

int sbuild(int now = 1, int ls = 0, int rs = k - 1) {
    if (ls == rs) return stree[now] = 1;
    int mid = (ls + rs) >> 1;
    return stree[now] = sbuild(lc, ls, mid) + sbuild(rc, mid + 1, rs);
}

int get(int lq, int rq, int now = 1, int ls = 0, int rs = k - 1) {
    if (rq < lq || rq < ls || rs < lq) return 0;
    if (lq <= ls && rs <= rq) return stree[now];
    int mid = (ls + rs) >> 1;
    return get(lq, rq, lc, ls, mid) + get(lq, rq, rc, mid + 1, rs);
}

void schange(int id, int val, int now = 1, int ls = 0, int rs = k - 1) {
    if (ls == rs) {
        stree[now] = val;
        return;
    }

    int mid = (ls + rs) >> 1;
    if (id <= mid) schange(id, val, lc, ls, mid);
    else schange(id, val, rc, mid + 1, rs);

    stree[now] = stree[lc] + stree[rc];
    return;
}

int main() {
    IOS;
    
    cin >> n >> k;
    F0R(i, n) {
        cin >> ns[i].a >> ns[i].b;
        if (ns[i].a > ns[i].b) {
            swap(ns[i].a, ns[i].b);
            ns[i].ch = 1;
        }
    }

    F0R(i, k) {
        cin >> ks[i].F;
        ks[i].S = i;
    }

    F0R(i, k) mchange(i, ks[i].F);
    sbuild();

    sort(ks, ks + k);
    sort(ns, ns + n);

    /*cout << "descend(9) = " << descend(9) << endl;
    cout << "get(2, 2) = " << get(2, 2) << endl;

    F0R(i, n) {
        cout << ns[i].a << ' ' << ns[i].b << " - " << endl;
    }*/
    
    int ptr = 0;
    LL ans = 0;
    F0R(i, n) {
        while(ptr < k && ks[ptr].F < ns[i].a) {
            mchange(ks[ptr].S, INF);
            schange(ks[ptr].S, 0);
            ptr++;
        }

        int pl = descend(ns[i].b);
        if (pl == INF) {
            int sum = get(0, k - 1);
            if ((sum & 1) ^ (ns[i].ch)) ans += ns[i].b;
            else ans += ns[i].a;
            continue;
        }

        int sum = get(pl + 1, k - 1);
        if (sum & 1) ans += ns[i].a;
        else ans += ns[i].b;

        //cout << "for card " << ns[i].a << ' ' << ns[i].b << " added : " << (sum & 1 ? ns[i].a : ns[i].b) << endl;
    }

    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3412 KB Output is correct
2 Correct 4 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 4 ms 3500 KB Output is correct
6 Correct 3 ms 3472 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 4 ms 3476 KB Output is correct
9 Correct 4 ms 3412 KB Output is correct
10 Correct 3 ms 3412 KB Output is correct
11 Correct 6 ms 3412 KB Output is correct
12 Correct 3 ms 3412 KB Output is correct
13 Correct 3 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3412 KB Output is correct
2 Correct 4 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 4 ms 3500 KB Output is correct
6 Correct 3 ms 3472 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 4 ms 3476 KB Output is correct
9 Correct 4 ms 3412 KB Output is correct
10 Correct 3 ms 3412 KB Output is correct
11 Correct 6 ms 3412 KB Output is correct
12 Correct 3 ms 3412 KB Output is correct
13 Correct 3 ms 3412 KB Output is correct
14 Correct 11 ms 4012 KB Output is correct
15 Correct 21 ms 4692 KB Output is correct
16 Correct 32 ms 5072 KB Output is correct
17 Correct 44 ms 5836 KB Output is correct
18 Correct 43 ms 5948 KB Output is correct
19 Correct 56 ms 5904 KB Output is correct
20 Correct 44 ms 5900 KB Output is correct
21 Correct 30 ms 5824 KB Output is correct
22 Correct 30 ms 5488 KB Output is correct
23 Correct 30 ms 5460 KB Output is correct
24 Correct 36 ms 5480 KB Output is correct
25 Correct 27 ms 5500 KB Output is correct
26 Correct 35 ms 5708 KB Output is correct
27 Correct 40 ms 5940 KB Output is correct
28 Correct 37 ms 5960 KB Output is correct
29 Correct 52 ms 5952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3412 KB Output is correct
2 Correct 4 ms 3412 KB Output is correct
3 Correct 3 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 4 ms 3500 KB Output is correct
6 Correct 3 ms 3472 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 4 ms 3476 KB Output is correct
9 Correct 4 ms 3412 KB Output is correct
10 Correct 3 ms 3412 KB Output is correct
11 Correct 6 ms 3412 KB Output is correct
12 Correct 3 ms 3412 KB Output is correct
13 Correct 3 ms 3412 KB Output is correct
14 Correct 11 ms 4012 KB Output is correct
15 Correct 21 ms 4692 KB Output is correct
16 Correct 32 ms 5072 KB Output is correct
17 Correct 44 ms 5836 KB Output is correct
18 Correct 43 ms 5948 KB Output is correct
19 Correct 56 ms 5904 KB Output is correct
20 Correct 44 ms 5900 KB Output is correct
21 Correct 30 ms 5824 KB Output is correct
22 Correct 30 ms 5488 KB Output is correct
23 Correct 30 ms 5460 KB Output is correct
24 Correct 36 ms 5480 KB Output is correct
25 Correct 27 ms 5500 KB Output is correct
26 Correct 35 ms 5708 KB Output is correct
27 Correct 40 ms 5940 KB Output is correct
28 Correct 37 ms 5960 KB Output is correct
29 Correct 52 ms 5952 KB Output is correct
30 Correct 147 ms 11244 KB Output is correct
31 Correct 186 ms 11984 KB Output is correct
32 Correct 221 ms 12976 KB Output is correct
33 Correct 256 ms 14916 KB Output is correct
34 Correct 67 ms 11048 KB Output is correct
35 Correct 241 ms 14908 KB Output is correct
36 Correct 264 ms 14916 KB Output is correct
37 Correct 248 ms 14948 KB Output is correct
38 Correct 229 ms 14824 KB Output is correct
39 Correct 254 ms 14928 KB Output is correct
40 Correct 157 ms 14644 KB Output is correct
41 Correct 267 ms 14872 KB Output is correct
42 Correct 244 ms 14920 KB Output is correct
43 Correct 127 ms 14320 KB Output is correct
44 Correct 122 ms 14256 KB Output is correct
45 Correct 116 ms 14060 KB Output is correct
46 Correct 187 ms 13036 KB Output is correct
47 Correct 220 ms 12892 KB Output is correct
48 Correct 214 ms 14984 KB Output is correct
49 Correct 167 ms 14924 KB Output is correct