/*input
5 3
4 6
9 1
8 8
4 2
3 7
8
2
9
*/
#include<bits/stdc++.h>
using namespace std;
const int N=2e5 +100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
#define FOR(i, n) for(int i=1;i<=n;i++)
#define TRACE(x) cerr << #x << " = " << x << endl
//Trace prints the name of the variable and the value.
int n, k, a[N], b[N];pii ops[N];vector<int> dup;
vector<int> tree[4*N];
void build(int i=1, int l=1, int r=k)
{
if(l==r) {
tree[i].push_back(ops[l].s);return;
}
int mid=(l+r)>>1;build(2*i, l, mid);build(2*i+1, mid+1, r);
tree[i].insert(tree[i].end(), tree[2*i].begin(), tree[2*i].end());
tree[i].insert(tree[i].end(), tree[2*i+1].begin(), tree[2*i+1].end());
sort(tree[i].begin(), tree[i].end());
}
int ql, qr;
int rmq(int i=1, int l=1, int r=k)
{
if(l>qr||r<ql) return -inf;
if(l>=ql&&r<=qr) return tree[i].back();
int mid=(l+r)>>1;return max(rmq(2*i, l, mid), rmq(2*i+1, mid+1, r));
}
int val;
int query(int i=1, int l=1, int r=k)
{
if(l>qr||r<ql) return 0;
if(l>=ql&&r<=qr)
{
auto it=lower_bound(tree[i].begin(), tree[i].end(), val);
if(it==tree[i].begin()) return tree[i].size();
if(it==tree[i].end()) return 0;
int num=(it - tree[i].begin());num=tree[i].size()-num;return num;
}
int mid=(l+r)>>1;return query(2*i, l, mid)+ query(2*i+1, mid+1, r);
}
void solve()
{
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
for(int i=1;i<=k;i++)
{
cin>>ops[i].f;ops[i].s=i;
}
sort(ops+1, ops+k+1);
for(int i=1;i<=k;i++) dup.push_back(ops[i].f);
build();int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]==b[i]) {ans+=a[i];continue;}
int mx=max(a[i], b[i]), mn=min(a[i], b[i]);
auto it=lower_bound(dup.begin(), dup.end(), mn);
if(it==dup.end())
{
ans+=a[i];continue;
}
int smn=(it-dup.begin());smn=dup.size() - smn;ql=k-smn + 1;
it=lower_bound(dup.begin(), dup.end(), mx);
if(it==dup.end())
{
ans+=mx;continue;
}
qr=(it-dup.begin());
int lpos;
if(ql<=qr) lpos=rmq();
else lpos=inf;
val=lpos;
ql=(it-dup.begin())+1;qr=k;int num=query();
int npos=rmq();
if(lpos==inf)
{
int tm=(qr-ql+1);tm%=2;
if(tm==0) ans+=a[i];
else ans+=b[i];continue;
}
if(lpos>npos)
{
ans+=mx;continue;
}
if(lpos<npos)
{
if(num%2==0) ans+=mx;
else ans+=mn;
}
}
cout<<ans<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t=1;
while(t--)
{
solve();
}
}
Compilation message
fortune_telling2.cpp: In function 'void solve()':
fortune_telling2.cpp:96:4: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
else ans+=b[i];continue;
^~~~
fortune_telling2.cpp:96:19: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
else ans+=b[i];continue;
^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19328 KB |
Output is correct |
2 |
Correct |
17 ms |
19328 KB |
Output is correct |
3 |
Correct |
17 ms |
19456 KB |
Output is correct |
4 |
Correct |
18 ms |
19456 KB |
Output is correct |
5 |
Correct |
19 ms |
19456 KB |
Output is correct |
6 |
Correct |
17 ms |
19328 KB |
Output is correct |
7 |
Correct |
18 ms |
19328 KB |
Output is correct |
8 |
Correct |
17 ms |
19328 KB |
Output is correct |
9 |
Correct |
17 ms |
19328 KB |
Output is correct |
10 |
Correct |
17 ms |
19328 KB |
Output is correct |
11 |
Correct |
17 ms |
19328 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
18 ms |
19456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19328 KB |
Output is correct |
2 |
Correct |
17 ms |
19328 KB |
Output is correct |
3 |
Correct |
17 ms |
19456 KB |
Output is correct |
4 |
Correct |
18 ms |
19456 KB |
Output is correct |
5 |
Correct |
19 ms |
19456 KB |
Output is correct |
6 |
Correct |
17 ms |
19328 KB |
Output is correct |
7 |
Correct |
18 ms |
19328 KB |
Output is correct |
8 |
Correct |
17 ms |
19328 KB |
Output is correct |
9 |
Correct |
17 ms |
19328 KB |
Output is correct |
10 |
Correct |
17 ms |
19328 KB |
Output is correct |
11 |
Correct |
17 ms |
19328 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
18 ms |
19456 KB |
Output is correct |
14 |
Correct |
37 ms |
21504 KB |
Output is correct |
15 |
Correct |
64 ms |
23808 KB |
Output is correct |
16 |
Correct |
96 ms |
26360 KB |
Output is correct |
17 |
Correct |
130 ms |
29172 KB |
Output is correct |
18 |
Correct |
125 ms |
29040 KB |
Output is correct |
19 |
Correct |
123 ms |
29116 KB |
Output is correct |
20 |
Correct |
134 ms |
29168 KB |
Output is correct |
21 |
Correct |
115 ms |
29172 KB |
Output is correct |
22 |
Correct |
97 ms |
28532 KB |
Output is correct |
23 |
Correct |
100 ms |
28528 KB |
Output is correct |
24 |
Correct |
98 ms |
28532 KB |
Output is correct |
25 |
Correct |
97 ms |
28660 KB |
Output is correct |
26 |
Correct |
109 ms |
28932 KB |
Output is correct |
27 |
Correct |
123 ms |
29044 KB |
Output is correct |
28 |
Correct |
110 ms |
29044 KB |
Output is correct |
29 |
Correct |
119 ms |
29044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19328 KB |
Output is correct |
2 |
Correct |
17 ms |
19328 KB |
Output is correct |
3 |
Correct |
17 ms |
19456 KB |
Output is correct |
4 |
Correct |
18 ms |
19456 KB |
Output is correct |
5 |
Correct |
19 ms |
19456 KB |
Output is correct |
6 |
Correct |
17 ms |
19328 KB |
Output is correct |
7 |
Correct |
18 ms |
19328 KB |
Output is correct |
8 |
Correct |
17 ms |
19328 KB |
Output is correct |
9 |
Correct |
17 ms |
19328 KB |
Output is correct |
10 |
Correct |
17 ms |
19328 KB |
Output is correct |
11 |
Correct |
17 ms |
19328 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
18 ms |
19456 KB |
Output is correct |
14 |
Correct |
37 ms |
21504 KB |
Output is correct |
15 |
Correct |
64 ms |
23808 KB |
Output is correct |
16 |
Correct |
96 ms |
26360 KB |
Output is correct |
17 |
Correct |
130 ms |
29172 KB |
Output is correct |
18 |
Correct |
125 ms |
29040 KB |
Output is correct |
19 |
Correct |
123 ms |
29116 KB |
Output is correct |
20 |
Correct |
134 ms |
29168 KB |
Output is correct |
21 |
Correct |
115 ms |
29172 KB |
Output is correct |
22 |
Correct |
97 ms |
28532 KB |
Output is correct |
23 |
Correct |
100 ms |
28528 KB |
Output is correct |
24 |
Correct |
98 ms |
28532 KB |
Output is correct |
25 |
Correct |
97 ms |
28660 KB |
Output is correct |
26 |
Correct |
109 ms |
28932 KB |
Output is correct |
27 |
Correct |
123 ms |
29044 KB |
Output is correct |
28 |
Correct |
110 ms |
29044 KB |
Output is correct |
29 |
Correct |
119 ms |
29044 KB |
Output is correct |
30 |
Correct |
329 ms |
65516 KB |
Output is correct |
31 |
Correct |
426 ms |
67048 KB |
Output is correct |
32 |
Correct |
544 ms |
68784 KB |
Output is correct |
33 |
Correct |
824 ms |
72296 KB |
Output is correct |
34 |
Correct |
306 ms |
65132 KB |
Output is correct |
35 |
Correct |
800 ms |
72432 KB |
Output is correct |
36 |
Correct |
776 ms |
72296 KB |
Output is correct |
37 |
Correct |
793 ms |
72288 KB |
Output is correct |
38 |
Correct |
771 ms |
72424 KB |
Output is correct |
39 |
Correct |
803 ms |
72292 KB |
Output is correct |
40 |
Correct |
622 ms |
72044 KB |
Output is correct |
41 |
Correct |
794 ms |
72296 KB |
Output is correct |
42 |
Correct |
792 ms |
72300 KB |
Output is correct |
43 |
Correct |
444 ms |
71400 KB |
Output is correct |
44 |
Correct |
457 ms |
71528 KB |
Output is correct |
45 |
Correct |
461 ms |
71408 KB |
Output is correct |
46 |
Correct |
505 ms |
70404 KB |
Output is correct |
47 |
Correct |
510 ms |
70120 KB |
Output is correct |
48 |
Correct |
669 ms |
72248 KB |
Output is correct |
49 |
Correct |
642 ms |
72348 KB |
Output is correct |