Submission #988750

# Submission time Handle Problem Language Result Execution time Memory
988750 2024-05-25T22:06:37 Z MamaShakuni Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
184 ms 31384 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 fileio \
	freopen("input.txt", "r", stdin); \
	freopen("output.txt", "w", stdout); 
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ld long double
#define vl vector<long long>
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<long long>>
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vsi vector<set<int>>
#define ull unordered_map<ll, ll>
#define pll pair<long long, long long>
#define pii pair<int,int>
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fo(i,a,n) for(ll i=a; i<=n; i++)
#define fr(i,n,a) for(ll i=n; i>=a; i--)
#define all(v) v.begin(),v.end()
#define max3(a,b,c) max(a, max(b,c))
#define min3(a,b,c) min(a, min(b,c))
#define endl "\n"
 
/*
#define M 998244353
 
ll multiply (ll a, ll b) {
	return ((a*b)%M +M)%M;
}
 
ll power (ll base, ll exp) {
	if (exp==0) return 1;
	ll ans = (exp%2==1) ? base:1;
	ll sa = power(base, exp/2);
	ans = multiply(ans, sa);
	ans = multiply(ans, sa);
	return ans;
}
 
ll divide (ll a, ll b) {
	ll x = power(b, M-2);
	return multiply(a,x);	
}
*/
 
 
struct Elements {
    ll a;
    ll b;
    ll idx;
    ll maxi;
    ll mini;
};
 
struct Query {
    ll val;
    ll idx;
};
 
class SegmentTree {
    
    vl maxiCalculator;
    vl countCalculator;
    vl a;
    int n;
 
 
    void buildTree(vl& sTree, int idx, int start, int end) {
        if (start == end) {
            sTree[idx] = start;
            return;
        }
        int mid = (start+end)/2;
        buildTree(sTree, 2*idx+1, start, mid);
        buildTree(sTree, 2*idx+2, mid+1, end);
        sTree[idx] = (a[sTree[2*idx+2]] >= a[sTree[2*idx+1]]) ? sTree[2*idx+2] : sTree[2*idx+1];
    }
 
    ll queryMaxTreeIdx(vl& sTree, int idx, int start, int end, int maxi) {
        if (a[sTree[idx]] < maxi) return -1;
        if (start == end) return start;
        int mid = (start+end)/2;
        int rightVal = a[sTree[2*idx+2]];
        if (rightVal >= maxi) return queryMaxTreeIdx(sTree, 2*idx+2, mid+1, end, maxi);
        else return queryMaxTreeIdx(sTree, 2*idx+1, start, mid, maxi);
    }
    
    ll queryCountTreeIdx(const vl& sTree, int idx, int start, int end, int qs, int qe) {
        if (qs > end || qe < start) return 0;
        if (start>=qs && end<=qe) return sTree[idx];
        int mid = (start+end)/2;
        ll leftAns = queryCountTreeIdx(sTree, 2*idx+1, start, mid, qs, qe);
        ll rightAns = queryCountTreeIdx(sTree, 2*idx+2, mid+1, end, qs, qe);
        return leftAns+rightAns;
    }
 
    void updateMaxTreeIdx(vl& sTree, int idx, int start, int end, int updateIdx) {
        if (start == end) {
            a[updateIdx] = -1;
            return;
        }
        int mid = (start+end)/2;
        if (updateIdx <= mid) updateMaxTreeIdx(sTree, 2*idx+1, start, mid, updateIdx);
        else updateMaxTreeIdx(sTree, 2*idx+2, mid+1, end, updateIdx);
        sTree[idx] = (a[sTree[2*idx+2]] >= a[sTree[2*idx+1]]) ? sTree[2*idx+2] : sTree[2*idx+1];
    }
 
    void updateCountTreeIdx(vl& sTree, int idx, int start, int end, int updateIdx) {
        sTree[idx]++;    
        if (start == end) return;
        int mid = (start+end)/2;
        if (updateIdx <= mid) updateCountTreeIdx(sTree, 2*idx+1, start, mid, updateIdx);
        else updateCountTreeIdx(sTree, 2*idx+2, mid+1, end, updateIdx);
    }
 
    void displayTree(const vl& sTree, int idx, int start, int end) {
        cout<<"Range "<<start<<" -> "<<end<<" contains "<<sTree[idx]<<endl;
        if (start == end) return;
        int mid = (start+end)/2;
        displayTree(sTree, 2*idx+1, start, mid);
        displayTree(sTree, 2*idx+2, mid+1, end);
    }
 
    void displayTree(const vl& sTree) {
        displayTree(sTree, 0, 0, n-1);
    }
 
    public:
    
    SegmentTree(const vector<Query>& array) {
        n = array.size();
        a.resize(n);
        maxiCalculator.resize(4*n);
        countCalculator.resize(4*n);
        fo (i,0,4*n-1) maxiCalculator[i] = 0;
        fo (i,0,4*n-1) countCalculator[i] = 0;
        fo (i,0,n-1) a[i] = array[i].val;
        buildTree(maxiCalculator, 0, 0, n-1);
        //cout<<"Displaying MaxTree"<<endl; displayTree(maxiCalculator);
    }
 
    ll findRightmostIndex(ll maxi) {
        return queryMaxTreeIdx(maxiCalculator, 0, 0, n-1, maxi);
    }
 
    void update(Query query) {
        //cout<<"Updating : "<<query.idx<<" having "<<query.val<<endl;
        updateMaxTreeIdx(maxiCalculator, 0, 0, n-1, query.idx);
        updateCountTreeIdx(countCalculator, 0, 0, n-1, query.idx);
        //cout<<"Displaying MaxTree"<<endl; displayTree(maxiCalculator);
        //cout<<"Displaying CountTree"<<endl; displayTree(countCalculator);
    }
 
    ll countRotations(ll leftIdx) {
        return queryCountTreeIdx(countCalculator, 0, 0, n-1, leftIdx, n-1);
    }
};
 
 
void solve() {
	
    int n, q; cin>>n>>q;
    vector<Elements> array(n);
    fo (i,0,n-1) {
        ll x, y; cin>>x>>y;
        array[i] = {x,y,i,max(x,y),min(x,y)};
    }
    sort(all(array), [](auto x, auto y) {return x.maxi > y.maxi;});
    
    vector<Query> query(q);
    fo (i,0,q-1) {
        cin>>query[i].val;
        query[i].idx = i;
    }
    SegmentTree fastQueries(query);
    sort(all(query), [](auto x, auto y) {return x.val > y.val;});
    ll eliminatedIdx = 0;
    ll ans = 0;
 
    fo (i,0,n-1) {
        //cout<<"Solving for : "<<array[i].idx<<" having "<<array[i].a<<" and "<<array[i].b<<endl;
        while (eliminatedIdx < q && query[eliminatedIdx].val >= array[i].maxi) {
            fastQueries.update(query[eliminatedIdx]);
            eliminatedIdx++;
        }
        int rightMost = fastQueries.findRightmostIndex(array[i].mini);
        int countRotations = fastQueries.countRotations(rightMost+1);
        //cout<<"Found rightmost index : "<<rightMost<<" and rotations are "<<countRotations<<endl;
        ll shortAns = ((countRotations%2) ? array[i].mini : array[i].maxi);
        if (rightMost == -1) shortAns = ((countRotations%2) ? array[i].b : array[i].a);
        ans += shortAns;
    }
    cout<<ans<<endl;
}
 
int main() {
    fastio;
    int t=1; //cin>>t;
    while(t--) {
        solve();
    }
    return 0;    
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 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 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 524 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 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 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 524 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 7 ms 1880 KB Output is correct
15 Correct 18 ms 3416 KB Output is correct
16 Correct 21 ms 4916 KB Output is correct
17 Correct 28 ms 6504 KB Output is correct
18 Correct 28 ms 6496 KB Output is correct
19 Correct 26 ms 6496 KB Output is correct
20 Correct 28 ms 6500 KB Output is correct
21 Correct 20 ms 6604 KB Output is correct
22 Correct 18 ms 5988 KB Output is correct
23 Correct 20 ms 5980 KB Output is correct
24 Correct 23 ms 5980 KB Output is correct
25 Correct 18 ms 5980 KB Output is correct
26 Correct 25 ms 6236 KB Output is correct
27 Correct 28 ms 6492 KB Output is correct
28 Correct 27 ms 6652 KB Output is correct
29 Correct 28 ms 6496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 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 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 524 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 7 ms 1880 KB Output is correct
15 Correct 18 ms 3416 KB Output is correct
16 Correct 21 ms 4916 KB Output is correct
17 Correct 28 ms 6504 KB Output is correct
18 Correct 28 ms 6496 KB Output is correct
19 Correct 26 ms 6496 KB Output is correct
20 Correct 28 ms 6500 KB Output is correct
21 Correct 20 ms 6604 KB Output is correct
22 Correct 18 ms 5988 KB Output is correct
23 Correct 20 ms 5980 KB Output is correct
24 Correct 23 ms 5980 KB Output is correct
25 Correct 18 ms 5980 KB Output is correct
26 Correct 25 ms 6236 KB Output is correct
27 Correct 28 ms 6492 KB Output is correct
28 Correct 27 ms 6652 KB Output is correct
29 Correct 28 ms 6496 KB Output is correct
30 Correct 96 ms 20304 KB Output is correct
31 Correct 108 ms 22356 KB Output is correct
32 Correct 146 ms 25432 KB Output is correct
33 Correct 163 ms 31312 KB Output is correct
34 Correct 49 ms 19540 KB Output is correct
35 Correct 159 ms 31316 KB Output is correct
36 Correct 170 ms 31312 KB Output is correct
37 Correct 184 ms 31316 KB Output is correct
38 Correct 151 ms 31328 KB Output is correct
39 Correct 161 ms 31312 KB Output is correct
40 Correct 101 ms 31052 KB Output is correct
41 Correct 181 ms 31332 KB Output is correct
42 Correct 158 ms 31316 KB Output is correct
43 Correct 74 ms 30548 KB Output is correct
44 Correct 83 ms 30548 KB Output is correct
45 Correct 76 ms 30548 KB Output is correct
46 Correct 129 ms 29332 KB Output is correct
47 Correct 137 ms 29292 KB Output is correct
48 Correct 162 ms 31384 KB Output is correct
49 Correct 157 ms 31380 KB Output is correct