/*input
5 1
9 1
8 8
3 7
4 6
4 2
3
*/
#include <bits/stdc++.h>
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
struct FENWICK {
vi sk;
int N;
FENWICK(int n) : N(n) {
sk.resize(N + 1, 0);
}
void add(int i, int val) {
for (; i <= N; i += (i) & (-i))
sk[i] += val;
}
int get(int i) {
int ret = 0;
for (; i > 0; i -= (i) & (-i))
ret += sk[i];
return ret;
}
};
struct node {
int l, r;
int did;
node *left = nullptr;
node *right = nullptr;
node(int a, int b): l(a), r(b), did(-1) { }
inline void add_left() { if (!left) left = new node(l, (l + r) / 2); }
inline void add_right() { if (!right) right = new node((l + r) / 2 + 1, r); }
void add(int pos, int val) {
if (l == r) {
did = max(did, val);
return;
}
if (pos <= (l + r) / 2) {
add_left(); left->add(pos, val);
} else {
add_right(); right->add(pos, val);
}
did = max((left ? left->did : -1), (right ? right->did : -1));
}
int get(int a, int b) {
if (r < a or b < l) return -1;
else if (a <= l and r <= b) return did;
else {
add_left();
add_right();
return max(left->get(a, b), right->get(a, b));
}
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, K;
cin >> N >> K;
vpi sk(N);
for (int i = 0; i < N; ++i)
{
cin >> sk[i].x >> sk[i].y;
}
node inds(1, (int)1e9);
vpi suInd(K);
for (int i = 0; i < K; ++i) {
int a;
cin >> a;
inds.add(a, i);
suInd[i] = {a, i};
}
sort(suInd.rbegin(), suInd.rend());
sort(sk.begin(), sk.end(), [](const pi & a, const pi & b) {
return max(a.x, a.y) > max(b.x, b.y);
});
FENWICK fen(K);
ll ats = 0;
int j = 0;
for (int i = 0; i < N; ++i)
{
int a = max(sk[i].x, sk[i].y);
int b = min(sk[i].x, sk[i].y);
int k = inds.get(b, a - 1);
while (j < (int)suInd.size() and suInd[j].x >= a) {
fen.add(suInd[j].y + 1, 1);
j++;
}
int kiek;
if (k == -1) {
kiek = fen.get(K);
if (kiek & 1) ats += (ll)sk[i].y;
else ats += (ll)sk[i].x;
} else {
kiek = fen.get(K) - fen.get(k + 1);
if (kiek & 1) ats += (ll)b;
else ats += (ll)a;
}
// printf("i = %d, a = %d, b = %d, k = %d, kiek = %d\n", i, a, b, k, kiek);
}
printf("%lld\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
4608 KB |
Output is correct |
4 |
Correct |
7 ms |
4608 KB |
Output is correct |
5 |
Correct |
6 ms |
4608 KB |
Output is correct |
6 |
Correct |
7 ms |
4608 KB |
Output is correct |
7 |
Correct |
7 ms |
4608 KB |
Output is correct |
8 |
Correct |
6 ms |
4352 KB |
Output is correct |
9 |
Correct |
3 ms |
1536 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
6 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
4608 KB |
Output is correct |
4 |
Correct |
7 ms |
4608 KB |
Output is correct |
5 |
Correct |
6 ms |
4608 KB |
Output is correct |
6 |
Correct |
7 ms |
4608 KB |
Output is correct |
7 |
Correct |
7 ms |
4608 KB |
Output is correct |
8 |
Correct |
6 ms |
4352 KB |
Output is correct |
9 |
Correct |
3 ms |
1536 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
6 ms |
3072 KB |
Output is correct |
14 |
Correct |
61 ms |
34876 KB |
Output is correct |
15 |
Correct |
122 ms |
64760 KB |
Output is correct |
16 |
Correct |
182 ms |
92920 KB |
Output is correct |
17 |
Correct |
239 ms |
119800 KB |
Output is correct |
18 |
Correct |
239 ms |
119800 KB |
Output is correct |
19 |
Correct |
253 ms |
119444 KB |
Output is correct |
20 |
Correct |
229 ms |
119800 KB |
Output is correct |
21 |
Correct |
215 ms |
110840 KB |
Output is correct |
22 |
Correct |
57 ms |
8696 KB |
Output is correct |
23 |
Correct |
52 ms |
6776 KB |
Output is correct |
24 |
Correct |
57 ms |
5332 KB |
Output is correct |
25 |
Correct |
62 ms |
10488 KB |
Output is correct |
26 |
Correct |
132 ms |
67448 KB |
Output is correct |
27 |
Correct |
157 ms |
75256 KB |
Output is correct |
28 |
Correct |
150 ms |
71928 KB |
Output is correct |
29 |
Correct |
186 ms |
97656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
4608 KB |
Output is correct |
4 |
Correct |
7 ms |
4608 KB |
Output is correct |
5 |
Correct |
6 ms |
4608 KB |
Output is correct |
6 |
Correct |
7 ms |
4608 KB |
Output is correct |
7 |
Correct |
7 ms |
4608 KB |
Output is correct |
8 |
Correct |
6 ms |
4352 KB |
Output is correct |
9 |
Correct |
3 ms |
1536 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
6 ms |
3072 KB |
Output is correct |
14 |
Correct |
61 ms |
34876 KB |
Output is correct |
15 |
Correct |
122 ms |
64760 KB |
Output is correct |
16 |
Correct |
182 ms |
92920 KB |
Output is correct |
17 |
Correct |
239 ms |
119800 KB |
Output is correct |
18 |
Correct |
239 ms |
119800 KB |
Output is correct |
19 |
Correct |
253 ms |
119444 KB |
Output is correct |
20 |
Correct |
229 ms |
119800 KB |
Output is correct |
21 |
Correct |
215 ms |
110840 KB |
Output is correct |
22 |
Correct |
57 ms |
8696 KB |
Output is correct |
23 |
Correct |
52 ms |
6776 KB |
Output is correct |
24 |
Correct |
57 ms |
5332 KB |
Output is correct |
25 |
Correct |
62 ms |
10488 KB |
Output is correct |
26 |
Correct |
132 ms |
67448 KB |
Output is correct |
27 |
Correct |
157 ms |
75256 KB |
Output is correct |
28 |
Correct |
150 ms |
71928 KB |
Output is correct |
29 |
Correct |
186 ms |
97656 KB |
Output is correct |
30 |
Correct |
436 ms |
149496 KB |
Output is correct |
31 |
Correct |
643 ms |
227192 KB |
Output is correct |
32 |
Runtime error |
657 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
33 |
Halted |
0 ms |
0 KB |
- |