Submission #997870

# Submission time Handle Problem Language Result Execution time Memory
997870 2024-06-13T04:02:54 Z irmuun Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
351 ms 58252 KB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
#define pll pair<ll,ll>

struct segtree{
    ll n;
    vector<ll>d;
    segtree(ll n):n(n){
        d.resize(4*n);
        build(1,1,n);
    }
    void build(ll node,ll l,ll r){
        if(l==r){
            d[node]=0;
            return;
        }
        ll mid=(l+r)/2;
        build(node*2,l,mid);
        build(node*2+1,mid+1,r);
        d[node]=max(d[node*2],d[node*2+1]);
    }
    ll query(ll node,ll l,ll r,ll L,ll R){
        if(L>R||r<L||R<l) return 0;
        if(L<=l&&r<=R){
            return d[node];
        }
        ll mid=(l+r)/2;
        return max(query(node*2,l,mid,L,R),query(node*2+1,mid+1,r,L,R));
    }
    void update(ll node,ll l,ll r,ll pos,ll val){
        if(l>pos||r<pos) return;
        if(l==r){
            d[node]=val;
            return;
        }
        ll mid=(l+r)/2;
        update(node*2,l,mid,pos,val);
        update(node*2+1,mid+1,r,pos,val);
        d[node]=max(d[node*2],d[node*2+1]);
    }
};

typedef tree<
pll,
null_type,
less<pll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,k;
    cin>>n>>k;
    ll a[n+5],b[n+5];
    for(ll i=1;i<=n;i++){
        cin>>a[i]>>b[i];
    }
    ll ts[k+5];
    pair<ll,ll>t[k+5];
    for(ll i=1;i<=k;i++){
        cin>>t[i].ff;
        t[i].ss=i;
        ts[i]=t[i].ff;
    }
    sort(t+1,t+k+1);
    ll T[k+5];
    for(ll i=1;i<=k;i++){
        T[i]=t[i].ff;
    }
    segtree sg(k);
    for(ll i=1;i<=k;i++){
        sg.update(1,1,k,i,t[i].ss);
    }
    ordered_set st;
    ll ans=0,last[n+5];
    vector<array<ll,2>>que[k+5];
    for(ll i=1;i<=n;i++){
        ll l,r;
        l=lower_bound(T+1,T+k+1,min(a[i],b[i]))-T;
        r=lower_bound(T+1,T+k+1,max(a[i],b[i]))-T;
        r--;
        last[i]=0;
        if(l<=r){
            last[i]=sg.query(1,1,k,l,r);
        }
        que[last[i]].pb({min(a[i],b[i]),i});
    }
    ll c[n+5];
    map<ll,ll>cnt;
    for(ll i=k;i>=0;i--){
        for(auto [x,j]:que[i]){
            c[j]=st.size()-st.order_of_key({x,0});
        }
        if(i>0){
            st.insert({ts[i],++cnt[ts[i]]});
        }
    }
    for(ll i=1;i<=n;i++){
        ll pos=0;
        if(last[i]>0){
            if(a[i]<b[i]) pos=1;
        }
        pos=(pos+c[i])%2;
        if(pos==0) ans+=a[i];
        else ans+=b[i];
    }
    cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 728 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 752 KB Output is correct
12 Correct 1 ms 528 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 728 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 752 KB Output is correct
12 Correct 1 ms 528 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 12 ms 3160 KB Output is correct
15 Correct 24 ms 6136 KB Output is correct
16 Correct 39 ms 9104 KB Output is correct
17 Correct 56 ms 12012 KB Output is correct
18 Correct 51 ms 11976 KB Output is correct
19 Correct 51 ms 11740 KB Output is correct
20 Correct 52 ms 12112 KB Output is correct
21 Correct 50 ms 11840 KB Output is correct
22 Correct 40 ms 10944 KB Output is correct
23 Correct 40 ms 10576 KB Output is correct
24 Correct 39 ms 10576 KB Output is correct
25 Correct 40 ms 10956 KB Output is correct
26 Correct 43 ms 11604 KB Output is correct
27 Correct 47 ms 12112 KB Output is correct
28 Correct 50 ms 11804 KB Output is correct
29 Correct 49 ms 11896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 728 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 720 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 752 KB Output is correct
12 Correct 1 ms 528 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 12 ms 3160 KB Output is correct
15 Correct 24 ms 6136 KB Output is correct
16 Correct 39 ms 9104 KB Output is correct
17 Correct 56 ms 12012 KB Output is correct
18 Correct 51 ms 11976 KB Output is correct
19 Correct 51 ms 11740 KB Output is correct
20 Correct 52 ms 12112 KB Output is correct
21 Correct 50 ms 11840 KB Output is correct
22 Correct 40 ms 10944 KB Output is correct
23 Correct 40 ms 10576 KB Output is correct
24 Correct 39 ms 10576 KB Output is correct
25 Correct 40 ms 10956 KB Output is correct
26 Correct 43 ms 11604 KB Output is correct
27 Correct 47 ms 12112 KB Output is correct
28 Correct 50 ms 11804 KB Output is correct
29 Correct 49 ms 11896 KB Output is correct
30 Correct 192 ms 45136 KB Output is correct
31 Correct 250 ms 47960 KB Output is correct
32 Correct 236 ms 51252 KB Output is correct
33 Correct 322 ms 58156 KB Output is correct
34 Correct 194 ms 44628 KB Output is correct
35 Correct 329 ms 58136 KB Output is correct
36 Correct 351 ms 58212 KB Output is correct
37 Correct 341 ms 58096 KB Output is correct
38 Correct 343 ms 58052 KB Output is correct
39 Correct 304 ms 58252 KB Output is correct
40 Correct 297 ms 57536 KB Output is correct
41 Correct 329 ms 58036 KB Output is correct
42 Correct 336 ms 58084 KB Output is correct
43 Correct 246 ms 57788 KB Output is correct
44 Correct 253 ms 57024 KB Output is correct
45 Correct 269 ms 57000 KB Output is correct
46 Correct 251 ms 52688 KB Output is correct
47 Correct 240 ms 52048 KB Output is correct
48 Correct 293 ms 58176 KB Output is correct
49 Correct 306 ms 58012 KB Output is correct