#include <bits/stdc++.h>
#include "bubblesort2.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 BLOCK = 2000;
const int INF = 1e9 + 69;
int di(int x, int y){
return x / y + (x % y > 0);
}
struct FenwickTree{
int n;
vector<int> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
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;
}
};
vector<int> get_inverse(vector<int> A){
int n = A.size();
vector<int> B = A;
remove_dup(B);
FenwickTree bit(B.size());
vector<int> ans(n);
for(int i = 0; i<n; ++i){
int idx = lower_bound(ALL(B), A[i]) - B.begin() + 1;
ans[i] = i - bit.get(idx);
bit.update(idx, 1);
}
return ans;
}
struct SegmentTree{
int n, h;
array<int, BLOCK * 2 + 2> t, d;
SegmentTree(int _n){
n = _n;
h = logOf(n);
for(int i = 1; i <= n * 2 + 1; ++i) t[i] = d[i] = 0;
}
void build_tree(array<int, BLOCK + 1> b){
for(int i = 1; i<=n*2+1; ++i) t[i] = d[i] = 0;;
for(int i = 0; i < n; ++i) t[n + 1 + i] = b[i];
for(int i = n; i>=1; --i) t[i] = max(t[i*2], t[i*2+1]);
}
void apply(int p, int value) {
t[p] += value;
if (p < n) d[p] += value;
}
void build(int p) {
while (p > 1) p >>= 1, t[p] = max(t[p<<1], t[p<<1|1]) + d[p];
}
void push(int p) {
for (int s = h; s > 0; --s) {
int i = p >> s;
if (d[i] != 0) {
apply(i<<1, d[i]);
apply(i<<1|1, d[i]);
d[i] = 0;
}
}
}
void update(int l, int r, int value) {
l++; r++;
l += n, r += n+1;
int l0 = l, r0 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l&1) apply(l++, value);
if (r&1) apply(--r, value);
}
build(l0);
build(r0 - 1);
}
int get() {
int l = 1, r = n;
l += n, r += n+1;
push(l);
push(r - 1);
int res = -INF;
for (; l < r; l >>= 1, r >>= 1) {
if (l&1) res = max(res, t[l++]);
if (r&1) res = max(t[--r], res);
}
return res;
}
};
vector<int> countScans(vector<int> a,vector<int> X,vector<int> V){
int n = a.size(), q = X.size();
int b_n = di(n, BLOCK);
vector<vector<pair<int, int>>> stair(b_n);
vector<SegmentTree> sigma;
vector<array<int, BLOCK + 1>> val(b_n), pref(b_n);
vector<int> cringe = get_inverse(a);
for(int i = 0; i < n; ++i){
stair[i / BLOCK].push_back({a[i],i});
}
for(int i = 0; i < b_n; ++i) sort(ALL(stair[i]));
for(int i = 0; i < b_n; ++i){
sigma.push_back(SegmentTree(stair[i].size()));
for(int j = 0; j <= BLOCK; ++j) val[i][j] = pref[i][j] = 0;
for(int k = 0; k < stair[i].size(); ++k){
int cur = cringe[stair[i][k].second];
sigma[i].update(k, k, cur);
val[i][k] = cur;
}
}
vector<int> answer(q);
for(int cur_query = 0; cur_query < q; ++cur_query){
// cout << "Query: " << cur_query << endl;
int u = X[cur_query], v = V[cur_query];
int cur_b = u / BLOCK;
for(int i = cur_b + 1; i < b_n; ++i){
int idx1 = lower_bound(ALL(stair[i]), make_pair(a[u], -1)) - stair[i].begin() - 1;
int idx2 = lower_bound(ALL(stair[i]), make_pair(v, -1)) - stair[i].begin() - 1;
if (idx1 != idx2){
if (idx1 < idx2) sigma[i].update(idx1 + 1, idx2, 1);
else sigma[i].update(idx2 + 1, idx1, -1);
pref[i][idx1 + 1]++;
pref[i][idx2 + 1]--;
}
}
int idx = lower_bound(ALL(stair[cur_b]), make_pair(a[u], u)) - stair[cur_b].begin();
array<int, BLOCK + 1> cur_sigma = val[cur_b];
for(int j = 1; j <= BLOCK; ++j) pref[cur_b][j] += pref[cur_b][j-1];
for(int j = 0; j <= BLOCK; ++j){
cur_sigma[j] += pref[cur_b][j];
pref[cur_b][j] = 0;
}
a[u] = v;
stair[cur_b][idx] = make_pair(a[u], u);
while (idx + 1 < stair[cur_b].size() && stair[cur_b][idx] > stair[cur_b][idx + 1]){
if (stair[cur_b][idx].second < stair[cur_b][idx + 1].second) cur_sigma[idx + 1]++;
swap(stair[cur_b][idx], stair[cur_b][idx + 1]);
swap(cur_sigma[idx], cur_sigma[idx + 1]);
idx++;
}
while (idx > 0 && stair[cur_b][idx - 1] > stair[cur_b][idx]){
if (stair[cur_b][idx].second < stair[cur_b][idx - 1].second) cur_sigma[idx - 1]--;
swap(stair[cur_b][idx], stair[cur_b][idx - 1]);
swap(cur_sigma[idx], cur_sigma[idx - 1]);
idx--;
}
cur_sigma[idx] = 0;
for(int j = 0; j < cur_b; ++j){
int _idx = upper_bound(ALL(stair[j]), stair[cur_b][idx]) - stair[j].begin();
cur_sigma[idx] += stair[j].size() - _idx;
}
for(int j = idx + 1; j < stair[cur_b].size(); ++j) if (stair[cur_b][j].second < stair[cur_b][idx].second)
cur_sigma[idx]++;
sigma[cur_b].build_tree(cur_sigma);
val[cur_b] = cur_sigma;
int ans = 0;
for(int i = 0; i < b_n; ++i) maximize(ans, sigma[i].get());
answer[cur_query] = ans;
// for(int i = 0; i<b_n; ++i){
// vector<int> sig = sigma[i].get_tree();
// printArr(sig, " ", "");
// }
// cout << "\n";
// if (cur_query % 10000 == 0) {
// cerr << "current query: " << cur_query << "\n";
// cerr.flush();
// }
}
return answer;
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clock_t start = clock();
// int n, q; cin >> n >> q;
// vector<int> a(n);
// for(int i = 0; i<n; ++i) cin >> a[i];
// vector<int> x(q), v(q);
// for(int i = 0; i<q; ++i) cin >> x[i] >> v[i];
// printArr(countScans(a, x, v));
// cerr << "Time elapsed: " << clock() - start << "ms!\n";
// return 0;
// }
Compilation message
bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:184:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
184 | for(int k = 0; k < stair[i].size(); ++k){
| ~~^~~~~~~~~~~~~~~~~
bubblesort2.cpp:223:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
223 | while (idx + 1 < stair[cur_b].size() && stair[cur_b][idx] > stair[cur_b][idx + 1]){
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
bubblesort2.cpp:240:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
240 | for(int j = idx + 1; j < stair[cur_b].size(); ++j) if (stair[cur_b][j].second < stair[cur_b][idx].second)
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
448 KB |
Output is correct |
3 |
Correct |
29 ms |
452 KB |
Output is correct |
4 |
Correct |
25 ms |
604 KB |
Output is correct |
5 |
Correct |
23 ms |
604 KB |
Output is correct |
6 |
Correct |
19 ms |
604 KB |
Output is correct |
7 |
Correct |
20 ms |
456 KB |
Output is correct |
8 |
Correct |
21 ms |
604 KB |
Output is correct |
9 |
Correct |
22 ms |
452 KB |
Output is correct |
10 |
Correct |
21 ms |
604 KB |
Output is correct |
11 |
Correct |
21 ms |
600 KB |
Output is correct |
12 |
Correct |
21 ms |
648 KB |
Output is correct |
13 |
Correct |
21 ms |
604 KB |
Output is correct |
14 |
Correct |
30 ms |
600 KB |
Output is correct |
15 |
Correct |
24 ms |
652 KB |
Output is correct |
16 |
Correct |
21 ms |
604 KB |
Output is correct |
17 |
Correct |
27 ms |
856 KB |
Output is correct |
18 |
Correct |
21 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
448 KB |
Output is correct |
3 |
Correct |
29 ms |
452 KB |
Output is correct |
4 |
Correct |
25 ms |
604 KB |
Output is correct |
5 |
Correct |
23 ms |
604 KB |
Output is correct |
6 |
Correct |
19 ms |
604 KB |
Output is correct |
7 |
Correct |
20 ms |
456 KB |
Output is correct |
8 |
Correct |
21 ms |
604 KB |
Output is correct |
9 |
Correct |
22 ms |
452 KB |
Output is correct |
10 |
Correct |
21 ms |
604 KB |
Output is correct |
11 |
Correct |
21 ms |
600 KB |
Output is correct |
12 |
Correct |
21 ms |
648 KB |
Output is correct |
13 |
Correct |
21 ms |
604 KB |
Output is correct |
14 |
Correct |
30 ms |
600 KB |
Output is correct |
15 |
Correct |
24 ms |
652 KB |
Output is correct |
16 |
Correct |
21 ms |
604 KB |
Output is correct |
17 |
Correct |
27 ms |
856 KB |
Output is correct |
18 |
Correct |
21 ms |
604 KB |
Output is correct |
19 |
Correct |
88 ms |
1112 KB |
Output is correct |
20 |
Correct |
109 ms |
1112 KB |
Output is correct |
21 |
Correct |
90 ms |
1096 KB |
Output is correct |
22 |
Correct |
106 ms |
1244 KB |
Output is correct |
23 |
Correct |
101 ms |
1208 KB |
Output is correct |
24 |
Correct |
94 ms |
1116 KB |
Output is correct |
25 |
Correct |
101 ms |
1096 KB |
Output is correct |
26 |
Correct |
90 ms |
1160 KB |
Output is correct |
27 |
Correct |
88 ms |
1156 KB |
Output is correct |
28 |
Correct |
86 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2048 KB |
Output is correct |
2 |
Correct |
362 ms |
3276 KB |
Output is correct |
3 |
Correct |
795 ms |
4300 KB |
Output is correct |
4 |
Correct |
800 ms |
4312 KB |
Output is correct |
5 |
Correct |
749 ms |
4312 KB |
Output is correct |
6 |
Correct |
777 ms |
4308 KB |
Output is correct |
7 |
Correct |
739 ms |
4308 KB |
Output is correct |
8 |
Correct |
693 ms |
4300 KB |
Output is correct |
9 |
Correct |
691 ms |
4312 KB |
Output is correct |
10 |
Correct |
596 ms |
4568 KB |
Output is correct |
11 |
Correct |
529 ms |
4564 KB |
Output is correct |
12 |
Correct |
513 ms |
4464 KB |
Output is correct |
13 |
Correct |
512 ms |
4348 KB |
Output is correct |
14 |
Correct |
544 ms |
4348 KB |
Output is correct |
15 |
Correct |
493 ms |
4564 KB |
Output is correct |
16 |
Correct |
503 ms |
4568 KB |
Output is correct |
17 |
Correct |
528 ms |
4464 KB |
Output is correct |
18 |
Correct |
500 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
448 KB |
Output is correct |
3 |
Correct |
29 ms |
452 KB |
Output is correct |
4 |
Correct |
25 ms |
604 KB |
Output is correct |
5 |
Correct |
23 ms |
604 KB |
Output is correct |
6 |
Correct |
19 ms |
604 KB |
Output is correct |
7 |
Correct |
20 ms |
456 KB |
Output is correct |
8 |
Correct |
21 ms |
604 KB |
Output is correct |
9 |
Correct |
22 ms |
452 KB |
Output is correct |
10 |
Correct |
21 ms |
604 KB |
Output is correct |
11 |
Correct |
21 ms |
600 KB |
Output is correct |
12 |
Correct |
21 ms |
648 KB |
Output is correct |
13 |
Correct |
21 ms |
604 KB |
Output is correct |
14 |
Correct |
30 ms |
600 KB |
Output is correct |
15 |
Correct |
24 ms |
652 KB |
Output is correct |
16 |
Correct |
21 ms |
604 KB |
Output is correct |
17 |
Correct |
27 ms |
856 KB |
Output is correct |
18 |
Correct |
21 ms |
604 KB |
Output is correct |
19 |
Correct |
88 ms |
1112 KB |
Output is correct |
20 |
Correct |
109 ms |
1112 KB |
Output is correct |
21 |
Correct |
90 ms |
1096 KB |
Output is correct |
22 |
Correct |
106 ms |
1244 KB |
Output is correct |
23 |
Correct |
101 ms |
1208 KB |
Output is correct |
24 |
Correct |
94 ms |
1116 KB |
Output is correct |
25 |
Correct |
101 ms |
1096 KB |
Output is correct |
26 |
Correct |
90 ms |
1160 KB |
Output is correct |
27 |
Correct |
88 ms |
1156 KB |
Output is correct |
28 |
Correct |
86 ms |
1116 KB |
Output is correct |
29 |
Correct |
35 ms |
2048 KB |
Output is correct |
30 |
Correct |
362 ms |
3276 KB |
Output is correct |
31 |
Correct |
795 ms |
4300 KB |
Output is correct |
32 |
Correct |
800 ms |
4312 KB |
Output is correct |
33 |
Correct |
749 ms |
4312 KB |
Output is correct |
34 |
Correct |
777 ms |
4308 KB |
Output is correct |
35 |
Correct |
739 ms |
4308 KB |
Output is correct |
36 |
Correct |
693 ms |
4300 KB |
Output is correct |
37 |
Correct |
691 ms |
4312 KB |
Output is correct |
38 |
Correct |
596 ms |
4568 KB |
Output is correct |
39 |
Correct |
529 ms |
4564 KB |
Output is correct |
40 |
Correct |
513 ms |
4464 KB |
Output is correct |
41 |
Correct |
512 ms |
4348 KB |
Output is correct |
42 |
Correct |
544 ms |
4348 KB |
Output is correct |
43 |
Correct |
493 ms |
4564 KB |
Output is correct |
44 |
Correct |
503 ms |
4568 KB |
Output is correct |
45 |
Correct |
528 ms |
4464 KB |
Output is correct |
46 |
Correct |
500 ms |
4300 KB |
Output is correct |
47 |
Correct |
4317 ms |
17444 KB |
Output is correct |
48 |
Execution timed out |
9024 ms |
42440 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |