#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mxN = (int)2e5+10, LINF = (int)1e18;
vector<int> v, w[mxN];
int n, m, q, mxSz, sum;
unordered_map<int,int> ind;
int a[mxN], b[mxN], t[mxN], orig[3*mxN], segTree[6*mxN];
template<class T> using ordered_set = tree<T,null_type, less<T>,
rb_tree_tag,tree_order_statistics_node_update>;
ordered_set<pair<int,int>> S;
void upd(int i, int v, int p=0, int l=0, int r=mxSz-1){
if(l==r){ segTree[p]=max(segTree[p],v); return; }
int mid = (l+r)/2; int lp = p+1, rp = p+2*(mid-l+1);
if(i<=mid) upd(i,v,lp,l,mid);
else upd(i,v,rp,mid+1,r);
segTree[p] = max(segTree[lp],segTree[rp]);
}
int query(int i, int j, int p=0, int l=0, int r=mxSz-1){
if(i>r or j<l or i>j) return 0;
if(i<=l and r<=j) return segTree[p];
int mid = (l+r)/2; int lp = p+1, rp = p+2*(mid-l+1);
return max(query(i,j,lp,l,mid),query(i,j,rp,mid+1,r));
}
main() {
cin >> n >> q;
for(int i = 1; i <= n; i++)
cin >> a[i] >> b[i], v.pb(a[i]), v.pb(b[i]);
for(int i = 1; i <= q; i++) cin >> t[i], v.pb(t[i]);
sort(all(v)); v.erase(unique(all(v)),v.end());
for(mxSz = 0; auto u : v) ind[u]=mxSz++, orig[mxSz-1]=u;
for(int i = 1; i <= n; i++) a[i]=ind[a[i]],b[i]=ind[b[i]];
for(int i = 1; i <= q; i++) t[i]=ind[t[i]], upd(t[i],i);
for(int i = 1; i <= n; i++)
w[query(min(a[i],b[i]),max(a[i],b[i])-1)].pb(i);
for(int i = q; i>=0; S.insert({t[i],i}), i--){
for(auto j : w[i]){
int x = a[j], y = b[j], tot = q-i;
bool switched = (x<y and i);
int pos = S.order_of_key({max(x,y),-1});
pos = sz(S)-pos, switched^=pos%2;
sum+=orig[switched?y:x];
}
}
cout << sum << "\n";
}
Compilation message
fortune_telling2.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
36 | main() {
| ^~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:42:16: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
42 | for(mxSz = 0; auto u : v) ind[u]=mxSz++, orig[mxSz-1]=u;
| ^~~~
fortune_telling2.cpp:49:28: warning: unused variable 'tot' [-Wunused-variable]
49 | int x = a[j], y = b[j], tot = q-i;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
5 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
6 ms |
5332 KB |
Output is correct |
6 |
Correct |
7 ms |
5332 KB |
Output is correct |
7 |
Correct |
6 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5332 KB |
Output is correct |
9 |
Correct |
5 ms |
5244 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
5 ms |
5204 KB |
Output is correct |
13 |
Correct |
5 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
5 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
6 ms |
5332 KB |
Output is correct |
6 |
Correct |
7 ms |
5332 KB |
Output is correct |
7 |
Correct |
6 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5332 KB |
Output is correct |
9 |
Correct |
5 ms |
5244 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
5 ms |
5204 KB |
Output is correct |
13 |
Correct |
5 ms |
5204 KB |
Output is correct |
14 |
Correct |
28 ms |
8152 KB |
Output is correct |
15 |
Correct |
56 ms |
11328 KB |
Output is correct |
16 |
Correct |
107 ms |
14944 KB |
Output is correct |
17 |
Correct |
139 ms |
17604 KB |
Output is correct |
18 |
Correct |
126 ms |
17644 KB |
Output is correct |
19 |
Correct |
139 ms |
17608 KB |
Output is correct |
20 |
Correct |
122 ms |
17712 KB |
Output is correct |
21 |
Correct |
115 ms |
17644 KB |
Output is correct |
22 |
Correct |
78 ms |
14828 KB |
Output is correct |
23 |
Correct |
81 ms |
13628 KB |
Output is correct |
24 |
Correct |
77 ms |
12844 KB |
Output is correct |
25 |
Correct |
87 ms |
16084 KB |
Output is correct |
26 |
Correct |
95 ms |
14132 KB |
Output is correct |
27 |
Correct |
112 ms |
14844 KB |
Output is correct |
28 |
Correct |
113 ms |
14892 KB |
Output is correct |
29 |
Correct |
135 ms |
16692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
5 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
6 ms |
5332 KB |
Output is correct |
6 |
Correct |
7 ms |
5332 KB |
Output is correct |
7 |
Correct |
6 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5332 KB |
Output is correct |
9 |
Correct |
5 ms |
5244 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
5 ms |
5204 KB |
Output is correct |
13 |
Correct |
5 ms |
5204 KB |
Output is correct |
14 |
Correct |
28 ms |
8152 KB |
Output is correct |
15 |
Correct |
56 ms |
11328 KB |
Output is correct |
16 |
Correct |
107 ms |
14944 KB |
Output is correct |
17 |
Correct |
139 ms |
17604 KB |
Output is correct |
18 |
Correct |
126 ms |
17644 KB |
Output is correct |
19 |
Correct |
139 ms |
17608 KB |
Output is correct |
20 |
Correct |
122 ms |
17712 KB |
Output is correct |
21 |
Correct |
115 ms |
17644 KB |
Output is correct |
22 |
Correct |
78 ms |
14828 KB |
Output is correct |
23 |
Correct |
81 ms |
13628 KB |
Output is correct |
24 |
Correct |
77 ms |
12844 KB |
Output is correct |
25 |
Correct |
87 ms |
16084 KB |
Output is correct |
26 |
Correct |
95 ms |
14132 KB |
Output is correct |
27 |
Correct |
112 ms |
14844 KB |
Output is correct |
28 |
Correct |
113 ms |
14892 KB |
Output is correct |
29 |
Correct |
135 ms |
16692 KB |
Output is correct |
30 |
Correct |
432 ms |
35944 KB |
Output is correct |
31 |
Correct |
478 ms |
41896 KB |
Output is correct |
32 |
Correct |
661 ms |
52104 KB |
Output is correct |
33 |
Correct |
927 ms |
67172 KB |
Output is correct |
34 |
Correct |
350 ms |
36240 KB |
Output is correct |
35 |
Correct |
896 ms |
73052 KB |
Output is correct |
36 |
Correct |
851 ms |
72924 KB |
Output is correct |
37 |
Correct |
912 ms |
73136 KB |
Output is correct |
38 |
Correct |
926 ms |
73036 KB |
Output is correct |
39 |
Correct |
938 ms |
73116 KB |
Output is correct |
40 |
Correct |
894 ms |
72628 KB |
Output is correct |
41 |
Correct |
864 ms |
73052 KB |
Output is correct |
42 |
Correct |
925 ms |
72992 KB |
Output is correct |
43 |
Correct |
649 ms |
66936 KB |
Output is correct |
44 |
Correct |
670 ms |
67364 KB |
Output is correct |
45 |
Correct |
690 ms |
68740 KB |
Output is correct |
46 |
Correct |
520 ms |
51764 KB |
Output is correct |
47 |
Correct |
467 ms |
46436 KB |
Output is correct |
48 |
Correct |
778 ms |
62320 KB |
Output is correct |
49 |
Correct |
745 ms |
62108 KB |
Output is correct |