#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
#define endl "\n"
const int N = 4e5 + 100,K = 19;
int a[N + 1],b[N + 1];
int val[N + 1],lst[N + 1],cnt = 0;
int sparse[N + 1][K + 1],lg[N + 1];
bool sign[N + 1];
map<int,int> mp;
vector<int> query;
vector<pair<int,int>> seg[4*N + 1];
int bit[N + 1];
void add(int pos) {
while(pos > 0) {
bit[pos]++;
pos -= pos&(-pos);
}
}
int queryBIT(int pos) {
int res = 0;
for(int i = pos;i <= cnt;i += i&(-i)) {
res += bit[i];
}
return res;
}
void buildSP() {
for(int i = 2;i <= N;i++) {
lg[i] = lg[i/2] + 1;
}
for(int i = 0;i <= cnt;i++) {
sparse[i][0] = lst[i];
}
for(int j = 1;j <= K;j++) {
for(int i = 0;i + (1<<j) <= N;i++) {
sparse[i][j] = max(sparse[i][j - 1],sparse[i + (1<<(j - 1))][j - 1]);
}
}
}
int querySP(int l,int r) {
int len = lg[r - l + 1];
return max(sparse[l][len],sparse[r - (1<<len) + 1][len]);
}
signed main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i = 1;i <= n;i++) {
cin>>a[i]>>b[i];
sign[i] = 0;
if(a[i] > b[i]) {
sign[i] = 1;
swap(a[i],b[i]);
}
mp[a[i]] = -1;
mp[b[i]] = -1;
}
for(int i = 0;i < k;i++) {
int x;
cin>>x;
mp[x] = -1;
query.push_back(x);
}
for(auto u : mp) {
mp[u.first] = cnt;
val[cnt] = u.first;
cnt++;
}
for(int i = 1;i <= n;i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
}
for(int i = 0;i < query.size();i++) {
query[i] = mp[query[i]];
}
for(int i = 0;i <= cnt;i++) {
lst[i] = -1;
}
for(int i = 0;i < query.size();i++) {
lst[query[i]] = max(lst[query[i]],i);
}
mp.clear();
ll res = 0;
buildSP();
vector<pair<int,int>> asd;
for(int i = 1;i <= n;i++) {
if(a[i] == b[i]) {
res += val[a[i]];
continue;
}
int nw = querySP(a[i],b[i] - 1);
asd.push_back({nw + 1,i});
}
sort(asd.begin(),asd.end());
reverse(asd.begin(),asd.end());
int ptr = query.size() - 1;
for(auto u : asd) {
while(ptr >= u.first) {
add(query[ptr]);
ptr--;
}
int w = sign[u.second];
if(u.first > 0) {
w = 1;
}
int cnt = queryBIT(b[u.second]);
w ^= (cnt%2);
if(w == 0) {
res += val[a[u.second]];
} else {
res += val[b[u.second]];
}
}
cout<<res<<endl;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:90:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int i = 0;i < query.size();i++) {
| ~~^~~~~~~~~~~~~~
fortune_telling2.cpp:96:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i = 0;i < query.size();i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
70884 KB |
Output is correct |
2 |
Correct |
78 ms |
70852 KB |
Output is correct |
3 |
Correct |
83 ms |
70992 KB |
Output is correct |
4 |
Correct |
80 ms |
70972 KB |
Output is correct |
5 |
Correct |
89 ms |
70988 KB |
Output is correct |
6 |
Correct |
90 ms |
70984 KB |
Output is correct |
7 |
Correct |
84 ms |
70988 KB |
Output is correct |
8 |
Correct |
86 ms |
70988 KB |
Output is correct |
9 |
Correct |
81 ms |
70988 KB |
Output is correct |
10 |
Correct |
85 ms |
70872 KB |
Output is correct |
11 |
Correct |
93 ms |
70932 KB |
Output is correct |
12 |
Correct |
86 ms |
70932 KB |
Output is correct |
13 |
Correct |
87 ms |
70948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
70884 KB |
Output is correct |
2 |
Correct |
78 ms |
70852 KB |
Output is correct |
3 |
Correct |
83 ms |
70992 KB |
Output is correct |
4 |
Correct |
80 ms |
70972 KB |
Output is correct |
5 |
Correct |
89 ms |
70988 KB |
Output is correct |
6 |
Correct |
90 ms |
70984 KB |
Output is correct |
7 |
Correct |
84 ms |
70988 KB |
Output is correct |
8 |
Correct |
86 ms |
70988 KB |
Output is correct |
9 |
Correct |
81 ms |
70988 KB |
Output is correct |
10 |
Correct |
85 ms |
70872 KB |
Output is correct |
11 |
Correct |
93 ms |
70932 KB |
Output is correct |
12 |
Correct |
86 ms |
70932 KB |
Output is correct |
13 |
Correct |
87 ms |
70948 KB |
Output is correct |
14 |
Correct |
99 ms |
72628 KB |
Output is correct |
15 |
Correct |
143 ms |
74500 KB |
Output is correct |
16 |
Correct |
148 ms |
76120 KB |
Output is correct |
17 |
Correct |
187 ms |
77896 KB |
Output is correct |
18 |
Correct |
199 ms |
78264 KB |
Output is correct |
19 |
Correct |
196 ms |
77848 KB |
Output is correct |
20 |
Correct |
206 ms |
77840 KB |
Output is correct |
21 |
Correct |
172 ms |
77808 KB |
Output is correct |
22 |
Correct |
150 ms |
75768 KB |
Output is correct |
23 |
Correct |
134 ms |
74740 KB |
Output is correct |
24 |
Correct |
127 ms |
73984 KB |
Output is correct |
25 |
Correct |
149 ms |
76320 KB |
Output is correct |
26 |
Correct |
149 ms |
75496 KB |
Output is correct |
27 |
Correct |
158 ms |
75988 KB |
Output is correct |
28 |
Correct |
155 ms |
75848 KB |
Output is correct |
29 |
Correct |
169 ms |
76688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
70884 KB |
Output is correct |
2 |
Correct |
78 ms |
70852 KB |
Output is correct |
3 |
Correct |
83 ms |
70992 KB |
Output is correct |
4 |
Correct |
80 ms |
70972 KB |
Output is correct |
5 |
Correct |
89 ms |
70988 KB |
Output is correct |
6 |
Correct |
90 ms |
70984 KB |
Output is correct |
7 |
Correct |
84 ms |
70988 KB |
Output is correct |
8 |
Correct |
86 ms |
70988 KB |
Output is correct |
9 |
Correct |
81 ms |
70988 KB |
Output is correct |
10 |
Correct |
85 ms |
70872 KB |
Output is correct |
11 |
Correct |
93 ms |
70932 KB |
Output is correct |
12 |
Correct |
86 ms |
70932 KB |
Output is correct |
13 |
Correct |
87 ms |
70948 KB |
Output is correct |
14 |
Correct |
99 ms |
72628 KB |
Output is correct |
15 |
Correct |
143 ms |
74500 KB |
Output is correct |
16 |
Correct |
148 ms |
76120 KB |
Output is correct |
17 |
Correct |
187 ms |
77896 KB |
Output is correct |
18 |
Correct |
199 ms |
78264 KB |
Output is correct |
19 |
Correct |
196 ms |
77848 KB |
Output is correct |
20 |
Correct |
206 ms |
77840 KB |
Output is correct |
21 |
Correct |
172 ms |
77808 KB |
Output is correct |
22 |
Correct |
150 ms |
75768 KB |
Output is correct |
23 |
Correct |
134 ms |
74740 KB |
Output is correct |
24 |
Correct |
127 ms |
73984 KB |
Output is correct |
25 |
Correct |
149 ms |
76320 KB |
Output is correct |
26 |
Correct |
149 ms |
75496 KB |
Output is correct |
27 |
Correct |
158 ms |
75988 KB |
Output is correct |
28 |
Correct |
155 ms |
75848 KB |
Output is correct |
29 |
Correct |
169 ms |
76688 KB |
Output is correct |
30 |
Correct |
390 ms |
84528 KB |
Output is correct |
31 |
Correct |
489 ms |
89636 KB |
Output is correct |
32 |
Correct |
707 ms |
95940 KB |
Output is correct |
33 |
Runtime error |
977 ms |
215084 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |