#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 9;
const ll Log2 = 20;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;
struct data{
ll x,y,id,dummy;
};
data a[N];
ll n,k,st[4*N],q[N];
bool lf(data a,data b){
return max(a.x,a.y) > max(b.x,b.y);
}
void upd(ll id,ll l,ll r,ll u,ll val){
if (u < l||r < u) return;
if (l == r){
st[id] = val; return;
}
ll mid = (l + r)/2;
upd(id*2,l,mid,u,val); upd(id*2 + 1,mid + 1,r,u,val);
st[id] = max(st[id*2],st[id*2 + 1]);
}
/// last but >= val
ll WalkLast(ll id,ll l,ll r,ll val){
if (l == r){
if (st[id] >= val) return l;
return 0;
}
ll mid = (l + r)/2;
if (st[id*2 + 1] >= val) return WalkLast(id*2 + 1,mid + 1,r,val);
return WalkLast(id*2,l,mid,val);
}
ll WalkFirst(ll id,ll l,ll r,ll val){
if (l == r){
if (st[id] >= val) return l;
return inf;
}
ll mid = (l + r)/2;
if (st[id*2] >= val) return WalkFirst(id*2,l,mid,val);
return WalkFirst(id*2 + 1,mid + 1,r,val);
}
ll bit[N];
void updBIT(ll p,ll val){
for (ll i = p;i <= k;i += i & -i) bit[i] += val;
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
vector<ll> pos;
bool cmp(ll x,ll y){
return q[x] > q[y];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin>>n>>k;
for (ll i = 1;i <= n;i++){
cin>>a[i].x>>a[i].y,a[i].id = i;
if (a[i].x < a[i].y) a[i].dummy = 1,swap(a[i].x,a[i].y);
}
for (ll i = 1;i <= k;i++) cin>>q[i],upd(1,1,k,i,q[i]),pos.push_back(i);
sort(a + 1,a + n + 1,lf); sort(pos.begin(),pos.end(),cmp);
ll cur = 0,kq = 0,mn = inf;
for (ll i = 1;i <= n;i++){
while(cur < k&&q[pos[cur]] >= a[i].x){
mn = min(mn,pos[cur]);
upd(1,1,k,pos[cur],-inf); updBIT(pos[cur],1);
cur++;
}
ll nxt = WalkLast(1,1,k,a[i].y),prv = WalkFirst(1,1,k,a[i].y);
ll ans = Get(k) - Get(nxt); ans %= 2;
if (!nxt) ans += a[i].dummy; ans %= 2;
if (ans) kq += a[i].y;//cout<<a[i].y<<"\n";
else kq += a[i].x;//cout<<a[i].x<<"\n";
}
cout<<kq;
}
Compilation message
fortune_telling2.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization "O2"
|
fortune_telling2.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:99:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
99 | if (!nxt) ans += a[i].dummy; ans %= 2;
| ^~
fortune_telling2.cpp:99:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
99 | if (!nxt) ans += a[i].dummy; ans %= 2;
| ^~~
fortune_telling2.cpp:97:41: warning: unused variable 'prv' [-Wunused-variable]
97 | ll nxt = WalkLast(1,1,k,a[i].y),prv = WalkFirst(1,1,k,a[i].y);
| ^~~
fortune_telling2.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
13 ms |
1568 KB |
Output is correct |
15 |
Correct |
26 ms |
2652 KB |
Output is correct |
16 |
Correct |
38 ms |
3488 KB |
Output is correct |
17 |
Correct |
53 ms |
4812 KB |
Output is correct |
18 |
Correct |
57 ms |
4748 KB |
Output is correct |
19 |
Correct |
54 ms |
4744 KB |
Output is correct |
20 |
Correct |
65 ms |
4732 KB |
Output is correct |
21 |
Correct |
44 ms |
4804 KB |
Output is correct |
22 |
Correct |
40 ms |
4364 KB |
Output is correct |
23 |
Correct |
42 ms |
4376 KB |
Output is correct |
24 |
Correct |
50 ms |
4280 KB |
Output is correct |
25 |
Correct |
39 ms |
4292 KB |
Output is correct |
26 |
Correct |
51 ms |
4472 KB |
Output is correct |
27 |
Correct |
53 ms |
4800 KB |
Output is correct |
28 |
Correct |
53 ms |
4764 KB |
Output is correct |
29 |
Correct |
61 ms |
4756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
13 ms |
1568 KB |
Output is correct |
15 |
Correct |
26 ms |
2652 KB |
Output is correct |
16 |
Correct |
38 ms |
3488 KB |
Output is correct |
17 |
Correct |
53 ms |
4812 KB |
Output is correct |
18 |
Correct |
57 ms |
4748 KB |
Output is correct |
19 |
Correct |
54 ms |
4744 KB |
Output is correct |
20 |
Correct |
65 ms |
4732 KB |
Output is correct |
21 |
Correct |
44 ms |
4804 KB |
Output is correct |
22 |
Correct |
40 ms |
4364 KB |
Output is correct |
23 |
Correct |
42 ms |
4376 KB |
Output is correct |
24 |
Correct |
50 ms |
4280 KB |
Output is correct |
25 |
Correct |
39 ms |
4292 KB |
Output is correct |
26 |
Correct |
51 ms |
4472 KB |
Output is correct |
27 |
Correct |
53 ms |
4800 KB |
Output is correct |
28 |
Correct |
53 ms |
4764 KB |
Output is correct |
29 |
Correct |
61 ms |
4756 KB |
Output is correct |
30 |
Correct |
206 ms |
11580 KB |
Output is correct |
31 |
Correct |
231 ms |
13644 KB |
Output is correct |
32 |
Correct |
281 ms |
16240 KB |
Output is correct |
33 |
Correct |
316 ms |
21232 KB |
Output is correct |
34 |
Correct |
126 ms |
11192 KB |
Output is correct |
35 |
Correct |
329 ms |
21300 KB |
Output is correct |
36 |
Correct |
314 ms |
21304 KB |
Output is correct |
37 |
Correct |
314 ms |
21316 KB |
Output is correct |
38 |
Correct |
296 ms |
21388 KB |
Output is correct |
39 |
Correct |
307 ms |
21212 KB |
Output is correct |
40 |
Correct |
262 ms |
21088 KB |
Output is correct |
41 |
Correct |
309 ms |
21272 KB |
Output is correct |
42 |
Correct |
319 ms |
21304 KB |
Output is correct |
43 |
Correct |
207 ms |
19488 KB |
Output is correct |
44 |
Correct |
202 ms |
19604 KB |
Output is correct |
45 |
Correct |
202 ms |
20080 KB |
Output is correct |
46 |
Correct |
254 ms |
19332 KB |
Output is correct |
47 |
Correct |
273 ms |
19276 KB |
Output is correct |
48 |
Correct |
305 ms |
21336 KB |
Output is correct |
49 |
Correct |
291 ms |
21464 KB |
Output is correct |