Submission #530588

# Submission time Handle Problem Language Result Execution time Memory
530588 2022-02-25T23:18:08 Z Lobo Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
575 ms 53420 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define maxn 620000

int n, q, tr[4*maxn], trs[4*maxn], a[maxn], b[maxn], t[maxn], ccval[maxn];

void att(int no, int l, int r, int pos, int val) {
    if(l > pos || r < pos) return;
    if(l == r) {
        tr[no] = max(tr[no],val);
        return;
    }
    int f1 = 2*no;
    int f2 = 2*no+1;
    int mid = (l+r)/2;

    att(f1,l,mid,pos,val);
    att(f2,mid+1,r,pos,val);
    tr[no] = max(tr[f1],tr[f2]);
}

int qrr(int no, int l, int r, int L, int R) {
    if(L > R) return 0;
    if(l > R || r < L) return 0;
    if(l >= L && r <= R) return tr[no];
    int f1 = 2*no;
    int f2 = 2*no+1;
    int mid = (l+r)/2;

    return max(qrr(f1,l,mid,L,R),qrr(f2,mid+1,r,L,R));
}

void atts(int no, int l, int r, int pos, int val) {
    if(l > pos || r < pos) return;
    if(l == r) {
        trs[no]+= val;
        return;
    }
    int f1 = 2*no;
    int f2 = 2*no+1;
    int mid = (l+r)/2;

    atts(f1,l,mid,pos,val);
    atts(f2,mid+1,r,pos,val);
    trs[no] = trs[f1]+trs[f2];
}

int qrrs(int no, int l, int r, int L, int R) {
    if(L > R) return 0;
    if(l > R || r < L) return 0;
    if(l >= L && r <= R) return trs[no];
    int f1 = 2*no;
    int f2 = 2*no+1;
    int mid = (l+r)/2;

    return qrrs(f1,l,mid,L,R)+qrrs(f2,mid+1,r,L,R);
}

void solve() {
    cin >> n >> q;
    vector<int> cc;
    for(int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        cc.pb(a[i]);
        cc.pb(b[i]);
    }

    for(int i = 1; i <= q; i++) {
        cin >> t[i];
        cc.pb(t[i]);
    }

    sort(all(cc));
    cc.erase(unique(all(cc)),cc.end());
    int m = cc.size();

    vector<pair<pair<int,int>,pair<int,int>>> qr;
    for(int i = 1; i <= n; i++) {
        int aux;
        aux = a[i];
        a[i] = upper_bound(all(cc),a[i]) - cc.begin();
        ccval[a[i]] = aux;

        aux = b[i];
        b[i] = upper_bound(all(cc),b[i]) - cc.begin();
        ccval[b[i]] = aux;
        qr.pb(mp(mp(max(a[i],b[i]),0),mp(a[i],b[i])));
        // cout << a[i] << " " << b[i] << endl;
    }
    for(int i = 1; i <= q; i++) {
        t[i] = upper_bound(all(cc),t[i]) - cc.begin();
        att(1,1,m,t[i],i);
        qr.pb(mp(mp(t[i],1),mp(i,0)));
        // cout << " att " << t[i] << " " << i << endl;
    }

    sort(all(qr),greater<pair<pair<int,int>,pair<int,int>>>());
    int ans = 0;
    for(auto X : qr) {
        if(X.fr.sc == 0) {
            //query
            //primeiro pega a pos que vai ter que ver ate o final
            int a1 = X.sc.fr;
            int b1 = X.sc.sc;
            // cout << ccval[a1] << " " << ccval[b1] << endl;
            int pos = qrr(1,1,m,min(a1,b1),max(a1,b1)-1);
            // cout << pos << endl;
            
            int qtd = qrrs(1,1,q,pos+1,q);

            if(pos != 0 && b1 > a1) qtd++;

            if(qtd%2 == 0) ans+= ccval[a1];
            else ans+= ccval[b1];
        }
        else {
            //att
            int pos = X.sc.fr;
            //adicionar 1 em pos
            atts(1,1,q,pos,1);
        }
    }

    cout << ans << endl;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    //freopen("out.out", "w", stdout);

    int tt = 1;
    // cin >> tt;
    while(tt--) solve();

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 16 ms 2696 KB Output is correct
15 Correct 36 ms 4940 KB Output is correct
16 Correct 58 ms 6940 KB Output is correct
17 Correct 81 ms 9396 KB Output is correct
18 Correct 101 ms 9428 KB Output is correct
19 Correct 80 ms 9420 KB Output is correct
20 Correct 80 ms 9368 KB Output is correct
21 Correct 75 ms 9240 KB Output is correct
22 Correct 72 ms 9108 KB Output is correct
23 Correct 81 ms 7920 KB Output is correct
24 Correct 77 ms 7796 KB Output is correct
25 Correct 64 ms 9104 KB Output is correct
26 Correct 78 ms 8716 KB Output is correct
27 Correct 99 ms 9132 KB Output is correct
28 Correct 69 ms 8896 KB Output is correct
29 Correct 69 ms 9232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 16 ms 2696 KB Output is correct
15 Correct 36 ms 4940 KB Output is correct
16 Correct 58 ms 6940 KB Output is correct
17 Correct 81 ms 9396 KB Output is correct
18 Correct 101 ms 9428 KB Output is correct
19 Correct 80 ms 9420 KB Output is correct
20 Correct 80 ms 9368 KB Output is correct
21 Correct 75 ms 9240 KB Output is correct
22 Correct 72 ms 9108 KB Output is correct
23 Correct 81 ms 7920 KB Output is correct
24 Correct 77 ms 7796 KB Output is correct
25 Correct 64 ms 9104 KB Output is correct
26 Correct 78 ms 8716 KB Output is correct
27 Correct 99 ms 9132 KB Output is correct
28 Correct 69 ms 8896 KB Output is correct
29 Correct 69 ms 9232 KB Output is correct
30 Correct 268 ms 20352 KB Output is correct
31 Correct 313 ms 27768 KB Output is correct
32 Correct 393 ms 38368 KB Output is correct
33 Correct 501 ms 53416 KB Output is correct
34 Correct 210 ms 19956 KB Output is correct
35 Correct 575 ms 53420 KB Output is correct
36 Correct 528 ms 53304 KB Output is correct
37 Correct 537 ms 53332 KB Output is correct
38 Correct 511 ms 53408 KB Output is correct
39 Correct 477 ms 53384 KB Output is correct
40 Correct 503 ms 51852 KB Output is correct
41 Correct 513 ms 53408 KB Output is correct
42 Correct 483 ms 53408 KB Output is correct
43 Correct 358 ms 41972 KB Output is correct
44 Correct 339 ms 42532 KB Output is correct
45 Correct 350 ms 44020 KB Output is correct
46 Correct 386 ms 40932 KB Output is correct
47 Correct 336 ms 35900 KB Output is correct
48 Correct 369 ms 42936 KB Output is correct
49 Correct 415 ms 43008 KB Output is correct