90分萌新求助第三个测试点,调一天了
查看原帖
90分萌新求助第三个测试点,调一天了
637419
Supermanpilot楼主2022/5/5 22:25
#include<iostream>
using namespace std;
typedef long long ll;
const ll mod = 1000000;
int root = 0, tot = 0;
ll x;
struct Splay
{
	int ch[2];
	int fa;
	int val;
	int size;
	int cnt;
};
Splay t[1001000];
int get(int x)
{
	return t[t[x].fa].ch[1] == x;
}
void clear(int x)
{
	t[x].ch[1] = t[x].ch[0] = t[x].cnt = t[x].size = t[x].val = t[x].fa = 0;
}
void maintain(int x)
{
	t[x].size = t[t[x].ch[0]].size + t[t[x].ch[1]].size + t[x].cnt;
}
void rotate(int x)
{
	int y = t[x].fa, z = t[y].fa, chk = get(x);
	t[y].ch[chk] = t[x].ch[chk ^ 1];
	if (t[x].ch[chk ^ 1])
	{
		t[t[x].ch[chk ^ 1]].fa = y;
	}
	t[x].ch[chk ^ 1] = y;
	t[x].fa = z;
	t[y].fa = x;
	if (z)
	{
		t[z].ch[t[z].ch[1] == y] = x;
	}
	maintain(y);
	maintain(x);
}
void splay(int x)
{
	for (int f = t[x].fa; f = t[x].fa, f; rotate(x))
	{
		if (t[f].fa)
		{
			rotate(get(x) == get(f) ? f : x);
		}
	}
	root = x;
}
void insert(int x)
{
	if (!root)
	{
		t[++tot].val = x;
		t[tot].cnt++;
		root = tot;
		maintain(root);
		return;
	}
	int cur = root, f = 0;
	while (1)
	{
		if (t[cur].val == x)
		{
			t[cur].cnt++;
			maintain(cur);
			splay(cur);
			return;
		}
		f = cur;
		cur = t[f].ch[t[f].val < x];
		if (!cur)
		{
			t[++tot].val = x;
			t[tot].cnt++;
			t[tot].fa = f;
			t[f].ch[t[f].val < x] = tot;
			maintain(tot);
			maintain(f);
			splay(tot);
			return;
		}
	}
}
int pre()
{
	int cur = t[root].ch[0];
	if (!cur)
	{
		return cur;
	}
	while (t[cur].ch[1])
	{
		cur = t[cur].ch[1];
	}
	splay(cur);
	return cur;
}
int nxt()
{
	int cur = t[root].ch[1];
	if (!cur)
	{
		return cur;
	}
	while (t[cur].ch[0])
	{
		cur = t[cur].ch[0];
	}
	splay(cur);
	return cur;
}
void del()
{
	if (t[root].cnt > 1)
	{
		t[root].cnt--;
		maintain(root);
		return;
	}
	else if (!t[root].ch[0] && !t[root].ch[1])
	{
		clear(root);
		root = 0;
		return;
	}
	else if (!t[root].ch[1])
	{
		int cur = root;
		root = t[cur].ch[0];
		t[root].fa = 0;
		clear(cur);
		maintain(root);
		if (t[root].ch[1])
		{
			pre();
		}
		return;
	}
	else if (!t[root].ch[0])
	{
		int cur = root;
		root = t[cur].ch[1];
		t[root].fa = 0;
		clear(cur);
		maintain(root);
		if (t[root].ch[0])
		{
			nxt();
		}
		return;
	}
	else
	{
		int cur = root;
		int a = pre();
		splay(cur);
		int b = nxt();
		splay(cur);
		if (abs(t[a].val - x) <= abs(t[b].val - x))
		{
			pre();
			t[root].ch[1] = t[cur].ch[1];
			t[t[cur].ch[1]].fa = root;
			clear(cur);
			maintain(root);
			return;
		}
		else
		{
			nxt();
			t[root].ch[0] = t[cur].ch[0];
			t[t[cur].ch[0]].fa = root;
			clear(cur);
			maintain(root);
			return;
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	int op;
	int flag = 0;
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> op >> x;
		if (op == 0)
		{
			if (flag >= 0)
			{
				insert(x);
				flag++;
			}
			else
			{
				insert(x);
				del();
				ans = (ans + abs(x - t[root].val)%mod) % mod;
				del();
				flag++;
			}
		}
		else
		{
			if (flag <= 0)
			{
				insert(x);
				flag--;
			}
			else
			{
				insert(x);
				del();
				ans = (ans + abs(x - t[root].val)%mod) % mod;
				del();
				flag--;
			}
		}
	}
	cout << ans;
}
2022/5/5 22:25
加载中...