//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 200010, SQ = 600;
int a[MAX_N], b[MAX_N], mt[MAX_N], n, m;
struct ASK {
int a, b, c, d;
}qry[MAX_N];
bool operator < (ASK a, ASK b) {
int l1 = a.c, r1 = a.d;
int l2 = b.c, r2 = b.d;
int aid = l1 / SQ, bid = l2 / SQ;
if (aid != bid) return aid < bid;
return (r1 < r2) ^ (aid & 1);
}
pair<int,int> lisan[MAX_N];
struct DS {
int sum[SQ], cnt[MAX_N], all, n;
void add(int i, int d) {
sum[i / SQ] += d;
cnt[i] += d;
all += d;
}
int find_mx() {
for (int i = n;i >= 0;--i) if (cnt[i]) return i;
return -1;
int id = n / SQ;
int RB = id * SQ;
for (int i = n;i >= RB;--i)
if (cnt[i]) return i;
for (int i = id - 1;i >= 0;--i)
if (sum[i]) for (int j = (i * SQ) - 1;;--j)
if (cnt[j]) return j;
return -1;
}
int getsum(int l, int r) {
{
int res = 0;
for (int i = l;i <= r;++i) res += cnt[i];
return res;
}
assert(r == n);
int res = all;
--l;
int lid = l / SQ;
for (int i = 0;i < lid;++i) res -= sum[i];
for (int i = lid * SQ;i <= l;++i) res -= cnt[i];
return res;
}
}RMQ[3];
int sum[MAX_N];
array<int, 3> getnxt() {
int p = RMQ[1].find_mx();
int v = RMQ[2].getsum(p + 1, m) & 1;
DE(p, v);
if (p != -1) {
if (v == 1) return {-1, 1, 1};
else return {-1, 2, 2};
}
if (v == 1) return {-1, 2, 1};
else return {-1, 1, 2};
}
ll solveall() {
sort(qry, qry + n);
ll res = 0;
RMQ[1].n = RMQ[2].n = m;
auto addin = [&](int pos, int d) {
int rp = lisan[pos].second;
if (sum[pos]) RMQ[ sum[pos] ].add(rp, -1);
sum[pos] += d;
if (sum[pos]) RMQ[ sum[pos] ].add(rp, 1);
};
int cl = m, cr = m;
for (int i = 0;i < n;++i) {
auto [a, b, l, r] = qry[i];
while (cl > l) addin(--cl, 1);
while (cr > r) addin(--cr, 1);
while (cr < r) addin(cr++,-1);
while (cl < l) addin(cl++,-1);
auto ret = getnxt();
DE(ret[1], ret[2]);
ll ans ;
if (a <= b) ans = ret[1] == 1 ? min(a, b) : max(a, b);
else ans = ret[2] == 1 ? min(a, b) : max(a, b);
res += ans;
}
return res;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0;i < n;++i)
cin >> a[i] >> b[i];
for (int i = 0;i < m;++i) {
cin >> mt[i];
lisan[i] = {mt[i], i};
}
sort(lisan, lisan + m);
for (int i = 0;i < n;++i) {
int c = a[i], d = b[i];
if (c > d) swap(c, d);
c = lower_bound(lisan, lisan + m, make_pair(c, -1)) - lisan;
d = lower_bound(lisan, lisan + m, make_pair(d, -1)) - lisan;
qry[i] = {a[i], b[i], c, d};
}
cout << solveall() << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'std::array<int, 3> getnxt()':
fortune_telling2.cpp:15:17: warning: statement has no effect [-Wunused-value]
15 | #define DE(...) 0
| ^
fortune_telling2.cpp:74:2: note: in expansion of macro 'DE'
74 | DE(p, v);
| ^~
fortune_telling2.cpp: In function 'll solveall()':
fortune_telling2.cpp:15:17: warning: statement has no effect [-Wunused-value]
15 | #define DE(...) 0
| ^
fortune_telling2.cpp:104:3: note: in expansion of macro 'DE'
104 | DE(ret[1], ret[2]);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
21 ms |
876 KB |
Output is correct |
15 |
Correct |
46 ms |
1260 KB |
Output is correct |
16 |
Correct |
73 ms |
1920 KB |
Output is correct |
17 |
Correct |
103 ms |
2228 KB |
Output is correct |
18 |
Correct |
93 ms |
2284 KB |
Output is correct |
19 |
Correct |
83 ms |
2412 KB |
Output is correct |
20 |
Correct |
85 ms |
2284 KB |
Output is correct |
21 |
Correct |
78 ms |
2284 KB |
Output is correct |
22 |
Correct |
23 ms |
2284 KB |
Output is correct |
23 |
Correct |
24 ms |
2284 KB |
Output is correct |
24 |
Correct |
25 ms |
2284 KB |
Output is correct |
25 |
Correct |
24 ms |
2284 KB |
Output is correct |
26 |
Correct |
682 ms |
2156 KB |
Output is correct |
27 |
Correct |
837 ms |
2412 KB |
Output is correct |
28 |
Correct |
817 ms |
2412 KB |
Output is correct |
29 |
Correct |
1453 ms |
2412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
21 ms |
876 KB |
Output is correct |
15 |
Correct |
46 ms |
1260 KB |
Output is correct |
16 |
Correct |
73 ms |
1920 KB |
Output is correct |
17 |
Correct |
103 ms |
2228 KB |
Output is correct |
18 |
Correct |
93 ms |
2284 KB |
Output is correct |
19 |
Correct |
83 ms |
2412 KB |
Output is correct |
20 |
Correct |
85 ms |
2284 KB |
Output is correct |
21 |
Correct |
78 ms |
2284 KB |
Output is correct |
22 |
Correct |
23 ms |
2284 KB |
Output is correct |
23 |
Correct |
24 ms |
2284 KB |
Output is correct |
24 |
Correct |
25 ms |
2284 KB |
Output is correct |
25 |
Correct |
24 ms |
2284 KB |
Output is correct |
26 |
Correct |
682 ms |
2156 KB |
Output is correct |
27 |
Correct |
837 ms |
2412 KB |
Output is correct |
28 |
Correct |
817 ms |
2412 KB |
Output is correct |
29 |
Correct |
1453 ms |
2412 KB |
Output is correct |
30 |
Correct |
378 ms |
5352 KB |
Output is correct |
31 |
Correct |
474 ms |
6380 KB |
Output is correct |
32 |
Correct |
584 ms |
7532 KB |
Output is correct |
33 |
Correct |
785 ms |
9836 KB |
Output is correct |
34 |
Correct |
45 ms |
5100 KB |
Output is correct |
35 |
Correct |
784 ms |
9816 KB |
Output is correct |
36 |
Correct |
787 ms |
9796 KB |
Output is correct |
37 |
Correct |
779 ms |
9964 KB |
Output is correct |
38 |
Correct |
527 ms |
9836 KB |
Output is correct |
39 |
Correct |
539 ms |
9864 KB |
Output is correct |
40 |
Correct |
422 ms |
9836 KB |
Output is correct |
41 |
Correct |
202 ms |
9836 KB |
Output is correct |
42 |
Correct |
460 ms |
9836 KB |
Output is correct |
43 |
Correct |
112 ms |
9068 KB |
Output is correct |
44 |
Correct |
112 ms |
9196 KB |
Output is correct |
45 |
Correct |
118 ms |
9452 KB |
Output is correct |
46 |
Correct |
130 ms |
9708 KB |
Output is correct |
47 |
Correct |
493 ms |
9708 KB |
Output is correct |
48 |
Execution timed out |
3065 ms |
9708 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |