QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#573587#9222. Price ComboYansuan_HClTL 2ms12068kbC++203.8kb2024-09-18 19:20:282024-09-18 19:20:29

Judging History

你现在查看的是最新测评结果

  • [2024-09-18 19:20:29]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:12068kb
  • [2024-09-18 19:20:28]
  • 提交

answer

#include <bits/stdc++.h>
#define ms(x, v) memset(x, v, sizeof(x))
#define il __attribute__((always_inline)) static
#define U(i,l,r) for(int i(l),END##i(r);i<=END##i;++i)
#define D(i,r,l) for(int i(r),END##i(l);i>=END##i;--i)
using namespace std;
using ll = long long;

#define IC isdigit(c)
#define GC c=getchar()
void rd(auto &x) { x = 0; char GC; bool f = 0;
	for (; !IC; GC) f |= c == '-';
	for (; IC; GC) x = x * 10 + c - 48;
	if (f) x = -x;
}
void rd(auto &x, auto &...y) { rd(x); rd(y...); }
#define meow(...) fprintf(stderr, __VA_ARGS__)
#define Assert(e, v) if (!(e)) exit(v);

const int N = 200005;
int n;

const ll inf = 1e18;
using info = array<ll, 4>; using tag = array<array<ll, 4>, 4>;
il void clear(tag &l) {
	U (x, 0, 3) l[x].fill(inf);
}
il void init(tag &l) {
	clear(l);
	U (x, 0, 3) l[x][x] = 0;
}
il void mul(info &f, info l, const tag &r) {
	f.fill(inf);
	U (x, 0, 3) if (l[x] != inf) U (y, 0, 3) if (r[x][y] != inf)
		f[y] = min(f[y], l[x] + r[x][y]);
}
il void mul(tag &f, tag l, const tag &r) {
	clear(f);
	U (i, 0, 3) U (j, 0, 3) if (l[i][j] != inf) U (k, 0, 3) if (r[j][k] != inf)
		f[i][k] = min(f[i][k], l[i][j] + r[j][k]);
}
il void check(info &f, const info &l, const info &r) {
	U (i, 0, 3) f[i] = min(l[i], r[i]);
}

#define mid ((l + r) >> 1)
#define ls (p << 1)
#define rs (ls | 1)
#define arg int p = 1, int l = 0, int r = n
#define LS ls, l, mid
#define RS rs, mid + 1, r
struct node { info v; tag ta, tb; } tr[N * 4];
void build(arg) {
	init(tr[p].ta); init(tr[p].tb); tr[p].v.fill(inf); tr[p].v[0] = 0;
	if (l == r) return;
	build(LS); build(RS);
}
il void up(node &x, const node &l, const node &r) {
	info vr; mul(vr, r.v, l.tb);
	check(x.v, l.v, vr);
	mul(x.tb, r.tb, l.tb);
}
il void appa(node &x, const tag &t) {
	mul(x.v, x.v, t); mul(x.ta, x.ta, t);
}
il void down(int p) {
	appa(tr[ls], tr[p].ta); appa(tr[rs], tr[p].ta);
	init(tr[p].ta);
}
node query(int b, int e, arg) {
	if (b <= l && e >= r) return tr[p];
	down(p);
	if (e <= mid) return query(b, e, LS);
	if (b > mid) return query(b, e, RS);
	node f, x = query(b, e, LS), y = query(b, e, RS);
	up(f, x, y);
	return f;
}
void modv(int x, const info &v, arg) {
	if (l == r) { tr[p].v = v; return; }
	down(p);
	(x <= mid) ? modv(x, v, LS) : modv(x, v, RS);
	up(tr[p], tr[ls], tr[rs]);
}
void modt(int x, const tag &v, arg) {
	if (l == r) { tr[p].tb = v; return; }
	down(p);
	(x <= mid) ? modt(x, v, LS) : modt(x, v, RS);
	up(tr[p], tr[ls], tr[rs]);
}
void op(int b, int e, const tag &t, arg) {
	if (b <= l && e >= r) { appa(tr[p], t); return; }
	down(p);
	if (b <= mid) op(b, e, t, LS);
	if (e > mid) op(b, e, t, RS);
	up(tr[p], tr[ls], tr[rs]);
}
void add_a(int b, int e, ll v) {
	tag f; clear(f);
	U (i, 0, 3) f[i ^ 2][i] = (i & 2 ? v : 0);
	op(b, e, f);
}
void ins_b(int x, ll v) {
	tag f; clear(f);
	U (i, 0, 3) f[i ^ 1][i] = (i & 1 ? v : 0);
	modt(x, f);
}

ll a[N], b[N];
tuple<ll, ll, int> la[N], lb[N];
//int refa[N], refb[N];
signed main() {
//	freopen("ava.in", "r", stdin);
	
	rd(n);
	U (i, 1, n) rd(a[i]); 
	U (i, 1, n) rd(b[i]), lb[i] = {b[i], a[i], i}, la[i] = {a[i], b[i], i};
	sort(la + 1, la + n + 1); sort(lb + 1, lb + n + 1);
	U (i, 1, n) {
		ll oa = a[i], ob = b[i];
		a[i] = lower_bound(la + 1, la + n + 1, tuple<ll, ll, int>(oa, ob, i)) - la;
		b[i] = lower_bound(lb + 1, lb + n + 1, tuple<ll, ll, int>(ob, oa, i)) - lb;
//		refa[a[i]] = i; refb[b[i]] = i;
	}
	
	build();
	D (x, n, 1) {
		auto [oa, ob, i] = la[x];
		node _ = query(b[i] - 1, n); info t = _.v;
		modv(b[i] - 1, t);
		add_a(0, b[i] - 1, oa);
		ins_b(b[i] - 1, ob);
//		assert(a[i] == x);
	}
	ll ans = inf;
	node _ = query(0, n); info t = _.v;
	U (i, 0, 3) ans = min(ans, t[i]);
//	clog << "#";
//	U (i, 0, 3) clog << t[i] << ' ';
//	clog << endl;
	printf("%lld\n", ans);
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 12068kb

input:

7
10 12 19 99 10 8 49
9 14 15 199 11 7 19

output:

131

result:

ok single line: '131'

Test #2:

score: 0
Accepted
time: 1ms
memory: 9940kb

input:

7
10 12 19 99 10 8 49
9 14 15 199 11 7 19

output:

131

result:

ok single line: '131'

Test #3:

score: -100
Time Limit Exceeded

input:

199913
1212731 2525164 3210261 2457211 1013738 1931420 923123 867112 762069 2108660 108920 2491869 867107 387025 2278045 574027 1661570 820133 1274150 2001346 779766 3305537 3000211 2418643 2108660 2001343 1074820 2754411 826712 3117447 1661569 338161 1849064 3007920 3057426 468078 3252798 1274146 4...

output:


result: