#include "bubblesort2.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 500'032;
const int Sa = 4096, Sq = 4096;
int cnt_gt[N];
int val[N];
int mx_qrange[Sq];
int added_to[N];
vector<int> ans;
int n, q, qx[N], qy[N];
int fen[N];
void fen_add(int i, int x)
{
++i;
while (i < N) {
fen[i] += x;
i += i & -i;
}
}
int fen_get(int r)
{
int ans = 0;
while (r > 0) {
ans += fen[r];
r -= r & -r;
}
return ans;
}
void init()
{
vector<int> cmp(val, val+n);
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
int mx = cmp.size() - 1;
Loop (i,0,n) {
int j = lower_bound(cmp.begin(), cmp.end(), val[i])
- cmp.begin();
cnt_gt[i] = fen_get(mx-j);
fen_add(mx-j, 1);
}
}
#define OPTIM __attribute__((optimize("O3,unroll-loops"),target("avx")))
OPTIM pii do_cnt_gt(int l, int r, int x)
{
int ans = 0;
int mx = INT_MIN;
Loop (i,l,r) {
ans += val[i] > x;
mx = cnt_gt[i] > mx? cnt_gt[i]: mx;
}
return {ans, mx};
}
OPTIM int do_sub_inrange(int l, int r, int xl, int xr)
{
int mx = INT_MIN;
Loop (i,l,r) {
int cnt = cnt_gt[i];
int val = ::val[i];
cnt -= xl <= val && val < xr;
mx = cnt > mx? cnt: mx;
cnt_gt[i] = cnt;
}
return mx;
}
OPTIM int do_add_inrange(int l, int r, int xl, int xr)
{
int mx = INT_MIN;
Loop (i,l,r) {
int cnt = cnt_gt[i];
int val = ::val[i];
cnt += xl <= val && val < xr;
mx = cnt > mx? cnt: mx;
cnt_gt[i] = cnt;
}
return mx;
}
int apply(int al, int ar, int qi)
{
int i = qx[qi], x = qy[qi];
int mx = INT_MIN;
if (al < i) {
int cr = min(ar, i);
auto [tmp, new_mx] = do_cnt_gt(al, cr, x);
added_to[qi] += tmp;
mx = max(mx, new_mx);
}
if (i+1 < ar) {
int cl = max(al, i+1);
if (val[i] < x) {
auto tmp = do_add_inrange(cl, ar, val[i], x);
mx = max(mx, tmp);
} else {
auto tmp = do_sub_inrange(cl, ar, x, val[i]);
mx = max(mx, tmp);
}
}
if (al <= i && i < ar) {
cnt_gt[i] = added_to[qi];
mx = max(mx, cnt_gt[i]);
}
val[i] = x;
return mx;
}
void solve_qrange(int ql, int qr)
{
vector<int> ch;
Loop (i,ql,qr)
ch.push_back(val[qx[i]]);
memset(mx_qrange, 0, sizeof(mx_qrange));
for (int al = 0; al < n; al += Sa) {
int ar = min(al + Sa, n);
Loop (qi,ql,qr) {
int x = apply(al, ar, qi);
mx_qrange[qi-ql] = max(mx_qrange[qi-ql], x);
}
Loop (i,ql,qr)
val[qx[i]] = ch[i-ql];
}
Loop (i,ql,qr)
val[qx[i]] = qy[i];
Loop (i,ql,qr)
ans.push_back(mx_qrange[i-ql]);
}
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V)
{
n = A.size();
Loop (i,0,n)
val[i] = A[i];
q = X.size();
Loop (i,0,q) {
qx[i] = X[i];
qy[i] = V[i];
}
init();
for (int ql = 0; ql < q; ql += Sq) {
int qr = min(ql+Sq, q);
solve_qrange(ql, qr);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
12 ms |
724 KB |
Output is correct |
20 |
Correct |
15 ms |
772 KB |
Output is correct |
21 |
Correct |
15 ms |
852 KB |
Output is correct |
22 |
Correct |
16 ms |
772 KB |
Output is correct |
23 |
Correct |
17 ms |
768 KB |
Output is correct |
24 |
Correct |
15 ms |
772 KB |
Output is correct |
25 |
Correct |
14 ms |
852 KB |
Output is correct |
26 |
Correct |
14 ms |
852 KB |
Output is correct |
27 |
Correct |
14 ms |
852 KB |
Output is correct |
28 |
Correct |
13 ms |
776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
992 KB |
Output is correct |
2 |
Correct |
166 ms |
1912 KB |
Output is correct |
3 |
Correct |
403 ms |
2896 KB |
Output is correct |
4 |
Correct |
413 ms |
2956 KB |
Output is correct |
5 |
Correct |
396 ms |
2940 KB |
Output is correct |
6 |
Correct |
414 ms |
2932 KB |
Output is correct |
7 |
Correct |
413 ms |
2876 KB |
Output is correct |
8 |
Correct |
421 ms |
2876 KB |
Output is correct |
9 |
Correct |
413 ms |
2928 KB |
Output is correct |
10 |
Correct |
354 ms |
3068 KB |
Output is correct |
11 |
Correct |
345 ms |
3056 KB |
Output is correct |
12 |
Correct |
362 ms |
2928 KB |
Output is correct |
13 |
Correct |
317 ms |
2888 KB |
Output is correct |
14 |
Correct |
333 ms |
2844 KB |
Output is correct |
15 |
Correct |
307 ms |
2824 KB |
Output is correct |
16 |
Correct |
296 ms |
3120 KB |
Output is correct |
17 |
Correct |
283 ms |
2864 KB |
Output is correct |
18 |
Correct |
288 ms |
2824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
12 ms |
724 KB |
Output is correct |
20 |
Correct |
15 ms |
772 KB |
Output is correct |
21 |
Correct |
15 ms |
852 KB |
Output is correct |
22 |
Correct |
16 ms |
772 KB |
Output is correct |
23 |
Correct |
17 ms |
768 KB |
Output is correct |
24 |
Correct |
15 ms |
772 KB |
Output is correct |
25 |
Correct |
14 ms |
852 KB |
Output is correct |
26 |
Correct |
14 ms |
852 KB |
Output is correct |
27 |
Correct |
14 ms |
852 KB |
Output is correct |
28 |
Correct |
13 ms |
776 KB |
Output is correct |
29 |
Correct |
16 ms |
992 KB |
Output is correct |
30 |
Correct |
166 ms |
1912 KB |
Output is correct |
31 |
Correct |
403 ms |
2896 KB |
Output is correct |
32 |
Correct |
413 ms |
2956 KB |
Output is correct |
33 |
Correct |
396 ms |
2940 KB |
Output is correct |
34 |
Correct |
414 ms |
2932 KB |
Output is correct |
35 |
Correct |
413 ms |
2876 KB |
Output is correct |
36 |
Correct |
421 ms |
2876 KB |
Output is correct |
37 |
Correct |
413 ms |
2928 KB |
Output is correct |
38 |
Correct |
354 ms |
3068 KB |
Output is correct |
39 |
Correct |
345 ms |
3056 KB |
Output is correct |
40 |
Correct |
362 ms |
2928 KB |
Output is correct |
41 |
Correct |
317 ms |
2888 KB |
Output is correct |
42 |
Correct |
333 ms |
2844 KB |
Output is correct |
43 |
Correct |
307 ms |
2824 KB |
Output is correct |
44 |
Correct |
296 ms |
3120 KB |
Output is correct |
45 |
Correct |
283 ms |
2864 KB |
Output is correct |
46 |
Correct |
288 ms |
2824 KB |
Output is correct |
47 |
Correct |
3440 ms |
8988 KB |
Output is correct |
48 |
Execution timed out |
9014 ms |
21696 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |