답안 #650305

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
650305 2022-10-13T13:08:14 Z ymm Bubble Sort 2 (JOI18_bubblesort2) C++17
60 / 100
9000 ms 33628 KB
#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("avx2")))

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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 3 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 3 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 18 ms 724 KB Output is correct
20 Correct 20 ms 960 KB Output is correct
21 Correct 19 ms 988 KB Output is correct
22 Correct 21 ms 980 KB Output is correct
23 Correct 19 ms 964 KB Output is correct
24 Correct 18 ms 952 KB Output is correct
25 Correct 18 ms 964 KB Output is correct
26 Correct 18 ms 972 KB Output is correct
27 Correct 19 ms 960 KB Output is correct
28 Correct 18 ms 956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 996 KB Output is correct
2 Correct 224 ms 2324 KB Output is correct
3 Correct 569 ms 3552 KB Output is correct
4 Correct 550 ms 3436 KB Output is correct
5 Correct 557 ms 3452 KB Output is correct
6 Correct 529 ms 3476 KB Output is correct
7 Correct 562 ms 3504 KB Output is correct
8 Correct 561 ms 3572 KB Output is correct
9 Correct 551 ms 3480 KB Output is correct
10 Correct 486 ms 3552 KB Output is correct
11 Correct 483 ms 3696 KB Output is correct
12 Correct 501 ms 3608 KB Output is correct
13 Correct 448 ms 3576 KB Output is correct
14 Correct 462 ms 3560 KB Output is correct
15 Correct 441 ms 3560 KB Output is correct
16 Correct 411 ms 3528 KB Output is correct
17 Correct 405 ms 3532 KB Output is correct
18 Correct 423 ms 3656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 3 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 18 ms 724 KB Output is correct
20 Correct 20 ms 960 KB Output is correct
21 Correct 19 ms 988 KB Output is correct
22 Correct 21 ms 980 KB Output is correct
23 Correct 19 ms 964 KB Output is correct
24 Correct 18 ms 952 KB Output is correct
25 Correct 18 ms 964 KB Output is correct
26 Correct 18 ms 972 KB Output is correct
27 Correct 19 ms 960 KB Output is correct
28 Correct 18 ms 956 KB Output is correct
29 Correct 19 ms 996 KB Output is correct
30 Correct 224 ms 2324 KB Output is correct
31 Correct 569 ms 3552 KB Output is correct
32 Correct 550 ms 3436 KB Output is correct
33 Correct 557 ms 3452 KB Output is correct
34 Correct 529 ms 3476 KB Output is correct
35 Correct 562 ms 3504 KB Output is correct
36 Correct 561 ms 3572 KB Output is correct
37 Correct 551 ms 3480 KB Output is correct
38 Correct 486 ms 3552 KB Output is correct
39 Correct 483 ms 3696 KB Output is correct
40 Correct 501 ms 3608 KB Output is correct
41 Correct 448 ms 3576 KB Output is correct
42 Correct 462 ms 3560 KB Output is correct
43 Correct 441 ms 3560 KB Output is correct
44 Correct 411 ms 3528 KB Output is correct
45 Correct 405 ms 3532 KB Output is correct
46 Correct 423 ms 3656 KB Output is correct
47 Correct 4606 ms 12796 KB Output is correct
48 Execution timed out 9070 ms 33628 KB Time limit exceeded
49 Halted 0 ms 0 KB -