Submission #1130171

#TimeUsernameProblemLanguageResultExecution timeMemory
1130171ngokhanhFortune Telling 2 (JOI14_fortune_telling2)C++20
100 / 100
360 ms69452 KiB
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define rep2(i,a,b,c) for (int i=a;i<=b;i+=c)
#define rev(i,a,b) for (int i=a;i>=b;i--)
#define rev2(i,a,b,c) for (int i=a;i>=b;i-=c)
#define pii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define ll long long
#define pll pair<ll,ll>
#define F first
#define S second
#define sz(a) (ll)(a.size())
#define on(n) __builtin_popcountll(n)
#define ld long double
#define __log2(x) 31-__builtin_clz(x)
#define Mask(x) (1LL<<(x))
#define ALL(v) v.begin(),v.end()

const int MAXN=2e5+5;
const int mod=1e9+9;
const int base=113;
const ll INF=1e15;

bool cmp(const pii A,const pii B){
    return max(A.F,A.S) > max(B.F,B.S);
}

bool cmp2(const pii A,const pii B){
    return A.F > B.F;
}

int N,K,T[MAXN],st[3*MAXN][25];
pll P[MAXN],C[MAXN];

int get(int l,int r){
    if (l>r) return -1;
    int k=__log2(r-l+1);
    return max(st[l][k],st[r-Mask(k)+1][k]);
}

struct BIT{

    int Bit[MAXN];

    void Upd(int idx){
        for (int x=idx;x<=K;x+=(x&(-x)))
            Bit[x]++;
    }

    int get(int idx){
        int res=0;
        for (int x=idx;x>0;x-=(x&(-x)))
            res+=Bit[x];
        return res;
    }

    int Get(int l,int r) { return get(r)-get(l-1);};

};

BIT Fentree;

void solution(){
    cin >> N >> K;
    rep(i,1,N) cin >> P[i].F >> P[i].S;
    rep(i,1,K) cin >> T[i];
    rep(i,1,K) C[i]={T[i],i};
    sort(C+1,C+1+K,cmp2);
    sort(P+1,P+1+N,cmp);
    vector<int> v;
    rep(i,1,N) v.pb(P[i].F),v.pb(P[i].S);
    rep(i,1,K) v.pb(T[i]);
    sort(ALL(v));
    v.resize(unique(ALL(v))-v.begin());
    memset(st,-1,sizeof(st));
    rep(i,1,K){
        int posT=lower_bound(ALL(v),T[i])-v.begin()+1;
        st[posT][0]=max(st[posT][0],i);
    }
    rep(j,1,20)
        for (int i=1;i+Mask(j)-1<=sz(v);i++)
            st[i][j]=max(st[i][j-1],st[i+Mask(j-1)][j-1]);
    int idx=0;
    ll res=0;
    rep(i,1,N){
        bool ok=(P[i].F<P[i].S);
        if (ok) swap(P[i].F,P[i].S);
        while(idx+1<=K&&C[idx+1].F>=P[i].F){
            idx++;
            Fentree.Upd(C[idx].S);
        }
        int L=lower_bound(ALL(v),P[i].S)-v.begin()+1;
        int R=lower_bound(ALL(v),P[i].F)-v.begin()+1;
        int pos=get(L,R-1),turn=0;
        if (pos==-1) turn=Fentree.get(K)+ok;
            else if (pos+1<=K) turn=Fentree.Get(pos+1,K);
        if (turn%2==0) res+=P[i].F;
            else res+=P[i].S;
    }
    cout << res;

}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int test=1;
    //cin >> test;
    while(test--)
        solution();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...