#include <bits/stdc++.h>
#define Tree int h,int l,int r
#define Left (h<<1),l,(l+r)>>1
#define Right ((h<<1)|1),((l+r)>>1)+1,r
#define ll long long
#define pb push_back
#define F first
#define S second
using namespace std;
const int Nm=2e5+5;
ll ans;
int n,q,L,R,S;
int l[Nm],r[Nm],t[Nm];
int G[3*Nm],Last[3*Nm],Rf[3*Nm],v[12*Nm];
pair < int , int > Id[Nm];
vector < int > s;
map < int , int > f;
void Build(Tree) {
if (l==r) { v[h]=Last[l]; return ;}
Build(Left),Build(Right);
v[h]=max(v[(h<<1)],v[((h<<1)|1)]);
}
int Get(Tree) {
if (r<L || R<l) return 0;
if (L<=l && r<=R) return v[h];
int x=Get(Left),y=Get(Right);
return max(x,y);
}
void Up(int x) {
while (x<=S) {
G[x]++;
x+=(x & -x);
}
}
int Ge(int x) {
int res=0;
while (x>0) {
res+=G[x];
x-=(x & -x);
}
return res;
}
main () {
scanf("%d%d",&n,&q);
for (int i=1; i<=n; i++) {
scanf("%d%d",&l[i],&r[i]);
s.pb(l[i]),s.pb(r[i]);
}
for (int i=1; i<=q; i++) {
scanf("%d",&t[i]);
s.pb(t[i]);
}
sort(s.begin(),s.end());
s.erase(unique(s.begin(),s.end()),s.end());
for (int i=0; i<s.size(); i++) f[s[i]]=i+1,Rf[i+1]=s[i];
for (int i=1; i<=n; i++) l[i]=f[l[i]],r[i]=f[r[i]];
for (int i=1; i<=q; i++) t[i]=f[t[i]];
S=s.size();
s.clear(),f.clear();
for (int i=1; i<=q; i++) Last[t[i]]=i;
Build(1,1,S);
for (int i=1; i<=n; i++) {
L=min(l[i],r[i]),R=max(l[i],r[i])-1;
Id[i].F=Get(1,1,S),Id[i].S=i;
}
sort(Id+1,Id+n+1);
int Q=q,tot=0;
for (int i=n; i>=1; i--) {
while (Q && Id[i].F<Q) Up(t[Q]),--Q;
tot=Ge(S)-Ge(max(l[Id[i].S],r[Id[i].S])-1);
if (Id[i].F && l[Id[i].S]<r[Id[i].S])
swap(l[Id[i].S],r[Id[i].S]);
if (tot%2)
ans+=Rf[r[Id[i].S]];
else
ans+=Rf[l[Id[i].S]];
}
printf("%lld\n",ans);
}
Compilation message
fortune_telling2.cpp:48:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:63:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size(); i++) f[s[i]]=i+1,Rf[i+1]=s[i];
~^~~~~~~~~
fortune_telling2.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&q);
~~~~~^~~~~~~~~~~~~~
fortune_telling2.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&l[i],&r[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&t[i]);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
7 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
632 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
608 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
7 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
632 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
608 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
30 ms |
2680 KB |
Output is correct |
15 |
Correct |
64 ms |
5068 KB |
Output is correct |
16 |
Correct |
96 ms |
7664 KB |
Output is correct |
17 |
Correct |
137 ms |
9688 KB |
Output is correct |
18 |
Correct |
139 ms |
9712 KB |
Output is correct |
19 |
Correct |
135 ms |
9324 KB |
Output is correct |
20 |
Correct |
133 ms |
9712 KB |
Output is correct |
21 |
Correct |
128 ms |
9328 KB |
Output is correct |
22 |
Correct |
93 ms |
7152 KB |
Output is correct |
23 |
Correct |
83 ms |
5464 KB |
Output is correct |
24 |
Correct |
69 ms |
4720 KB |
Output is correct |
25 |
Correct |
84 ms |
7664 KB |
Output is correct |
26 |
Correct |
85 ms |
6768 KB |
Output is correct |
27 |
Correct |
100 ms |
7408 KB |
Output is correct |
28 |
Correct |
91 ms |
7280 KB |
Output is correct |
29 |
Correct |
107 ms |
8608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
7 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
632 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
608 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
30 ms |
2680 KB |
Output is correct |
15 |
Correct |
64 ms |
5068 KB |
Output is correct |
16 |
Correct |
96 ms |
7664 KB |
Output is correct |
17 |
Correct |
137 ms |
9688 KB |
Output is correct |
18 |
Correct |
139 ms |
9712 KB |
Output is correct |
19 |
Correct |
135 ms |
9324 KB |
Output is correct |
20 |
Correct |
133 ms |
9712 KB |
Output is correct |
21 |
Correct |
128 ms |
9328 KB |
Output is correct |
22 |
Correct |
93 ms |
7152 KB |
Output is correct |
23 |
Correct |
83 ms |
5464 KB |
Output is correct |
24 |
Correct |
69 ms |
4720 KB |
Output is correct |
25 |
Correct |
84 ms |
7664 KB |
Output is correct |
26 |
Correct |
85 ms |
6768 KB |
Output is correct |
27 |
Correct |
100 ms |
7408 KB |
Output is correct |
28 |
Correct |
91 ms |
7280 KB |
Output is correct |
29 |
Correct |
107 ms |
8608 KB |
Output is correct |
30 |
Correct |
264 ms |
17132 KB |
Output is correct |
31 |
Correct |
425 ms |
24896 KB |
Output is correct |
32 |
Correct |
629 ms |
31808 KB |
Output is correct |
33 |
Correct |
1102 ms |
50300 KB |
Output is correct |
34 |
Correct |
219 ms |
14956 KB |
Output is correct |
35 |
Correct |
1075 ms |
50012 KB |
Output is correct |
36 |
Correct |
1110 ms |
50064 KB |
Output is correct |
37 |
Correct |
1071 ms |
50280 KB |
Output is correct |
38 |
Correct |
1086 ms |
47836 KB |
Output is correct |
39 |
Correct |
1090 ms |
50140 KB |
Output is correct |
40 |
Correct |
926 ms |
47716 KB |
Output is correct |
41 |
Correct |
1069 ms |
48088 KB |
Output is correct |
42 |
Correct |
1091 ms |
49140 KB |
Output is correct |
43 |
Correct |
533 ms |
46556 KB |
Output is correct |
44 |
Correct |
547 ms |
46812 KB |
Output is correct |
45 |
Correct |
539 ms |
47068 KB |
Output is correct |
46 |
Correct |
482 ms |
27544 KB |
Output is correct |
47 |
Correct |
396 ms |
20444 KB |
Output is correct |
48 |
Correct |
640 ms |
34268 KB |
Output is correct |
49 |
Correct |
626 ms |
34140 KB |
Output is correct |