Submission #434765

#TimeUsernameProblemLanguageResultExecution timeMemory
434765soroushFortune Telling 2 (JOI14_fortune_telling2)C++17
100 / 100
281 ms14736 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair < int , int > pii; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; #define endl '\n' #define ms(x , y) memset(x , y , sizeof x) #define dokme(x) cout << x , exit(0); #define lc (v << 1) #define rc (lc | 1) #define mid ((l + r) >> 1) #define ff first #define ss second int n , k , t[maxn] , cnt[maxn << 2]; pii a[maxn] , mx[maxn << 2]; void build(int v = 1 , int l = 1 , int r = k + 1){ if(r - l == 1){ cnt[v] = 0, mx[v] = {t[l] , l}; return; } build(lc , l , mid); build(rc , mid , r); cnt[v] = cnt[lc] + cnt[rc]; mx[v] = max(mx[lc] , mx[rc]); } void shift(int pos , int v = 1 , int l = 1 , int r = k + 1){ if(r - l == 1){ cnt[v] = 1 , mx[v] = {-1 , l}; return; } if(pos < mid) shift(pos , lc , l , mid); else shift(pos , rc , mid , r); cnt[v] = cnt[lc] + cnt[rc]; mx[v] = max(mx[lc] , mx[rc]); } int get(int L , int R , int v = 1 , int l = 1 , int r = k + 1){ if(r <= L or R <= l)return 0; if(L <= l and r <= R)return cnt[v]; return get(L , R , lc , l , mid) + get(L , R , rc , mid , r); } int dfs(int x , int v = 1 , int l = 1 , int r = k + 1){ if(r - l == 1) return l; if(mx[rc].ff >= x)return dfs(x , rc , mid, r); return dfs(x , lc , l , mid); } int32_t main(){ ios::sync_with_stdio(0), cin.tie(0); cin >> n >> k; for(int i = 1 ; i <= n ; i ++) cin >> a[i].ff >> a[i].ss; sort(a + 1 , a + 1 + n , [](pii a , pii b){return max(a.ff , a.ss) > max(b.ff , b.ss);}); for(int i = 1 ; i <= k ; i ++) cin >> t[i]; build(); ll ans = 0; for(int i = 1 ; i <= n ; i ++){ bool swp = a[i].ff < a[i].ss; if(swp)swap(a[i].ff , a[i].ss); while(mx[1].ff >= a[i].ff) shift(mx[1].ss); if(mx[1].ff >= a[i].ss) ans += ((get(dfs(a[i].ss) , k + 1) % 2) ? a[i].ss : a[i].ff); else ans += ((swp) ? ((cnt[1]%2) ? a[i].ff : a[i].ss) : (((cnt[1]%2) ? a[i].ss : a[i].ff))); } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...