#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "FFILL"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
const int N = 2e5 + 3;
const int LIM = (1 << 16) + 3;
const int INF = 1e9 + 1;
const int LOG = 19;
int n, k, m;
struct fenwick_tree
{
int bit[3 * N];
void update(int u, int v)
{
for(; u; u -= u & -u) bit[u] ^= v;
}
int get(int u)
{
int res = 0;
for(; u <= m; u += u & -u) res ^= bit[u];
return res;
}
} BIT;
int a[N], b[N], p[N];
int col[N];
int st[3 * N][LOG + 1];
int lg[3 * N];
vector<int> zip;
void compress()
{
rep(i, n) zip.pb(a[i]), zip.pb(b[i]);
rep(i, k) zip.pb(p[i]);
sort(all(zip));
zip.resize(unique(all(zip)) - zip.begin());
m = sz(zip);
rep(i, n)
{
a[i] = upper_bound(all(zip), a[i]) - zip.begin();
b[i] = upper_bound(all(zip), b[i]) - zip.begin();
}
rep(i, k) p[i] = upper_bound(all(zip), p[i]) - zip.begin();
}
int get(int l, int r)
{
if(l > r) return 0;
int len = lg[r - l + 1];
return max(st[l][len], st[r - (1 << len) + 1][len]);
}
int last_assign[N];
void find_last_assign()
{
rep(i, k)
maximize(st[p[i]][0], i);
lg[1] = 0;
forn(i, 2, m) lg[i] = lg[i >> 1] + 1;
for(int j = 1; (1 << j) <= m; ++j)
for(int i = 1; i + (1 << j) - 1 <= m; ++i)
st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
rep(i, n) if(last_assign[i] = get(a[i], b[i] - 1))
col[i] = 1;
}
vector<pii> queries[N];
void count_flip()
{
rep(i, n) queries[last_assign[i] + 1].pb({i, b[i]});
ford(i, k, 1)
{
BIT.update(p[i], 1);
for(auto &[idx, u] : queries[i])
col[idx] ^= BIT.get(u);
}
}
void solve()
{
cin >> n >> k;
rep(i, n)
{
cin >> a[i] >> b[i];
if(a[i] > b[i]) col[i] = 1, swap(a[i], b[i]);
else col[i] = 0;
}
rep(i, k) cin >> p[i];
compress();
find_last_assign();
count_flip();
ll res = 0;
rep(i, n) res += zip[(col[i] ? b[i] : a[i]) - 1];
cout << res;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen("note.inp", "r"))
{
freopen("note.inp", "r", stdin);
freopen("note.out", "w", stdout);
}
while(TC--)
{
solve();
}
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'void find_last_assign()':
fortune_telling2.cpp:95:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
95 | st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
| ~~^~~
fortune_telling2.cpp:97:33: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
97 | rep(i, n) if(last_assign[i] = get(a[i], b[i] - 1))
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen("note.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | freopen("note.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
7004 KB |
Output is correct |
3 |
Correct |
3 ms |
5468 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
5 ms |
7000 KB |
Output is correct |
7 |
Correct |
3 ms |
7108 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
6896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
7004 KB |
Output is correct |
3 |
Correct |
3 ms |
5468 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
5 ms |
7000 KB |
Output is correct |
7 |
Correct |
3 ms |
7108 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
6896 KB |
Output is correct |
14 |
Correct |
11 ms |
8540 KB |
Output is correct |
15 |
Correct |
22 ms |
11836 KB |
Output is correct |
16 |
Correct |
33 ms |
14960 KB |
Output is correct |
17 |
Correct |
42 ms |
18320 KB |
Output is correct |
18 |
Correct |
41 ms |
18344 KB |
Output is correct |
19 |
Correct |
40 ms |
19412 KB |
Output is correct |
20 |
Correct |
41 ms |
19204 KB |
Output is correct |
21 |
Correct |
41 ms |
19668 KB |
Output is correct |
22 |
Correct |
31 ms |
15552 KB |
Output is correct |
23 |
Correct |
31 ms |
13860 KB |
Output is correct |
24 |
Correct |
28 ms |
12752 KB |
Output is correct |
25 |
Correct |
32 ms |
16592 KB |
Output is correct |
26 |
Correct |
35 ms |
15060 KB |
Output is correct |
27 |
Correct |
33 ms |
16128 KB |
Output is correct |
28 |
Correct |
32 ms |
16132 KB |
Output is correct |
29 |
Correct |
44 ms |
17660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
3 ms |
7004 KB |
Output is correct |
3 |
Correct |
3 ms |
5468 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
5 ms |
7000 KB |
Output is correct |
7 |
Correct |
3 ms |
7108 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
6896 KB |
Output is correct |
14 |
Correct |
11 ms |
8540 KB |
Output is correct |
15 |
Correct |
22 ms |
11836 KB |
Output is correct |
16 |
Correct |
33 ms |
14960 KB |
Output is correct |
17 |
Correct |
42 ms |
18320 KB |
Output is correct |
18 |
Correct |
41 ms |
18344 KB |
Output is correct |
19 |
Correct |
40 ms |
19412 KB |
Output is correct |
20 |
Correct |
41 ms |
19204 KB |
Output is correct |
21 |
Correct |
41 ms |
19668 KB |
Output is correct |
22 |
Correct |
31 ms |
15552 KB |
Output is correct |
23 |
Correct |
31 ms |
13860 KB |
Output is correct |
24 |
Correct |
28 ms |
12752 KB |
Output is correct |
25 |
Correct |
32 ms |
16592 KB |
Output is correct |
26 |
Correct |
35 ms |
15060 KB |
Output is correct |
27 |
Correct |
33 ms |
16128 KB |
Output is correct |
28 |
Correct |
32 ms |
16132 KB |
Output is correct |
29 |
Correct |
44 ms |
17660 KB |
Output is correct |
30 |
Correct |
77 ms |
28836 KB |
Output is correct |
31 |
Correct |
111 ms |
37788 KB |
Output is correct |
32 |
Correct |
161 ms |
48728 KB |
Output is correct |
33 |
Correct |
254 ms |
70724 KB |
Output is correct |
34 |
Correct |
76 ms |
26836 KB |
Output is correct |
35 |
Correct |
242 ms |
70844 KB |
Output is correct |
36 |
Correct |
257 ms |
70588 KB |
Output is correct |
37 |
Correct |
249 ms |
70588 KB |
Output is correct |
38 |
Correct |
253 ms |
70840 KB |
Output is correct |
39 |
Correct |
252 ms |
70520 KB |
Output is correct |
40 |
Correct |
230 ms |
71980 KB |
Output is correct |
41 |
Correct |
248 ms |
70516 KB |
Output is correct |
42 |
Correct |
249 ms |
70264 KB |
Output is correct |
43 |
Correct |
174 ms |
70824 KB |
Output is correct |
44 |
Correct |
166 ms |
71104 KB |
Output is correct |
45 |
Correct |
169 ms |
71148 KB |
Output is correct |
46 |
Correct |
139 ms |
43460 KB |
Output is correct |
47 |
Correct |
130 ms |
34496 KB |
Output is correct |
48 |
Correct |
181 ms |
53440 KB |
Output is correct |
49 |
Correct |
177 ms |
53696 KB |
Output is correct |