#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]];
}
cout<<ans<<"\n";
}
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 |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
6 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 |
8 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
7 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
6 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 |
8 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
7 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 |
61 ms |
4980 KB |
Output is correct |
16 |
Correct |
94 ms |
7664 KB |
Output is correct |
17 |
Correct |
145 ms |
9824 KB |
Output is correct |
18 |
Correct |
135 ms |
9712 KB |
Output is correct |
19 |
Correct |
151 ms |
9236 KB |
Output is correct |
20 |
Correct |
135 ms |
9616 KB |
Output is correct |
21 |
Correct |
130 ms |
9328 KB |
Output is correct |
22 |
Correct |
87 ms |
7024 KB |
Output is correct |
23 |
Correct |
76 ms |
5488 KB |
Output is correct |
24 |
Correct |
70 ms |
4720 KB |
Output is correct |
25 |
Correct |
86 ms |
7664 KB |
Output is correct |
26 |
Correct |
84 ms |
6896 KB |
Output is correct |
27 |
Correct |
101 ms |
7280 KB |
Output is correct |
28 |
Correct |
88 ms |
7280 KB |
Output is correct |
29 |
Correct |
108 ms |
8556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
6 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 |
8 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
7 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 |
61 ms |
4980 KB |
Output is correct |
16 |
Correct |
94 ms |
7664 KB |
Output is correct |
17 |
Correct |
145 ms |
9824 KB |
Output is correct |
18 |
Correct |
135 ms |
9712 KB |
Output is correct |
19 |
Correct |
151 ms |
9236 KB |
Output is correct |
20 |
Correct |
135 ms |
9616 KB |
Output is correct |
21 |
Correct |
130 ms |
9328 KB |
Output is correct |
22 |
Correct |
87 ms |
7024 KB |
Output is correct |
23 |
Correct |
76 ms |
5488 KB |
Output is correct |
24 |
Correct |
70 ms |
4720 KB |
Output is correct |
25 |
Correct |
86 ms |
7664 KB |
Output is correct |
26 |
Correct |
84 ms |
6896 KB |
Output is correct |
27 |
Correct |
101 ms |
7280 KB |
Output is correct |
28 |
Correct |
88 ms |
7280 KB |
Output is correct |
29 |
Correct |
108 ms |
8556 KB |
Output is correct |
30 |
Correct |
270 ms |
17256 KB |
Output is correct |
31 |
Correct |
415 ms |
24932 KB |
Output is correct |
32 |
Correct |
629 ms |
31844 KB |
Output is correct |
33 |
Correct |
1127 ms |
50060 KB |
Output is correct |
34 |
Correct |
222 ms |
14952 KB |
Output is correct |
35 |
Correct |
1090 ms |
50020 KB |
Output is correct |
36 |
Correct |
1119 ms |
50012 KB |
Output is correct |
37 |
Correct |
1096 ms |
50012 KB |
Output is correct |
38 |
Correct |
1092 ms |
47836 KB |
Output is correct |
39 |
Correct |
1113 ms |
50012 KB |
Output is correct |
40 |
Correct |
939 ms |
47708 KB |
Output is correct |
41 |
Correct |
1070 ms |
48148 KB |
Output is correct |
42 |
Correct |
1062 ms |
48988 KB |
Output is correct |
43 |
Correct |
543 ms |
46556 KB |
Output is correct |
44 |
Correct |
549 ms |
46556 KB |
Output is correct |
45 |
Correct |
563 ms |
47088 KB |
Output is correct |
46 |
Correct |
492 ms |
27228 KB |
Output is correct |
47 |
Correct |
392 ms |
20444 KB |
Output is correct |
48 |
Correct |
654 ms |
34304 KB |
Output is correct |
49 |
Correct |
636 ms |
34216 KB |
Output is correct |