#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<<19);
int n, q;
vector <pii> v, vec[N];
vector <int> que, saz;
int desaz[2*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);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9196 KB |
Output is correct |
2 |
Correct |
7 ms |
9196 KB |
Output is correct |
3 |
Correct |
7 ms |
9324 KB |
Output is correct |
4 |
Correct |
9 ms |
9324 KB |
Output is correct |
5 |
Correct |
8 ms |
9324 KB |
Output is correct |
6 |
Correct |
8 ms |
9324 KB |
Output is correct |
7 |
Correct |
8 ms |
9324 KB |
Output is correct |
8 |
Correct |
8 ms |
9324 KB |
Output is correct |
9 |
Correct |
8 ms |
9196 KB |
Output is correct |
10 |
Correct |
8 ms |
9196 KB |
Output is correct |
11 |
Correct |
8 ms |
9196 KB |
Output is correct |
12 |
Correct |
9 ms |
9196 KB |
Output is correct |
13 |
Correct |
8 ms |
9324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9196 KB |
Output is correct |
2 |
Correct |
7 ms |
9196 KB |
Output is correct |
3 |
Correct |
7 ms |
9324 KB |
Output is correct |
4 |
Correct |
9 ms |
9324 KB |
Output is correct |
5 |
Correct |
8 ms |
9324 KB |
Output is correct |
6 |
Correct |
8 ms |
9324 KB |
Output is correct |
7 |
Correct |
8 ms |
9324 KB |
Output is correct |
8 |
Correct |
8 ms |
9324 KB |
Output is correct |
9 |
Correct |
8 ms |
9196 KB |
Output is correct |
10 |
Correct |
8 ms |
9196 KB |
Output is correct |
11 |
Correct |
8 ms |
9196 KB |
Output is correct |
12 |
Correct |
9 ms |
9196 KB |
Output is correct |
13 |
Correct |
8 ms |
9324 KB |
Output is correct |
14 |
Correct |
25 ms |
10092 KB |
Output is correct |
15 |
Correct |
47 ms |
10856 KB |
Output is correct |
16 |
Correct |
63 ms |
11620 KB |
Output is correct |
17 |
Correct |
85 ms |
12256 KB |
Output is correct |
18 |
Correct |
81 ms |
12384 KB |
Output is correct |
19 |
Correct |
79 ms |
12512 KB |
Output is correct |
20 |
Correct |
83 ms |
12512 KB |
Output is correct |
21 |
Correct |
80 ms |
12640 KB |
Output is correct |
22 |
Correct |
60 ms |
11872 KB |
Output is correct |
23 |
Correct |
67 ms |
11616 KB |
Output is correct |
24 |
Correct |
61 ms |
11488 KB |
Output is correct |
25 |
Correct |
59 ms |
12256 KB |
Output is correct |
26 |
Correct |
63 ms |
11488 KB |
Output is correct |
27 |
Correct |
77 ms |
12020 KB |
Output is correct |
28 |
Correct |
67 ms |
11872 KB |
Output is correct |
29 |
Correct |
78 ms |
12256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9196 KB |
Output is correct |
2 |
Correct |
7 ms |
9196 KB |
Output is correct |
3 |
Correct |
7 ms |
9324 KB |
Output is correct |
4 |
Correct |
9 ms |
9324 KB |
Output is correct |
5 |
Correct |
8 ms |
9324 KB |
Output is correct |
6 |
Correct |
8 ms |
9324 KB |
Output is correct |
7 |
Correct |
8 ms |
9324 KB |
Output is correct |
8 |
Correct |
8 ms |
9324 KB |
Output is correct |
9 |
Correct |
8 ms |
9196 KB |
Output is correct |
10 |
Correct |
8 ms |
9196 KB |
Output is correct |
11 |
Correct |
8 ms |
9196 KB |
Output is correct |
12 |
Correct |
9 ms |
9196 KB |
Output is correct |
13 |
Correct |
8 ms |
9324 KB |
Output is correct |
14 |
Correct |
25 ms |
10092 KB |
Output is correct |
15 |
Correct |
47 ms |
10856 KB |
Output is correct |
16 |
Correct |
63 ms |
11620 KB |
Output is correct |
17 |
Correct |
85 ms |
12256 KB |
Output is correct |
18 |
Correct |
81 ms |
12384 KB |
Output is correct |
19 |
Correct |
79 ms |
12512 KB |
Output is correct |
20 |
Correct |
83 ms |
12512 KB |
Output is correct |
21 |
Correct |
80 ms |
12640 KB |
Output is correct |
22 |
Correct |
60 ms |
11872 KB |
Output is correct |
23 |
Correct |
67 ms |
11616 KB |
Output is correct |
24 |
Correct |
61 ms |
11488 KB |
Output is correct |
25 |
Correct |
59 ms |
12256 KB |
Output is correct |
26 |
Correct |
63 ms |
11488 KB |
Output is correct |
27 |
Correct |
77 ms |
12020 KB |
Output is correct |
28 |
Correct |
67 ms |
11872 KB |
Output is correct |
29 |
Correct |
78 ms |
12256 KB |
Output is correct |
30 |
Correct |
117 ms |
13912 KB |
Output is correct |
31 |
Correct |
190 ms |
16084 KB |
Output is correct |
32 |
Correct |
285 ms |
19156 KB |
Output is correct |
33 |
Runtime error |
190 ms |
33780 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |