萌新女娘求助 UKE
查看原帖
萌新女娘求助 UKE
511676
naoliaok_lovely楼主2025/1/15 21:31

以下代码交上去会 UKE,虽然知道大概率是 RE 了,但是感觉 SPJ 可以修一下。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define double long double

const int N = 2.5e5 + 10;
const double eps = 1e-10;
LL n;
int P, q;
double p;
struct tree
{
	LL l, r;
	int lson, rson;
	double sum, mul;
	void init(LL l1, LL r1)
	{
		l = l1, r = r1, mul = 1;
	}
}a[N * 120];
int tot;

inline void pushup(int p)
{
	a[p].sum = a[a[p].lson].sum + a[a[p].rson].sum;
}
inline void pushdown(int p)
{
	if(!a[p].lson)
	{
		double w = a[p].sum / (a[p].r - a[p].l + 1);
		LL mid = a[p].l + a[p].r >> 1;
		a[a[p].lson = ++tot].init(a[p].l, mid);
		a[a[p].lson].sum = w * (mid - a[p].l + 1);
		a[a[p].rson = ++tot].init(mid + 1, a[p].r);
		a[a[p].rson].sum = w * (a[p].r - mid);
		a[p].mul = 1;
	}
	else
	{
		a[a[p].lson].sum *= a[p].mul, a[a[p].lson].mul *= a[p].mul;
		a[a[p].rson].sum *= a[p].mul, a[a[p].rson].mul *= a[p].mul;
		a[p].mul = 1;
	}
}

void modify(int p, LL l, LL r, double d)
{
	if(l > r) return;
//	if(p == 1) cout << l << ' ' << r << ' ' << d << endl;
	if(l <= a[p].l && a[p].r <= r)
	{
		a[p].sum *= d, a[p].mul *= d;
		return;
	}
	
	pushdown(p);
	LL mid = a[p].l + a[p].r >> 1;
	if(l <= mid) modify(a[p].lson, l, r, d);
	if(r > mid) modify(a[p].rson, l, r, d);
	pushup(p);
}

LL pos;
double ppp;
void getpos(int p, double sum)
{
	if(a[p].l == a[p].r)
	{
		if(fabs(sum + a[p].sum - 0.5) < fabs(ppp - 0.5)) pos = a[p].l, ppp = sum + a[p].sum;
		return;
	}
	
	pushdown(p);
	if(sum + a[a[p].lson].sum > 0.5) 
	{
		if(a[a[p].lson].sum > eps) getpos(a[p].lson, sum);
		else getpos(a[p].rson, sum + a[a[p].lson].sum);
	}
	else if(sum + a[p].sum < 0.5) 
	{
		if(a[a[p].rson].sum > eps) getpos(a[p].rson, sum + a[a[p].lson].sum);
		else getpos(a[p].lson, sum);
	}
	else 
	{
		if(a[a[p].lson].sum > eps) getpos(a[p].lson, sum);
		if(a[a[p].rson].sum > eps) getpos(a[p].rson, sum + a[a[p].lson].sum);
	}
}

void del(int p, LL x)
{
	if(a[p].l == a[p].r) 
	{
		a[p].sum = 0;
		return;
	}
	
	pushdown(p);
	LL mid = a[p].l + a[p].r >> 1;
	if(x <= mid) del(a[p].lson, x);
	else del(a[p].rson, x);
	pushup(p);
}

int main()
{
	cin >> n >> P >> q;
	p = P / 100.0;
	a[tot = 1].init(1, n), a[1].sum = 1;
	
	while(1)
	{
		pos = 0, ppp = -1, getpos(1, 0);
		printf("%lld\n", pos);
		fflush(stdout);
		char op[5];
		scanf("%s", op);
		if(op[0] == '=' || op[0] == '!') break;
		
		del(1, pos);
		if(op[0] == '<') modify(1, 1, pos - 1, p), modify(1, pos + 1, n, (1 - p));
		else modify(1, 1, pos - 1, (1 - p)), modify(1, pos + 1, n, p);
		modify(1, 1, n, 1 / a[1].sum); 
	}
	return 0;
}
2025/1/15 21:31
加载中...