#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, a[N], b[N], c[N], last[3 * N][21], lg[3 * N], bit[3 * N];
vector<int> Value, Query[3 * N];
long long ans;
void upd(int pos) {
for(pos; pos > 0; pos -= pos & -pos) bit[pos]++;
}
int get(int pos) {
int res = 0;
for(pos; pos < 3 * N; pos += pos & -pos) res += bit[pos];
return res;
}
void getint(int &x) {
x = 0;
char c = getchar();
while('0' > c || c > '9') c = getchar();
while('0' <= c && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
}
int main() {
ios::sync_with_stdio(false);
getint(n); getint(m);
for(int i = 0; i < 3 * N; ++i) lg[i] = log2(i);
for(int i = 1; i <= n; ++i) getint(a[i]), getint(b[i]), Value.push_back(a[i]), Value.push_back(b[i]);
for(int i = 1; i <= m; ++i) getint(c[i]), Value.push_back(c[i]);
Value.push_back(0);
sort(Value.begin(), Value.end());
Value.resize(distance(Value.begin(), unique(Value.begin(), Value.end())));
for(int i = 1; i <= n; ++i)
a[i] = lower_bound(Value.begin(), Value.end(), a[i]) - Value.begin() + 1,
b[i] = lower_bound(Value.begin(), Value.end(), b[i]) - Value.begin() + 1;
for(int i = 1; i <= m; ++i)
c[i] = lower_bound(Value.begin(), Value.end(), c[i]) - Value.begin() + 1;
for(int i = m; i >= 1; --i) if(!last[0][c[i]]) last[0][c[i]] = i;
for(int i = 1; i <= 20; ++i) for(int j = 1; j < 3 * N; ++j)
last[i][j] = max(last[i - 1][j], last[i - 1][j + (1 << (i - 1))]);
for(int i = 1; i <= n; ++i) {
if(a[i] == b[i]) {
ans += a[i];
continue;
}
int mn = min(a[i], b[i]), mx = max(a[i], b[i]) - 1;
int pos = max(last[lg[mx - mn + 1]][mn], last[lg[mx - mn + 1]][mx - (1 << lg[mx - mn + 1]) + 1]);
Query[pos].push_back(i);
}
for(int i = m; i >= 0; --i) {
for(auto j: Query[i]) {
if(i && a[j] < b[j]) swap(a[j], b[j]);
if(get(a[j]) & 1) ans += Value[b[j] - 1];
else ans += Value[a[j] - 1];
}
upd(c[i]);
}
printf("%lld", ans);
}
Compilation message
fortune_telling2.cpp: In function 'void upd(int)':
fortune_telling2.cpp:11:9: warning: statement has no effect [-Wunused-value]
for(pos; pos > 0; pos -= pos & -pos) bit[pos]++;
^
fortune_telling2.cpp: In function 'int get(int)':
fortune_telling2.cpp:16:9: warning: statement has no effect [-Wunused-value]
for(pos; pos < 3 * N; pos += pos & -pos) res += bit[pos];
^
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:43:68: warning: iteration 20u invokes undefined behavior [-Waggressive-loop-optimizations]
last[i][j] = max(last[i - 1][j], last[i - 1][j + (1 << (i - 1))]);
^
fortune_telling2.cpp:42:48: note: containing loop
for(int i = 1; i <= 20; ++i) for(int j = 1; j < 3 * N; ++j)
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
72504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
72668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
119 ms |
74084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |