Submission #937756

#TimeUsernameProblemLanguageResultExecution timeMemory
937756KiaRezFortune Telling 2 (JOI14_fortune_telling2)C++17
100 / 100
339 ms59988 KiB
/* IN THE NAME OF GOD */ #include <bits/stdc++.h> // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef long double ld; #define F first #define S second #define Mp make_pair #define pb push_back #define pf push_front #define size(x) ((ll)x.size()) #define all(x) (x).begin(),(x).end() #define kill(x) cout << x << '\n', exit(0); #define fuck(x) cout << "(" << #x << " , " << x << ")" << endl #define endl '\n' const int N = 1e6+1e5+23, lg = 20; ll Mod = 1e9+7; //998244353; inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;} inline ll poww(ll a, ll b, ll mod=Mod) { ll ans = 1; a=MOD(a, mod); while (b) { if (b & 1) ans = MOD(ans*a, mod); b >>= 1; a = MOD(a*a, mod); } return ans; } int n, k, a[N], b[N], ans[N], seg[2*N], t[N], fen[N]; vector<int> comp, Q[N]; int get(int val) { int ptr = lower_bound(all(comp), val) - comp.begin() + 1; return ptr; } void upd(int ind, int val) { if(ind == 0) return ; if(ind >= (1<<lg)) seg[ind] = val; else seg[ind] = max(seg[2*ind], seg[2*ind+1]); upd(ind/2, val); } int query(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) { if(l>=rc || lc>=r) return 0; if(lc>=l && rc<=r) return seg[ind]; int mid = (lc+rc)/2; return max(query(l, r, 2*ind, lc, mid), query(l, r, 2*ind+1, mid, rc)); } void add(int ind, int val) { while(ind < N) { fen[ind] += val; ind += (ind&(-ind)); } } int qry(int ind) { int res=0; while(ind>0) {res+=fen[ind]; ind-=(ind&(-ind));} return res; } int main () { ios_base::sync_with_stdio(false), cin.tie(0); cin>>n>>k; for(int i=1; i<=n; i++) { cin>>a[i]>>b[i]; comp.pb(a[i]); comp.pb(b[i]); } for(int i=1; i<=k; i++) { cin>>t[i]; comp.pb(t[i]); } comp.pb(1e9+1); sort(all(comp)); comp.resize(unique(all(comp)) - comp.begin()); for(int i=1; i<=k; i++) { upd(get(t[i])+(1<<lg)-1, i); } for(int i=1; i<=n; i++) { int l=get(a[i]), r=get(b[i]); if(l>r) swap(l,r); Q[query(l, r)].pb(i); } for(int i=k; i>=0; i--) { for(auto it : Q[i]) { int f = qry(N-1) - qry(get(max(a[it],b[it]))-1); if(i>0 && a[it]<b[it]) f++; if(f%2==1) ans[it] = b[it]; else ans[it] = a[it]; } add(get(t[i]), 1); } ll answ = 0; for(int i=1; i<=n; i++) answ += ((ll)ans[i]); cout<<answ<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...