#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 S = 2048;
const int N = 500'032 + S;
int seg_mx[N/S][S*2-1];
int seg_cnt[N/S][S*2-1];
int val[N], cnt[N], val_srt[N], srt_ind[N];
int n;
void add_seg(int l, int r, int x, int t, int i, int b, int e)
{
if (l <= b && e <= r) {
seg_cnt[t][i] += x;
seg_mx[t][i] += x;
return;
}
if (r <= b || e <= l)
return;
add_seg(l, r, x, t, 2*i+1, b, (b+e)/2);
add_seg(l, r, x, t, 2*i+2, (b+e)/2, e);
seg_mx[t][i] = max(seg_mx[t][2*i+1], seg_mx[t][2*i+2]);
seg_mx[t][i] += seg_cnt[t][i];
}
void build_seg(int t, int i, int b, int e)
{
if (e-b == 1) {
seg_cnt[t][i] = cnt[srt_ind[b]];
seg_mx[t][i] = seg_cnt[t][i];
return;
}
build_seg(t, 2*i+1, b, (b+e)/2);
build_seg(t, 2*i+2, (b+e)/2, e);
seg_cnt[t][i] = 0;
seg_mx[t][i] = max(seg_mx[t][2*i+1], seg_mx[t][2*i+2]);
}
void flush_seg(int t, int i, int b, int e)
{
if (e-b == 1) {
cnt[srt_ind[b]] = seg_cnt[t][i];
return;
}
seg_cnt[t][2*i+1] += seg_cnt[t][i];
seg_cnt[t][2*i+2] += seg_cnt[t][i];
seg_cnt[t][i] = 0;
flush_seg(t, 2*i+1, b, (b+e)/2);
flush_seg(t, 2*i+2, (b+e)/2, e);
}
int get_gt(int l, int x)
{
return S - ( upper_bound(val_srt + l, val_srt + l + S, x)
- (val_srt + l));
}
void up_range(int l, int xl, int xr, int val)
{
xl = lower_bound(val_srt + l, val_srt + l + S, xl) - val_srt;
xr = lower_bound(val_srt + l, val_srt + l + S, xr) - val_srt;
if (xl < xr)
add_seg(xl, xr, val, l/S, 0, l, l + S);
}
void mov(int i, int x)
{
int j = i - i%S;
for (; srt_ind[j] != i; ++j);
val_srt[j] = x;
while ((j+1)%S && val_srt[j] > val_srt[j+1]) {
swap(val_srt[j], val_srt[j+1]);
swap(srt_ind[j], srt_ind[j+1]);
++j;
}
while (j%S && val_srt[j] < val_srt[j-1]) {
swap(val_srt[j], val_srt[j-1]);
swap(srt_ind[j], srt_ind[j-1]);
--j;
}
}
void up(int i, int x)
{
int pre = val[i];
int cnt_gt = 0;
for (int l = 0; l < i - i%S; l += S)
cnt_gt += get_gt(l, x);
flush_seg(i/S, 0, i - i%S, i - i%S + S);
val[i] = x;
for (int j = i - i%S; j < i; ++j)
cnt_gt += val[j] > x;
cnt[i] = cnt_gt;
for (int j = i + 1; j < i - i%S + S; ++j)
cnt[j] += (x > val[j]) - (pre > val[j]);
mov(i, x);
build_seg(i/S, 0, i - i%S, i - i%S + S);
int val = pre < x? 1: -1;
int xl = pre < x? pre: x;
int xr = pre < x? x: pre;
for (int l = i - i%S + S; l < n; l += S)
up_range(l, xl, xr, val);
}
int get()
{
int mx = 0;
for (int l = 0; l < n; l += S)
mx = max(seg_mx[l/S][0], mx);
return mx;
}
void init(vector<int> A)
{
n = A.size();
fill(val, val+N, (int)1e9 + 10);
fill(val_srt, val_srt+N, (int)1e9 + 10);
iota(srt_ind, srt_ind+N, 0);
memset(seg_cnt, 0, sizeof(seg_cnt));
memset(seg_mx, 0, sizeof(seg_mx));
memset(cnt, 0, sizeof(cnt));
Loop (i,0,n)
up(i, A[i]);
}
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V)
{
init(A);
vector<int> ans;
Loop (i,0,X.size()) {
up(X[i], V[i]);
ans.push_back(get());
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
15956 KB |
Output is correct |
2 |
Correct |
84 ms |
16020 KB |
Output is correct |
3 |
Correct |
188 ms |
16052 KB |
Output is correct |
4 |
Correct |
189 ms |
16044 KB |
Output is correct |
5 |
Correct |
188 ms |
15956 KB |
Output is correct |
6 |
Correct |
200 ms |
16056 KB |
Output is correct |
7 |
Correct |
187 ms |
16056 KB |
Output is correct |
8 |
Correct |
196 ms |
16052 KB |
Output is correct |
9 |
Correct |
182 ms |
16044 KB |
Output is correct |
10 |
Correct |
199 ms |
16056 KB |
Output is correct |
11 |
Correct |
184 ms |
16052 KB |
Output is correct |
12 |
Correct |
196 ms |
16048 KB |
Output is correct |
13 |
Correct |
186 ms |
16052 KB |
Output is correct |
14 |
Correct |
197 ms |
16056 KB |
Output is correct |
15 |
Correct |
191 ms |
16052 KB |
Output is correct |
16 |
Correct |
187 ms |
16056 KB |
Output is correct |
17 |
Correct |
191 ms |
16048 KB |
Output is correct |
18 |
Correct |
191 ms |
16052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
15956 KB |
Output is correct |
2 |
Correct |
84 ms |
16020 KB |
Output is correct |
3 |
Correct |
188 ms |
16052 KB |
Output is correct |
4 |
Correct |
189 ms |
16044 KB |
Output is correct |
5 |
Correct |
188 ms |
15956 KB |
Output is correct |
6 |
Correct |
200 ms |
16056 KB |
Output is correct |
7 |
Correct |
187 ms |
16056 KB |
Output is correct |
8 |
Correct |
196 ms |
16052 KB |
Output is correct |
9 |
Correct |
182 ms |
16044 KB |
Output is correct |
10 |
Correct |
199 ms |
16056 KB |
Output is correct |
11 |
Correct |
184 ms |
16052 KB |
Output is correct |
12 |
Correct |
196 ms |
16048 KB |
Output is correct |
13 |
Correct |
186 ms |
16052 KB |
Output is correct |
14 |
Correct |
197 ms |
16056 KB |
Output is correct |
15 |
Correct |
191 ms |
16052 KB |
Output is correct |
16 |
Correct |
187 ms |
16056 KB |
Output is correct |
17 |
Correct |
191 ms |
16048 KB |
Output is correct |
18 |
Correct |
191 ms |
16052 KB |
Output is correct |
19 |
Correct |
677 ms |
16184 KB |
Output is correct |
20 |
Correct |
750 ms |
16468 KB |
Output is correct |
21 |
Correct |
748 ms |
16332 KB |
Output is correct |
22 |
Correct |
729 ms |
16232 KB |
Output is correct |
23 |
Correct |
728 ms |
16236 KB |
Output is correct |
24 |
Correct |
737 ms |
16216 KB |
Output is correct |
25 |
Correct |
732 ms |
16344 KB |
Output is correct |
26 |
Correct |
733 ms |
16220 KB |
Output is correct |
27 |
Correct |
762 ms |
16296 KB |
Output is correct |
28 |
Correct |
736 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1354 ms |
16344 KB |
Output is correct |
2 |
Correct |
3093 ms |
17000 KB |
Output is correct |
3 |
Correct |
4906 ms |
18096 KB |
Output is correct |
4 |
Correct |
4871 ms |
17976 KB |
Output is correct |
5 |
Correct |
4923 ms |
17792 KB |
Output is correct |
6 |
Correct |
4902 ms |
17800 KB |
Output is correct |
7 |
Correct |
4898 ms |
17884 KB |
Output is correct |
8 |
Correct |
4781 ms |
17664 KB |
Output is correct |
9 |
Correct |
4783 ms |
17944 KB |
Output is correct |
10 |
Correct |
4458 ms |
17720 KB |
Output is correct |
11 |
Correct |
4420 ms |
17820 KB |
Output is correct |
12 |
Correct |
4425 ms |
17796 KB |
Output is correct |
13 |
Correct |
4433 ms |
17656 KB |
Output is correct |
14 |
Correct |
4384 ms |
17800 KB |
Output is correct |
15 |
Correct |
4389 ms |
17668 KB |
Output is correct |
16 |
Correct |
4282 ms |
17568 KB |
Output is correct |
17 |
Correct |
4300 ms |
17820 KB |
Output is correct |
18 |
Correct |
4327 ms |
17764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
15956 KB |
Output is correct |
2 |
Correct |
84 ms |
16020 KB |
Output is correct |
3 |
Correct |
188 ms |
16052 KB |
Output is correct |
4 |
Correct |
189 ms |
16044 KB |
Output is correct |
5 |
Correct |
188 ms |
15956 KB |
Output is correct |
6 |
Correct |
200 ms |
16056 KB |
Output is correct |
7 |
Correct |
187 ms |
16056 KB |
Output is correct |
8 |
Correct |
196 ms |
16052 KB |
Output is correct |
9 |
Correct |
182 ms |
16044 KB |
Output is correct |
10 |
Correct |
199 ms |
16056 KB |
Output is correct |
11 |
Correct |
184 ms |
16052 KB |
Output is correct |
12 |
Correct |
196 ms |
16048 KB |
Output is correct |
13 |
Correct |
186 ms |
16052 KB |
Output is correct |
14 |
Correct |
197 ms |
16056 KB |
Output is correct |
15 |
Correct |
191 ms |
16052 KB |
Output is correct |
16 |
Correct |
187 ms |
16056 KB |
Output is correct |
17 |
Correct |
191 ms |
16048 KB |
Output is correct |
18 |
Correct |
191 ms |
16052 KB |
Output is correct |
19 |
Correct |
677 ms |
16184 KB |
Output is correct |
20 |
Correct |
750 ms |
16468 KB |
Output is correct |
21 |
Correct |
748 ms |
16332 KB |
Output is correct |
22 |
Correct |
729 ms |
16232 KB |
Output is correct |
23 |
Correct |
728 ms |
16236 KB |
Output is correct |
24 |
Correct |
737 ms |
16216 KB |
Output is correct |
25 |
Correct |
732 ms |
16344 KB |
Output is correct |
26 |
Correct |
733 ms |
16220 KB |
Output is correct |
27 |
Correct |
762 ms |
16296 KB |
Output is correct |
28 |
Correct |
736 ms |
16212 KB |
Output is correct |
29 |
Correct |
1354 ms |
16344 KB |
Output is correct |
30 |
Correct |
3093 ms |
17000 KB |
Output is correct |
31 |
Correct |
4906 ms |
18096 KB |
Output is correct |
32 |
Correct |
4871 ms |
17976 KB |
Output is correct |
33 |
Correct |
4923 ms |
17792 KB |
Output is correct |
34 |
Correct |
4902 ms |
17800 KB |
Output is correct |
35 |
Correct |
4898 ms |
17884 KB |
Output is correct |
36 |
Correct |
4781 ms |
17664 KB |
Output is correct |
37 |
Correct |
4783 ms |
17944 KB |
Output is correct |
38 |
Correct |
4458 ms |
17720 KB |
Output is correct |
39 |
Correct |
4420 ms |
17820 KB |
Output is correct |
40 |
Correct |
4425 ms |
17796 KB |
Output is correct |
41 |
Correct |
4433 ms |
17656 KB |
Output is correct |
42 |
Correct |
4384 ms |
17800 KB |
Output is correct |
43 |
Correct |
4389 ms |
17668 KB |
Output is correct |
44 |
Correct |
4282 ms |
17568 KB |
Output is correct |
45 |
Correct |
4300 ms |
17820 KB |
Output is correct |
46 |
Correct |
4327 ms |
17764 KB |
Output is correct |
47 |
Execution timed out |
9054 ms |
20044 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |