/*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(0, MOD);
vi qr(K);
vpi suInd(K);
for (int i = 0; i < K; ++i) {
cin >> qr[i];
inds.add(qr[i], i);
suInd[i] = {qr[i], 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 |
4 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
7 ms |
4608 KB |
Output is correct |
4 |
Correct |
7 ms |
4608 KB |
Output is correct |
5 |
Correct |
11 ms |
4608 KB |
Output is correct |
6 |
Correct |
7 ms |
4608 KB |
Output is correct |
7 |
Correct |
6 ms |
4608 KB |
Output is correct |
8 |
Correct |
10 ms |
4352 KB |
Output is correct |
9 |
Correct |
5 ms |
1536 KB |
Output is correct |
10 |
Correct |
1 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 |
7 ms |
3200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
7 ms |
4608 KB |
Output is correct |
4 |
Correct |
7 ms |
4608 KB |
Output is correct |
5 |
Correct |
11 ms |
4608 KB |
Output is correct |
6 |
Correct |
7 ms |
4608 KB |
Output is correct |
7 |
Correct |
6 ms |
4608 KB |
Output is correct |
8 |
Correct |
10 ms |
4352 KB |
Output is correct |
9 |
Correct |
5 ms |
1536 KB |
Output is correct |
10 |
Correct |
1 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 |
7 ms |
3200 KB |
Output is correct |
14 |
Correct |
64 ms |
35212 KB |
Output is correct |
15 |
Correct |
119 ms |
65400 KB |
Output is correct |
16 |
Correct |
174 ms |
93816 KB |
Output is correct |
17 |
Correct |
226 ms |
121208 KB |
Output is correct |
18 |
Correct |
254 ms |
121336 KB |
Output is correct |
19 |
Correct |
249 ms |
120824 KB |
Output is correct |
20 |
Correct |
225 ms |
121076 KB |
Output is correct |
21 |
Correct |
221 ms |
112120 KB |
Output is correct |
22 |
Correct |
54 ms |
9464 KB |
Output is correct |
23 |
Correct |
58 ms |
7672 KB |
Output is correct |
24 |
Correct |
50 ms |
6264 KB |
Output is correct |
25 |
Correct |
56 ms |
11384 KB |
Output is correct |
26 |
Correct |
127 ms |
68864 KB |
Output is correct |
27 |
Correct |
142 ms |
76536 KB |
Output is correct |
28 |
Correct |
149 ms |
73400 KB |
Output is correct |
29 |
Correct |
173 ms |
99064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1664 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
7 ms |
4608 KB |
Output is correct |
4 |
Correct |
7 ms |
4608 KB |
Output is correct |
5 |
Correct |
11 ms |
4608 KB |
Output is correct |
6 |
Correct |
7 ms |
4608 KB |
Output is correct |
7 |
Correct |
6 ms |
4608 KB |
Output is correct |
8 |
Correct |
10 ms |
4352 KB |
Output is correct |
9 |
Correct |
5 ms |
1536 KB |
Output is correct |
10 |
Correct |
1 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 |
7 ms |
3200 KB |
Output is correct |
14 |
Correct |
64 ms |
35212 KB |
Output is correct |
15 |
Correct |
119 ms |
65400 KB |
Output is correct |
16 |
Correct |
174 ms |
93816 KB |
Output is correct |
17 |
Correct |
226 ms |
121208 KB |
Output is correct |
18 |
Correct |
254 ms |
121336 KB |
Output is correct |
19 |
Correct |
249 ms |
120824 KB |
Output is correct |
20 |
Correct |
225 ms |
121076 KB |
Output is correct |
21 |
Correct |
221 ms |
112120 KB |
Output is correct |
22 |
Correct |
54 ms |
9464 KB |
Output is correct |
23 |
Correct |
58 ms |
7672 KB |
Output is correct |
24 |
Correct |
50 ms |
6264 KB |
Output is correct |
25 |
Correct |
56 ms |
11384 KB |
Output is correct |
26 |
Correct |
127 ms |
68864 KB |
Output is correct |
27 |
Correct |
142 ms |
76536 KB |
Output is correct |
28 |
Correct |
149 ms |
73400 KB |
Output is correct |
29 |
Correct |
173 ms |
99064 KB |
Output is correct |
30 |
Correct |
434 ms |
152352 KB |
Output is correct |
31 |
Correct |
600 ms |
230944 KB |
Output is correct |
32 |
Runtime error |
678 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
33 |
Halted |
0 ms |
0 KB |
- |