#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int N=2e5+5;
const int OFF=(1<<20);
int n, q;
vector <pii> v, vec[N];
vector <int> que, saz;
int desaz[3*N], sol[N], Tmax[2*OFF], T[2*OFF], br[N];
#define DEBUG 0
int sazmi(int x) {
int ret=lower_bound(saz.begin(), saz.end(), x)-saz.begin();
desaz[ret]=x;
return ret;
}
int query_max(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return Tmax[pos];
if (lo>=b || hi<=a) return -1;
int mid=(lo+hi)/2;
return max(query_max(a, b, pos*2, lo, mid), query_max(a, b, pos*2+1, mid, hi));
}
int query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return 0;
int mid=(lo+hi)/2;
return query(a, b, pos*2, lo, mid)+query(a, b, pos*2+1, mid, hi);
}
void update(int pos, int x) {
pos+=OFF; T[pos]+=x;
pos/=2;
while (pos) {
T[pos]=T[pos*2]+T[pos*2+1];
pos/=2;
}
}
void build() {
for (int i=OFF-1; i>0; --i) Tmax[i]=max(Tmax[i*2], Tmax[i*2+1]);
}
void solve() {
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
memset(Tmax, -1, sizeof Tmax);
for (int i=0; i<q; ++i) {
que[i]=sazmi(que[i]);
Tmax[OFF+que[i]]=i;
}
build();
for (int i=0; i<n; ++i) {
v[i].X=sazmi(v[i].X); v[i].Y=sazmi(v[i].Y);
int mn=min(v[i].X, v[i].Y), mx=max(v[i].X, v[i].Y);
int pos=query_max(mn, mx);
br[i]=pos;
vec[pos+1].pb(mp(mx, i));
}
for (int i=q-1; i>=0; --i) {
update(que[i], 1);
for (pii pp:vec[i]) sol[pp.Y]=query(pp.X, OFF);
}
#if DEBUG
for (int i=0; i<n; ++i) {
printf("%d %d\n", v[i].X, v[i].Y);
}
for (int x:que) printf("%d\n", x);
#endif // DEBUG
ll res=0;
for (int i=0; i<n; ++i) {
int curr;
if (br[i]==-1) curr=sol[i]%2 ? v[i].Y : v[i].X;
else {
if (v[i].X>v[i].Y) swap(v[i].X, v[i].Y);
curr=sol[i]%2 ? v[i].X : v[i].Y;
}
res+=desaz[curr];
#if DEBUG
printf("i: %d, pos: %d, sol: %d, curr: %d, koji: %d\n", i, br[i], sol[i], curr, desaz[curr]);
#endif
}
printf("%lld\n", res);
}
void load() {
scanf("%d %d", &n, &q);
for (int i=0; i<n; ++i) {
int a, b;
scanf("%d %d", &a, &b);
saz.pb(a); saz.pb(b);
v.pb(mp(a, b));
}
for (int i=0; i<q; ++i) {
int x;
scanf("%d", &x);
que.pb(x); saz.pb(x);
}
}
int main() {
load();
solve();
return 0;
}
/*
1 4
7 8
8
6
5
3
*/
Compilation message
fortune_telling2.cpp: In function 'void load()':
fortune_telling2.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
104 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
107 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
113 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
13292 KB |
Output is correct |
2 |
Correct |
11 ms |
13292 KB |
Output is correct |
3 |
Correct |
14 ms |
13440 KB |
Output is correct |
4 |
Correct |
11 ms |
13428 KB |
Output is correct |
5 |
Correct |
14 ms |
13420 KB |
Output is correct |
6 |
Correct |
11 ms |
13324 KB |
Output is correct |
7 |
Correct |
12 ms |
13420 KB |
Output is correct |
8 |
Correct |
12 ms |
13420 KB |
Output is correct |
9 |
Correct |
12 ms |
13420 KB |
Output is correct |
10 |
Correct |
12 ms |
13420 KB |
Output is correct |
11 |
Correct |
11 ms |
13420 KB |
Output is correct |
12 |
Correct |
10 ms |
13420 KB |
Output is correct |
13 |
Correct |
11 ms |
13420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
13292 KB |
Output is correct |
2 |
Correct |
11 ms |
13292 KB |
Output is correct |
3 |
Correct |
14 ms |
13440 KB |
Output is correct |
4 |
Correct |
11 ms |
13428 KB |
Output is correct |
5 |
Correct |
14 ms |
13420 KB |
Output is correct |
6 |
Correct |
11 ms |
13324 KB |
Output is correct |
7 |
Correct |
12 ms |
13420 KB |
Output is correct |
8 |
Correct |
12 ms |
13420 KB |
Output is correct |
9 |
Correct |
12 ms |
13420 KB |
Output is correct |
10 |
Correct |
12 ms |
13420 KB |
Output is correct |
11 |
Correct |
11 ms |
13420 KB |
Output is correct |
12 |
Correct |
10 ms |
13420 KB |
Output is correct |
13 |
Correct |
11 ms |
13420 KB |
Output is correct |
14 |
Correct |
29 ms |
14148 KB |
Output is correct |
15 |
Correct |
48 ms |
14952 KB |
Output is correct |
16 |
Correct |
114 ms |
15588 KB |
Output is correct |
17 |
Correct |
144 ms |
16352 KB |
Output is correct |
18 |
Correct |
87 ms |
16480 KB |
Output is correct |
19 |
Correct |
92 ms |
16596 KB |
Output is correct |
20 |
Correct |
91 ms |
16484 KB |
Output is correct |
21 |
Correct |
81 ms |
16768 KB |
Output is correct |
22 |
Correct |
87 ms |
15840 KB |
Output is correct |
23 |
Correct |
75 ms |
15712 KB |
Output is correct |
24 |
Correct |
68 ms |
15584 KB |
Output is correct |
25 |
Correct |
95 ms |
16352 KB |
Output is correct |
26 |
Correct |
68 ms |
15584 KB |
Output is correct |
27 |
Correct |
89 ms |
16028 KB |
Output is correct |
28 |
Correct |
73 ms |
16028 KB |
Output is correct |
29 |
Correct |
85 ms |
16272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
13292 KB |
Output is correct |
2 |
Correct |
11 ms |
13292 KB |
Output is correct |
3 |
Correct |
14 ms |
13440 KB |
Output is correct |
4 |
Correct |
11 ms |
13428 KB |
Output is correct |
5 |
Correct |
14 ms |
13420 KB |
Output is correct |
6 |
Correct |
11 ms |
13324 KB |
Output is correct |
7 |
Correct |
12 ms |
13420 KB |
Output is correct |
8 |
Correct |
12 ms |
13420 KB |
Output is correct |
9 |
Correct |
12 ms |
13420 KB |
Output is correct |
10 |
Correct |
12 ms |
13420 KB |
Output is correct |
11 |
Correct |
11 ms |
13420 KB |
Output is correct |
12 |
Correct |
10 ms |
13420 KB |
Output is correct |
13 |
Correct |
11 ms |
13420 KB |
Output is correct |
14 |
Correct |
29 ms |
14148 KB |
Output is correct |
15 |
Correct |
48 ms |
14952 KB |
Output is correct |
16 |
Correct |
114 ms |
15588 KB |
Output is correct |
17 |
Correct |
144 ms |
16352 KB |
Output is correct |
18 |
Correct |
87 ms |
16480 KB |
Output is correct |
19 |
Correct |
92 ms |
16596 KB |
Output is correct |
20 |
Correct |
91 ms |
16484 KB |
Output is correct |
21 |
Correct |
81 ms |
16768 KB |
Output is correct |
22 |
Correct |
87 ms |
15840 KB |
Output is correct |
23 |
Correct |
75 ms |
15712 KB |
Output is correct |
24 |
Correct |
68 ms |
15584 KB |
Output is correct |
25 |
Correct |
95 ms |
16352 KB |
Output is correct |
26 |
Correct |
68 ms |
15584 KB |
Output is correct |
27 |
Correct |
89 ms |
16028 KB |
Output is correct |
28 |
Correct |
73 ms |
16028 KB |
Output is correct |
29 |
Correct |
85 ms |
16272 KB |
Output is correct |
30 |
Correct |
134 ms |
17980 KB |
Output is correct |
31 |
Correct |
201 ms |
20312 KB |
Output is correct |
32 |
Correct |
339 ms |
23328 KB |
Output is correct |
33 |
Correct |
517 ms |
29124 KB |
Output is correct |
34 |
Correct |
100 ms |
17368 KB |
Output is correct |
35 |
Correct |
543 ms |
29072 KB |
Output is correct |
36 |
Correct |
505 ms |
28872 KB |
Output is correct |
37 |
Correct |
548 ms |
28980 KB |
Output is correct |
38 |
Correct |
515 ms |
29256 KB |
Output is correct |
39 |
Correct |
532 ms |
28744 KB |
Output is correct |
40 |
Correct |
461 ms |
29520 KB |
Output is correct |
41 |
Correct |
531 ms |
28944 KB |
Output is correct |
42 |
Correct |
543 ms |
28488 KB |
Output is correct |
43 |
Correct |
302 ms |
27612 KB |
Output is correct |
44 |
Correct |
302 ms |
28032 KB |
Output is correct |
45 |
Correct |
306 ms |
28744 KB |
Output is correct |
46 |
Correct |
419 ms |
25032 KB |
Output is correct |
47 |
Correct |
312 ms |
24008 KB |
Output is correct |
48 |
Correct |
394 ms |
26440 KB |
Output is correct |
49 |
Correct |
386 ms |
26824 KB |
Output is correct |