#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define _all(T) T.begin(),T.end()
const int mxn = 6e5+10;
int N,Q;
vector<int> all;
int rp[mxn];
pii range[mxn];
int arr[mxn];
bitset<mxn> flop;
namespace C1{
vector<pii> bit[mxn];
void add(int p,pii v){
for(;p<mxn;p+=p&-p){
bit[p].push_back(v);
}
return;
}
void getval(int p,int id){
for(int i = p;i>0;i-= i&-i){
while(!bit[i].empty()&&bit[i].back().fs>=p){
int now = bit[i].back().sc;bit[i].pop_back();
rp[now] = max(rp[now],id);
}
}
return;
}
void GO(){
for(int i = 1;i<=N;i++){
if(range[i].fs == range[i].sc)continue;
add(range[i].fs,pii(range[i].sc-1,i));
}
for(int i = 1;i<mxn;i++)sort(bit[i].begin(),bit[i].end());
for(int i = Q;i>=1;i--){
getval(arr[i],i);
}
//for(int i = 1;i<=N;i++)cout<<rp[i]<<' ';cout<<endl;
return;
}
}
namespace C2{
ll ans = 0;
int bit[mxn];
void modify(int p,int v){
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
int getval(int s,int e){
int re = 0;
for(;e>0;e-= e&-e)re += bit[e];
s--;
for(;s>0;s-= s&-s)re -= bit[s];
return re;
}
vector<int> op[mxn];
void GO(){
ans = 0;
for(int i = 1;i<=N;i++){
op[rp[i]].push_back(i);
}
for(int i = Q;i>=1;i--){
modify(arr[i],1);
for(auto &j:op[i]){
int cnt = getval(range[j].sc,mxn-1);
if(cnt&1)ans += all[range[j].fs];
else ans += all[range[j].sc];
}
}
for(auto &j:op[0]){
int cnt = getval(range[j].sc,mxn-1);
if(flop[j])swap(range[j].fs,range[j].sc);
if(cnt&1)ans += all[range[j].sc];
else ans += all[range[j].fs];
}
cout<<ans<<'\n';
return;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>Q;
all.push_back(-1);
for(int i = 1;i<=N;i++){
cin>>range[i].fs>>range[i].sc;
if(range[i].fs>range[i].sc){
flop[i] = true;
swap(range[i].fs,range[i].sc);
}
all.push_back(range[i].fs);
all.push_back(range[i].sc);
}
for(int i = 1;i<=Q;i++){
cin>>arr[i];
all.push_back(arr[i]);
}
sort(all.begin(),all.end());all.resize(unique(all.begin(),all.end())-all.begin());
for(int i = 1;i<=N;i++){
range[i].fs = lower_bound(_all(all),range[i].fs)-all.begin();
range[i].sc = lower_bound(_all(all),range[i].sc)-all.begin();
}
for(int i = 1;i<=Q;i++)arr[i] = lower_bound(_all(all),arr[i])-all.begin();
C1::GO();
C2::GO();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33468 KB |
Output is correct |
2 |
Correct |
10 ms |
33416 KB |
Output is correct |
3 |
Correct |
12 ms |
33628 KB |
Output is correct |
4 |
Correct |
10 ms |
33624 KB |
Output is correct |
5 |
Correct |
10 ms |
33476 KB |
Output is correct |
6 |
Correct |
10 ms |
33628 KB |
Output is correct |
7 |
Correct |
10 ms |
33628 KB |
Output is correct |
8 |
Correct |
10 ms |
33476 KB |
Output is correct |
9 |
Correct |
15 ms |
33624 KB |
Output is correct |
10 |
Correct |
10 ms |
33628 KB |
Output is correct |
11 |
Correct |
12 ms |
33624 KB |
Output is correct |
12 |
Correct |
12 ms |
33624 KB |
Output is correct |
13 |
Correct |
11 ms |
33688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33468 KB |
Output is correct |
2 |
Correct |
10 ms |
33416 KB |
Output is correct |
3 |
Correct |
12 ms |
33628 KB |
Output is correct |
4 |
Correct |
10 ms |
33624 KB |
Output is correct |
5 |
Correct |
10 ms |
33476 KB |
Output is correct |
6 |
Correct |
10 ms |
33628 KB |
Output is correct |
7 |
Correct |
10 ms |
33628 KB |
Output is correct |
8 |
Correct |
10 ms |
33476 KB |
Output is correct |
9 |
Correct |
15 ms |
33624 KB |
Output is correct |
10 |
Correct |
10 ms |
33628 KB |
Output is correct |
11 |
Correct |
12 ms |
33624 KB |
Output is correct |
12 |
Correct |
12 ms |
33624 KB |
Output is correct |
13 |
Correct |
11 ms |
33688 KB |
Output is correct |
14 |
Correct |
25 ms |
37600 KB |
Output is correct |
15 |
Correct |
45 ms |
39492 KB |
Output is correct |
16 |
Correct |
57 ms |
41240 KB |
Output is correct |
17 |
Correct |
77 ms |
43360 KB |
Output is correct |
18 |
Correct |
74 ms |
43468 KB |
Output is correct |
19 |
Correct |
74 ms |
43472 KB |
Output is correct |
20 |
Correct |
86 ms |
43296 KB |
Output is correct |
21 |
Correct |
71 ms |
42964 KB |
Output is correct |
22 |
Correct |
45 ms |
41948 KB |
Output is correct |
23 |
Correct |
47 ms |
42052 KB |
Output is correct |
24 |
Correct |
47 ms |
41932 KB |
Output is correct |
25 |
Correct |
44 ms |
42224 KB |
Output is correct |
26 |
Correct |
58 ms |
41224 KB |
Output is correct |
27 |
Correct |
64 ms |
42360 KB |
Output is correct |
28 |
Correct |
81 ms |
44376 KB |
Output is correct |
29 |
Correct |
72 ms |
42672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
33468 KB |
Output is correct |
2 |
Correct |
10 ms |
33416 KB |
Output is correct |
3 |
Correct |
12 ms |
33628 KB |
Output is correct |
4 |
Correct |
10 ms |
33624 KB |
Output is correct |
5 |
Correct |
10 ms |
33476 KB |
Output is correct |
6 |
Correct |
10 ms |
33628 KB |
Output is correct |
7 |
Correct |
10 ms |
33628 KB |
Output is correct |
8 |
Correct |
10 ms |
33476 KB |
Output is correct |
9 |
Correct |
15 ms |
33624 KB |
Output is correct |
10 |
Correct |
10 ms |
33628 KB |
Output is correct |
11 |
Correct |
12 ms |
33624 KB |
Output is correct |
12 |
Correct |
12 ms |
33624 KB |
Output is correct |
13 |
Correct |
11 ms |
33688 KB |
Output is correct |
14 |
Correct |
25 ms |
37600 KB |
Output is correct |
15 |
Correct |
45 ms |
39492 KB |
Output is correct |
16 |
Correct |
57 ms |
41240 KB |
Output is correct |
17 |
Correct |
77 ms |
43360 KB |
Output is correct |
18 |
Correct |
74 ms |
43468 KB |
Output is correct |
19 |
Correct |
74 ms |
43472 KB |
Output is correct |
20 |
Correct |
86 ms |
43296 KB |
Output is correct |
21 |
Correct |
71 ms |
42964 KB |
Output is correct |
22 |
Correct |
45 ms |
41948 KB |
Output is correct |
23 |
Correct |
47 ms |
42052 KB |
Output is correct |
24 |
Correct |
47 ms |
41932 KB |
Output is correct |
25 |
Correct |
44 ms |
42224 KB |
Output is correct |
26 |
Correct |
58 ms |
41224 KB |
Output is correct |
27 |
Correct |
64 ms |
42360 KB |
Output is correct |
28 |
Correct |
81 ms |
44376 KB |
Output is correct |
29 |
Correct |
72 ms |
42672 KB |
Output is correct |
30 |
Correct |
88 ms |
40140 KB |
Output is correct |
31 |
Correct |
161 ms |
47408 KB |
Output is correct |
32 |
Correct |
231 ms |
55440 KB |
Output is correct |
33 |
Correct |
411 ms |
73640 KB |
Output is correct |
34 |
Correct |
70 ms |
36300 KB |
Output is correct |
35 |
Correct |
411 ms |
72856 KB |
Output is correct |
36 |
Correct |
416 ms |
73192 KB |
Output is correct |
37 |
Correct |
414 ms |
72896 KB |
Output is correct |
38 |
Correct |
408 ms |
74036 KB |
Output is correct |
39 |
Correct |
404 ms |
72864 KB |
Output is correct |
40 |
Correct |
374 ms |
71100 KB |
Output is correct |
41 |
Correct |
394 ms |
72436 KB |
Output is correct |
42 |
Correct |
419 ms |
73108 KB |
Output is correct |
43 |
Correct |
195 ms |
68032 KB |
Output is correct |
44 |
Correct |
193 ms |
68276 KB |
Output is correct |
45 |
Correct |
189 ms |
67404 KB |
Output is correct |
46 |
Correct |
203 ms |
66340 KB |
Output is correct |
47 |
Correct |
208 ms |
65084 KB |
Output is correct |
48 |
Correct |
331 ms |
71412 KB |
Output is correct |
49 |
Correct |
392 ms |
73688 KB |
Output is correct |