#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});
if(i==q) {sum+=orig[max(x,y)]; continue;}
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 |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
6 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
5 ms |
5332 KB |
Output is correct |
6 |
Correct |
5 ms |
5332 KB |
Output is correct |
7 |
Correct |
6 ms |
5368 KB |
Output is correct |
8 |
Correct |
5 ms |
5332 KB |
Output is correct |
9 |
Correct |
4 ms |
5332 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 |
5176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
6 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
5 ms |
5332 KB |
Output is correct |
6 |
Correct |
5 ms |
5332 KB |
Output is correct |
7 |
Correct |
6 ms |
5368 KB |
Output is correct |
8 |
Correct |
5 ms |
5332 KB |
Output is correct |
9 |
Correct |
4 ms |
5332 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 |
5176 KB |
Output is correct |
14 |
Correct |
30 ms |
8104 KB |
Output is correct |
15 |
Correct |
60 ms |
11376 KB |
Output is correct |
16 |
Correct |
91 ms |
14848 KB |
Output is correct |
17 |
Correct |
115 ms |
17672 KB |
Output is correct |
18 |
Correct |
145 ms |
17652 KB |
Output is correct |
19 |
Correct |
162 ms |
17608 KB |
Output is correct |
20 |
Correct |
129 ms |
17804 KB |
Output is correct |
21 |
Correct |
129 ms |
17584 KB |
Output is correct |
22 |
Correct |
84 ms |
14716 KB |
Output is correct |
23 |
Correct |
82 ms |
13616 KB |
Output is correct |
24 |
Correct |
76 ms |
12840 KB |
Output is correct |
25 |
Correct |
84 ms |
15996 KB |
Output is correct |
26 |
Correct |
114 ms |
14224 KB |
Output is correct |
27 |
Correct |
116 ms |
14864 KB |
Output is correct |
28 |
Correct |
119 ms |
14848 KB |
Output is correct |
29 |
Correct |
120 ms |
16696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
6 ms |
5332 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
5 ms |
5332 KB |
Output is correct |
6 |
Correct |
5 ms |
5332 KB |
Output is correct |
7 |
Correct |
6 ms |
5368 KB |
Output is correct |
8 |
Correct |
5 ms |
5332 KB |
Output is correct |
9 |
Correct |
4 ms |
5332 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 |
5176 KB |
Output is correct |
14 |
Correct |
30 ms |
8104 KB |
Output is correct |
15 |
Correct |
60 ms |
11376 KB |
Output is correct |
16 |
Correct |
91 ms |
14848 KB |
Output is correct |
17 |
Correct |
115 ms |
17672 KB |
Output is correct |
18 |
Correct |
145 ms |
17652 KB |
Output is correct |
19 |
Correct |
162 ms |
17608 KB |
Output is correct |
20 |
Correct |
129 ms |
17804 KB |
Output is correct |
21 |
Correct |
129 ms |
17584 KB |
Output is correct |
22 |
Correct |
84 ms |
14716 KB |
Output is correct |
23 |
Correct |
82 ms |
13616 KB |
Output is correct |
24 |
Correct |
76 ms |
12840 KB |
Output is correct |
25 |
Correct |
84 ms |
15996 KB |
Output is correct |
26 |
Correct |
114 ms |
14224 KB |
Output is correct |
27 |
Correct |
116 ms |
14864 KB |
Output is correct |
28 |
Correct |
119 ms |
14848 KB |
Output is correct |
29 |
Correct |
120 ms |
16696 KB |
Output is correct |
30 |
Correct |
427 ms |
35836 KB |
Output is correct |
31 |
Correct |
483 ms |
41884 KB |
Output is correct |
32 |
Correct |
633 ms |
52188 KB |
Output is correct |
33 |
Correct |
898 ms |
67412 KB |
Output is correct |
34 |
Correct |
340 ms |
34276 KB |
Output is correct |
35 |
Correct |
903 ms |
67116 KB |
Output is correct |
36 |
Correct |
967 ms |
67272 KB |
Output is correct |
37 |
Correct |
905 ms |
67352 KB |
Output is correct |
38 |
Correct |
995 ms |
67340 KB |
Output is correct |
39 |
Correct |
979 ms |
67276 KB |
Output is correct |
40 |
Correct |
873 ms |
66788 KB |
Output is correct |
41 |
Correct |
952 ms |
67436 KB |
Output is correct |
42 |
Correct |
1010 ms |
67344 KB |
Output is correct |
43 |
Correct |
739 ms |
61872 KB |
Output is correct |
44 |
Correct |
715 ms |
62256 KB |
Output is correct |
45 |
Correct |
685 ms |
63712 KB |
Output is correct |
46 |
Correct |
541 ms |
47876 KB |
Output is correct |
47 |
Correct |
475 ms |
42688 KB |
Output is correct |
48 |
Correct |
872 ms |
56552 KB |
Output is correct |
49 |
Correct |
716 ms |
56308 KB |
Output is correct |