제출 #1347666

#제출 시각아이디문제언어결과실행 시간메모리
1347666niepamietamhasla운세 보기 2 (JOI14_fortune_telling2)C++20
100 / 100
112 ms20336 KiB
#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;
typedef long long ll;
typedef long double ld;
#define pii pair<ll,ll>
#define vii vector<pair<ll,ll>>
#define vi vector<ll>
#define pll pair<ll, ll>
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define count_bits(x) __builtin_popcountll((x))
const ll M = 1e9+7;
const ll INF = 1e9;
//mt19937 mt;void random_start(){mt.seed(chrono::time_poll_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


const ll base = 1 << 18;

ll mx[base << 1];
ll suma[base << 1];

void wgore(ll w){
    mx[w] = max(mx[2*w], mx[2*w+1]);
    suma[w] = suma[2*w]+suma[2*w+1];
    return;
}

void akt(ll w){
    w += base - 1;
    mx[w] = -1;
    suma[w] = 1;
    w /= 2;
    while(w != 0){
        wgore(w);
        w /= 2;
    }
    return;
}

ll ans = 0;

const ll MAXN = 2e5 + 5;

pii X[MAXN];

struct d{
    ll vl;
    ll ind;
    ll typ;
};

vector<d> sweep;

bool comp(const d &d1, const d &d2){
    if(d1.vl != d2.vl){
        return d1.vl > d2.vl;
    }
    if(d1.typ != d2.typ){
        return d1.typ > d2.typ;
    }
    return false;
}

ll oblsume(ll w, ll a, ll b, ll akt_a, ll akt_b){
    if(a > b) return 0;
    if(a <= akt_a and akt_b <= b){
        
        return suma[w];
    }
    if(akt_a > b or akt_b < a) return 0;
    ll mid = (akt_a + akt_b) / 2;
    return oblsume(2 * w, a, b, akt_a, mid) + oblsume(2 * w + 1, a, b, mid + 1, akt_b);
}

ll znajdz(ll w, ll lb, ll ub, ll akt_a, ll akt_b){
    if(akt_a == akt_b){
        if(mx[w] >= lb and mx[w] < ub) return akt_a;
        else return 0;
    }
    ll mid = (akt_a + akt_b) / 2;
    ll vl = mx[2*w+1];
    if(vl >= lb and vl < ub){
        return znajdz(2 * w + 1, lb, ub, mid + 1, akt_b);
    }
    else{
        return znajdz(2 * w, lb, ub, akt_a, mid);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n, k;
    cin >> n >> k;
    ll a, b;
    for(ll i = 1; i <= n; ++i){
        cin >> a >> b;
        X[i] = {a, b};
        sweep.push_back({max(a, b), i, 0});
    }
    for(ll i = 1; i <= k; ++i){
        cin >> a;
        mx[i + base - 1] = a;
        sweep.push_back({a, i, 1});
    }
    for(ll i = base - 1; i > 0; --i){
        wgore(i);
    }
    sort(all(sweep), comp);
    for(auto u : sweep){
        if(u.typ == 1){
            akt(u.ind);
        }
        else{
            ll C = znajdz(1, min(X[u.ind].first, X[u.ind].second), max(X[u.ind].first, X[u.ind].second), 1, base);
            
            if(C == 0){
                ll P = oblsume(1, 1, k, 1, base);
                if(P % 2 == 0){
                    ans += X[u.ind].first;
                }
                else{
                    ans += X[u.ind].second;
                }
            }
            else{
                
                ll P = oblsume(1, C+1, k, 1, base);
                
                if(P % 2 == 0){
                    ans += max(X[u.ind].first, X[u.ind].second);
                }
                else{
                    ans += min(X[u.ind].first, X[u.ind].second);
                }
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...