//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
struct node{
int s,e,m;
int val=-1;
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void update(int i,int j){
val=max(val,j);
if (s==e) return;
else if (i<=m) l->update(i,j);
else r->update(i,j);
}
int query(int i,int j){
if (s==i && e==j) return val;
else if (j<=m) return l->query(i,j);
else if (m<i) return r->query(i,j);
else return max(l->query(i,m),r->query(m+1,j));
}
} *root=new node(0,600005);
struct node2{
int s,e,m;
int val=0;
node2 *l,*r;
node2 (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node2(s,m);
r=new node2(m+1,e);
}
}
void update(int i){
val++;
if (s==e) return;
else if (i<=m) l->update(i);
else r->update(i);
}
int query(int i,int j){
if (s==i && e==j) return val;
else if (j<=m) return l->query(i,j);
else if (m<i) return r->query(i,j);
else return l->query(i,m)+r->query(m+1,j);
}
} *root2=new node2(0,600005);
int n,q;
ii arr[200005];
int brr[200005];
vector<ii> proc;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>q;
rep(x,0,n) cin>>arr[x].fi>>arr[x].se;
rep(x,0,q) cin>>brr[x];
vector<int> uni;
rep(x,0,n) uni.push_back(arr[x].fi),uni.push_back(arr[x].se);
rep(x,0,q) uni.push_back(brr[x]);
sort(all(uni));
uni.erase(unique(all(uni)),uni.end());
map<int,int> id;
rep(x,0,sz(uni)) id[uni[x]]=x;
rep(x,0,n) arr[x].fi=id[arr[x].fi],arr[x].se=id[arr[x].se];
rep(x,0,q) brr[x]=id[brr[x]];
rep(x,0,q){
root->update(brr[x],x);
}
rep(x,0,n){
ii temp=arr[x];
if (temp.fi>temp.se) swap(temp.fi,temp.se);
int val=-1;
if (temp.fi!=temp.se) val=root->query(temp.fi,temp.se-1);
proc.push_back(ii(val+1,x));
}
sort(all(proc));
int idx=q;
ll ans=0;
while (!proc.empty()){
while (idx!=proc.back().fi){
idx--;
//cout<<"upd: "<<brr[idx]<<endl;
root2->update(brr[idx]);
}
int pos=proc.back().se;
bool orient;
if (proc.back().fi==0) orient=false;
else orient=arr[pos].fi<arr[pos].se;
//cout<<idx<<" "<<orient<<endl;
//cout<<root2->query(max(arr[pos].fi,arr[pos].se),600005)<<endl;
orient^=((root2->query(max(arr[pos].fi,arr[pos].se),600005))%2==1);
//cout<<idx<<" "<<orient<<endl;
if (!orient) ans+=uni[arr[pos].fi];
else ans+=uni[arr[pos].se];
proc.pop_back();
}
cout<<ans<<endl;
}
Compilation message
fortune_telling2.cpp: In constructor 'node::node(int, int)':
fortune_telling2.cpp:34:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | s=_s,e=_e,m=s+e>>1;
| ~^~
fortune_telling2.cpp: In constructor 'node2::node2(int, int)':
fortune_telling2.cpp:64:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | s=_s,e=_e,m=s+e>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
113132 KB |
Output is correct |
2 |
Correct |
125 ms |
113260 KB |
Output is correct |
3 |
Correct |
128 ms |
113408 KB |
Output is correct |
4 |
Correct |
124 ms |
113260 KB |
Output is correct |
5 |
Correct |
125 ms |
113260 KB |
Output is correct |
6 |
Correct |
127 ms |
113260 KB |
Output is correct |
7 |
Correct |
127 ms |
113260 KB |
Output is correct |
8 |
Correct |
127 ms |
113260 KB |
Output is correct |
9 |
Correct |
127 ms |
113260 KB |
Output is correct |
10 |
Correct |
154 ms |
113240 KB |
Output is correct |
11 |
Correct |
128 ms |
113252 KB |
Output is correct |
12 |
Correct |
124 ms |
113260 KB |
Output is correct |
13 |
Correct |
130 ms |
113388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
113132 KB |
Output is correct |
2 |
Correct |
125 ms |
113260 KB |
Output is correct |
3 |
Correct |
128 ms |
113408 KB |
Output is correct |
4 |
Correct |
124 ms |
113260 KB |
Output is correct |
5 |
Correct |
125 ms |
113260 KB |
Output is correct |
6 |
Correct |
127 ms |
113260 KB |
Output is correct |
7 |
Correct |
127 ms |
113260 KB |
Output is correct |
8 |
Correct |
127 ms |
113260 KB |
Output is correct |
9 |
Correct |
127 ms |
113260 KB |
Output is correct |
10 |
Correct |
154 ms |
113240 KB |
Output is correct |
11 |
Correct |
128 ms |
113252 KB |
Output is correct |
12 |
Correct |
124 ms |
113260 KB |
Output is correct |
13 |
Correct |
130 ms |
113388 KB |
Output is correct |
14 |
Correct |
152 ms |
115436 KB |
Output is correct |
15 |
Correct |
196 ms |
117748 KB |
Output is correct |
16 |
Correct |
237 ms |
119916 KB |
Output is correct |
17 |
Correct |
303 ms |
122284 KB |
Output is correct |
18 |
Correct |
337 ms |
122356 KB |
Output is correct |
19 |
Correct |
285 ms |
122520 KB |
Output is correct |
20 |
Correct |
300 ms |
122316 KB |
Output is correct |
21 |
Correct |
283 ms |
122224 KB |
Output is correct |
22 |
Correct |
215 ms |
119916 KB |
Output is correct |
23 |
Correct |
199 ms |
119024 KB |
Output is correct |
24 |
Correct |
202 ms |
118324 KB |
Output is correct |
25 |
Correct |
231 ms |
120556 KB |
Output is correct |
26 |
Correct |
220 ms |
119860 KB |
Output is correct |
27 |
Correct |
254 ms |
120480 KB |
Output is correct |
28 |
Correct |
226 ms |
120556 KB |
Output is correct |
29 |
Correct |
261 ms |
121328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
113132 KB |
Output is correct |
2 |
Correct |
125 ms |
113260 KB |
Output is correct |
3 |
Correct |
128 ms |
113408 KB |
Output is correct |
4 |
Correct |
124 ms |
113260 KB |
Output is correct |
5 |
Correct |
125 ms |
113260 KB |
Output is correct |
6 |
Correct |
127 ms |
113260 KB |
Output is correct |
7 |
Correct |
127 ms |
113260 KB |
Output is correct |
8 |
Correct |
127 ms |
113260 KB |
Output is correct |
9 |
Correct |
127 ms |
113260 KB |
Output is correct |
10 |
Correct |
154 ms |
113240 KB |
Output is correct |
11 |
Correct |
128 ms |
113252 KB |
Output is correct |
12 |
Correct |
124 ms |
113260 KB |
Output is correct |
13 |
Correct |
130 ms |
113388 KB |
Output is correct |
14 |
Correct |
152 ms |
115436 KB |
Output is correct |
15 |
Correct |
196 ms |
117748 KB |
Output is correct |
16 |
Correct |
237 ms |
119916 KB |
Output is correct |
17 |
Correct |
303 ms |
122284 KB |
Output is correct |
18 |
Correct |
337 ms |
122356 KB |
Output is correct |
19 |
Correct |
285 ms |
122520 KB |
Output is correct |
20 |
Correct |
300 ms |
122316 KB |
Output is correct |
21 |
Correct |
283 ms |
122224 KB |
Output is correct |
22 |
Correct |
215 ms |
119916 KB |
Output is correct |
23 |
Correct |
199 ms |
119024 KB |
Output is correct |
24 |
Correct |
202 ms |
118324 KB |
Output is correct |
25 |
Correct |
231 ms |
120556 KB |
Output is correct |
26 |
Correct |
220 ms |
119860 KB |
Output is correct |
27 |
Correct |
254 ms |
120480 KB |
Output is correct |
28 |
Correct |
226 ms |
120556 KB |
Output is correct |
29 |
Correct |
261 ms |
121328 KB |
Output is correct |
30 |
Correct |
494 ms |
127860 KB |
Output is correct |
31 |
Correct |
754 ms |
134756 KB |
Output is correct |
32 |
Correct |
1075 ms |
142948 KB |
Output is correct |
33 |
Correct |
1623 ms |
159544 KB |
Output is correct |
34 |
Correct |
449 ms |
126056 KB |
Output is correct |
35 |
Correct |
1673 ms |
159708 KB |
Output is correct |
36 |
Correct |
1685 ms |
159604 KB |
Output is correct |
37 |
Correct |
1622 ms |
159580 KB |
Output is correct |
38 |
Correct |
1505 ms |
159708 KB |
Output is correct |
39 |
Correct |
1578 ms |
159520 KB |
Output is correct |
40 |
Correct |
1377 ms |
159324 KB |
Output is correct |
41 |
Correct |
1473 ms |
159600 KB |
Output is correct |
42 |
Correct |
1508 ms |
159580 KB |
Output is correct |
43 |
Correct |
724 ms |
158940 KB |
Output is correct |
44 |
Correct |
707 ms |
158988 KB |
Output is correct |
45 |
Correct |
757 ms |
158752 KB |
Output is correct |
46 |
Correct |
734 ms |
143708 KB |
Output is correct |
47 |
Correct |
602 ms |
138716 KB |
Output is correct |
48 |
Correct |
1044 ms |
150108 KB |
Output is correct |
49 |
Correct |
980 ms |
150236 KB |
Output is correct |