#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 2e5 + 69;
struct FenwickTree{
int n;
vector<int> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1, 0);
}
void update(int i, int v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
};
struct SegmentTree{
int n;
vector<int> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 2 + 2, 0);
}
void update(int i, int v){
i += n; if (!maximize(a[i], v)) return;
while(i > 1){
i >>= 1;
a[i] = max(a[i * 2], a[i * 2 + 1]);
}
}
int get(int l, int r){
l += n; r += n + 1;
int ans = 0;
while(l < r){
if (l & 1) maximize(ans, a[l++]);
if (r & 1) maximize(ans, a[--r]);
l >>= 1; r >>= 1;
}
return ans;
}
};
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
int n, k; cin >> n >> k;
vector<pair<int, int>> a(n);
for(int i = 0; i<n; ++i) cin >> a[i].first >> a[i].second;
vector<int> query(k+1);
for(int i = 1; i<=k; ++i) cin >> query[i];
vector<int> X = query;
for(pair<int, int> i: a) {
X.push_back(i.first);
X.push_back(i.second);
}
remove_dup(X);
int m = X.size() - 1;
SegmentTree st(m);
for(int i = 1; i<=k; ++i){
int idx = lower_bound(ALL(X), query[i]) - X.begin();
st.update(idx, i);
}
vector<int> val(n);
vector<vector<int>> createQuery(k+1);
for(int i= 0; i<n; ++i){
int l = a[i].first, r = a[i].second;
if (l > r) swap(l, r);
l = lower_bound(ALL(X), l) - X.begin();
r = lower_bound(ALL(X), r) - X.begin() - 1;
val[i] = st.get(l, r);
createQuery[val[i]].push_back(i);
}
FenwickTree bit(m);
for(int i = k; i>=0; --i){
for(int j: createQuery[i]){
int l = lower_bound(ALL(X), max(a[j].first, a[j].second)) - X.begin();
int cnt = bit.get(m) - bit.get(l-1);
if (i > 0 && a[j].first < a[j].second){
swap(a[j].first, a[j].second);
}
if (cnt % 2) swap(a[j].first, a[j].second);
}
if (i == 0) break;
int idx = lower_bound(ALL(X), query[i]) - X.begin();
bit.update(idx, 1);
}
ll sum = 0;
for(pair<int, int> i: a) sum += i.first;
cout << sum << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
10 ms |
1628 KB |
Output is correct |
15 |
Correct |
23 ms |
2840 KB |
Output is correct |
16 |
Correct |
33 ms |
3884 KB |
Output is correct |
17 |
Correct |
50 ms |
5316 KB |
Output is correct |
18 |
Correct |
46 ms |
5096 KB |
Output is correct |
19 |
Correct |
48 ms |
5240 KB |
Output is correct |
20 |
Correct |
47 ms |
5184 KB |
Output is correct |
21 |
Correct |
46 ms |
5268 KB |
Output is correct |
22 |
Correct |
35 ms |
4212 KB |
Output is correct |
23 |
Correct |
30 ms |
3936 KB |
Output is correct |
24 |
Correct |
33 ms |
3892 KB |
Output is correct |
25 |
Correct |
41 ms |
4736 KB |
Output is correct |
26 |
Correct |
33 ms |
4480 KB |
Output is correct |
27 |
Correct |
38 ms |
4744 KB |
Output is correct |
28 |
Correct |
41 ms |
4656 KB |
Output is correct |
29 |
Correct |
45 ms |
5152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
10 ms |
1628 KB |
Output is correct |
15 |
Correct |
23 ms |
2840 KB |
Output is correct |
16 |
Correct |
33 ms |
3884 KB |
Output is correct |
17 |
Correct |
50 ms |
5316 KB |
Output is correct |
18 |
Correct |
46 ms |
5096 KB |
Output is correct |
19 |
Correct |
48 ms |
5240 KB |
Output is correct |
20 |
Correct |
47 ms |
5184 KB |
Output is correct |
21 |
Correct |
46 ms |
5268 KB |
Output is correct |
22 |
Correct |
35 ms |
4212 KB |
Output is correct |
23 |
Correct |
30 ms |
3936 KB |
Output is correct |
24 |
Correct |
33 ms |
3892 KB |
Output is correct |
25 |
Correct |
41 ms |
4736 KB |
Output is correct |
26 |
Correct |
33 ms |
4480 KB |
Output is correct |
27 |
Correct |
38 ms |
4744 KB |
Output is correct |
28 |
Correct |
41 ms |
4656 KB |
Output is correct |
29 |
Correct |
45 ms |
5152 KB |
Output is correct |
30 |
Correct |
97 ms |
11584 KB |
Output is correct |
31 |
Correct |
151 ms |
14400 KB |
Output is correct |
32 |
Correct |
191 ms |
17704 KB |
Output is correct |
33 |
Correct |
290 ms |
24532 KB |
Output is correct |
34 |
Correct |
88 ms |
10820 KB |
Output is correct |
35 |
Correct |
276 ms |
24616 KB |
Output is correct |
36 |
Correct |
292 ms |
24628 KB |
Output is correct |
37 |
Correct |
295 ms |
24380 KB |
Output is correct |
38 |
Correct |
273 ms |
24616 KB |
Output is correct |
39 |
Correct |
257 ms |
24616 KB |
Output is correct |
40 |
Correct |
272 ms |
25124 KB |
Output is correct |
41 |
Correct |
264 ms |
24584 KB |
Output is correct |
42 |
Correct |
263 ms |
24516 KB |
Output is correct |
43 |
Correct |
172 ms |
24616 KB |
Output is correct |
44 |
Correct |
176 ms |
24620 KB |
Output is correct |
45 |
Correct |
174 ms |
24392 KB |
Output is correct |
46 |
Correct |
175 ms |
19708 KB |
Output is correct |
47 |
Correct |
173 ms |
18216 KB |
Output is correct |
48 |
Correct |
208 ms |
22332 KB |
Output is correct |
49 |
Correct |
214 ms |
22308 KB |
Output is correct |