2020.1.11考试总结

恭贺 treAKer毒瘤之神的考验 一题中取得 rank1 的好成绩,成为新一届 毒瘤之神 !
......
结果今天就考了 treAKer 的毒瘤题...

T1

考场上看到1e6就想O(n)的做法,结果失败了...

正解思路很神奇,就是先对物品按照a来排序,询问按照m来排序,用双指针一起扫,同时维护\(f_i\) 当物品的c的和为i时 在所有的方案中,所选的物品中的b最小值最大 的方案中的 b值。判一下\(f_i\)和m+s的大小关系即可。

正应了物理老师的话:有能耐就使,没能耐就死。所以我死了...

#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int n, Q, now;
const int N = 1005;
int ans[1000010], f[100010];
struct wu 
{
	int a, b, c;
	friend bool operator <(const wu &a, const wu &b){return a.a < b.a;}
} w[1005];
struct Ask 
{
	int m, k, s, id;
	friend bool operator <(const Ask &a, const Ask &b){return a.m < b.m;}
} q[1000010];
inline int read() 
{
	int res = 0; char ch = getchar(); bool XX = false;
	for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
	for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
	return XX ? -res : res;
}
int main() 
{
	cin >> n;
	for (int i = 1; i <= n; ++i)w[i].c = read(), w[i].a = read(), w[i].b = read();
	cin >> Q;
	for (int i = 1; i <= Q; ++i)q[i].m = read(), q[i].k = read(), q[i].s = read(), q[i].id = i;
	sort(w + 1, w + 1 + n); sort(q + 1, q + 1 + Q);
	now = 1; f[0] = 1 << 30;
	for (int i = 1; i <= Q; ++i) 
	{
		while (w[now].a <= q[i].m && now <= n) 
		{
			for (int k = 100000; k >= w[now].c; --k) 
				f[k] = max(f[k], min(f[k - w[now].c], w[now].b));
			++now;
		}
		ans[q[i].id] = (f[q[i].k] > q[i].m + q[i].s);
	}
	for (int i = 1; i <= Q; ++i)puts(ans[i] ? "TAK" : "NIE");
	return 0;
}

T2
\(O(n^2)\)的DP很好推,f[i]=min(f[j],max(h[j+1...i]))(\(sum[i]-sum[j] \le L\))
发现最大值在一定的区间内不会变,并且j越小,f值越小,单调栈优化DP一下...
停,怎么只有95分啊?原来这不是正解啊..会被h单调递减的数据卡成\(O(n^2)\)...
其实这时候看情况只要翻转一下序列就行了,又变成\(O(n)\)了...
但会被h一大一小的数据卡掉...,虽然没有这样的数据,但我还是要讲一下正解。
正解就是用线段树优化DP,同时维护f和max就可以了。
事实证明,暴力还是很重要的.
考场95分代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<ctime>
#define int long long
using namespace std;
int n, L, top, cnt;
const int N = 100010;
int h[N], w[N], s[N], f[N], l[N], zhan[N];
inline int read() 
{
	int res = 0; char ch = getchar(); bool XX = false;
	for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
	for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
	return XX ? -res : res;
}
void solve1() 
{
	int tmp, mx;
	memset(f, 0x3f, sizeof(f)); f[0] = 0;
	for (int i = 1; i <= n; ++i) 
	{
		tmp = 0; mx = -1;
		for (int j = i; j >= 1; --j) 
		{
			tmp += w[j]; mx = max(mx, h[j]);
			if (tmp > L)break;
			f[i] = min(f[i], f[j - 1] + mx);
		}
	}
	cout << f[n];
}
void solve2() 
{
	for (int i = 1; i <= n; ++i) 
	{
		while (top && h[zhan[top]] <= h[i])--top;
		l[i] = zhan[top]; zhan[++top] = i;
	}
	for (int i = 1; i <= n; ++i)s[i] = s[i - 1] + w[i];
	int tmp, mx, pos;
	memset(f, 0x3f, sizeof(f)); f[0] = 0;
	for (int i = 1; i <= n; ++i) 
	{
		pos = i + 1;
		while (1) 
		{
			mx = h[pos - 1]; pos = l[pos - 1] + 1; tmp = s[i] - s[pos - 1];
			if (tmp > L)pos = lower_bound(s + 1, s + 1 + n, s[i] - L) - s;
			while (s[i] - s[pos - 1] > L)pos++;
			f[i] = min(f[i], f[pos - 1] + mx);
			if (pos == 1 || tmp > L)break;
		}
	}
	cout << f[n];
}
int TT;
inline void Swap(int &x, int &y) {TT = x; x = y; y = TT;}
signed main() 
{
	cin >> n >> L;
	h[0] = 1e15;
	for (int i = 1; i <= n; ++i)h[i] = read(), w[i] = read(), cnt += (h[i] < h[i - 1]);
	if (cnt > n / 2) 
		for (int i = 1, to = n / 2; i <= to; ++i)Swap(h[i], h[n - i + 1]), Swap(w[i], w[n - i + 1]);
	if (n <= 5000)solve1();
	else solve2();
	fclose(stdin); fclose(stdout);
	return 0;
}

正解

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define int long long
#define lson (k<<1)
#define rson ((k<<1)|1)
using namespace std;
int n, L, top;
const int N = 100010, inf = 1e18;
int h[N], w[N], s[N], l[N], dp[N], zhan[N], sum[N];
int tr[N << 2], f[N << 2], tag[N << 2];
inline int read() 
{
	int res = 0; char ch = getchar(); bool XX = false;
	for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
	for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
	return XX ? -res : res;
}
void upd(int k) 
{
	f[k] = min(f[lson], f[rson]); tr[k] = min(tr[lson], tr[rson]);
}
void down(int k) 
{
	if (tag[k] == inf)return;
	tr[lson] = f[lson] + tag[k]; tr[rson] = f[rson] + tag[k];
	tag[lson] = tag[rson] = tag[k];
	tag[k] = inf;
}
void build(int k, int l, int r) 
{
	tr[k] = f[k] = tag[k] = inf;
	if (l == r)return;
	int mid = (l + r) >> 1;
	build(lson, l, mid); build(rson, mid + 1, r);
}
void Insert(int k, int l, int r, int pos) 
{
	if (l == r) 
	{
		f[k] = dp[pos - 1]; tr[k] = inf;
		return;
	}
	down(k);
	int mid = (l + r) >> 1;
	if (pos <= mid)Insert(lson, l, mid, pos);
	else Insert(rson, mid + 1, r, pos);
}
void change(int k, int l, int r, int x, int y, int val) 
{
	if (x <= l && r <= y) 
	{
		tr[k] = f[k] + val; tag[k] = val;
		return;
	}
	down(k);
	int mid = (l + r) >> 1;
	if (x <= mid)change(lson, l, mid, x, y, val);
	if (mid + 1 <= y)change(rson, mid + 1, r, x, y, val);
	upd(k);
}
int ask(int k, int l, int r, int x, int y) 
{
	if (x <= l && r <= y)return tr[k];
	down(k);
	int mid = (l + r) >> 1, ans = inf;
	if (x <= mid)ans = min(ans, ask(lson, l, mid, x, y));
	if (mid + 1 <= y)ans = min(ans, ask(rson, mid + 1, r, x, y));
	return ans;
}
signed main() 
{
	cin >> n >> L;
	for (int i = 1; i <= n; ++i)h[i] = read(), w[i] = read(), sum[i] = sum[i - 1] + w[i];
	for (int i = 1; i <= n; ++i) 
	{
		while (top && h[zhan[top]] <= h[i])--top;
		l[i] = zhan[top]; zhan[++top] = i;
	}
	build(1, 1, n);
	for (int i = 1; i <= n; ++i) 
	{
		Insert(1, 1, n, i);
		change(1, 1, n, l[i] + 1, i, h[i]);
		int pos = lower_bound(sum, sum + i + 1, sum[i] - L) - sum;
		if (pos < i)dp[i] = ask(1, 1, n, pos + 1, i);
	}
	cout << dp[n];
	fclose(stdin); fclose(stdout);
	return 0;
}

T3
考场上只码了55分部分分,还调了1个小时...
有两种操作,询问链上第k小和连边,分别对应主席树和LCT。
但很明显我们并不能同时维护,所以我们选择放弃LCT
对于连边操作,我们根据启发式合并的思想合并,将较小的那棵树重建就行了。
treAKer :我要卡你们空间!
woc..那还得节点回收,而且有一大堆细节,调了半天.

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int Tcase, n, m, T, tot, cnt, x, y, k, lastans, lca, falca, js, top;
const int N = 80010;
int head[N], val[N], b[N], siz[N], shu[N], root[N], dep[N], fa[N][18];
int zhan[N * 20];
int sum[N * 100], lson[N * 100], rson[N * 100];
char opt[3];
struct bian 
{
	int to, nt;
} e[N << 1];
void add(int f, int t) 
{
	e[++tot] = (bian) {t, head[f]};
	head[f] = tot;
}
inline int read() 
{
	int res = 0; char ch = getchar(); bool XX = false;
	for (; !isdigit(ch); ch = getchar())(ch == '-') && (XX = true);
	for (; isdigit(ch); ch = getchar())res = (res << 3) + (res << 1) + (ch ^ 48);
	return XX ? -res : res;
}
int LCA(int x, int y) 
{
	if (dep[x] < dep[y])swap(x, y);
	for (int i = 17; i >= 0; --i)
		if (fa[x][i] && dep[fa[x][i]] >= dep[y])x = fa[x][i];
	if (x == y)return x;
	for (int i = 17; i >= 0; --i)
		if (fa[x][i] != fa[y][i])x = fa[x][i], y = fa[y][i];
	return fa[x][0];
}
void change(int pre, int &k, int l, int r, int pos, int val) 
{
	k = (top ? zhan[top--] : ++js);
	sum[k] = sum[pre] + val, rson[k] = rson[pre], lson[k] = lson[pre];
	if (l == r)return;
	int mid = (l + r) >> 1;
	if (pos <= mid)change(lson[pre], lson[k], l, mid, pos, val);
	else change(rson[pre], rson[k], mid + 1, r, pos, val);
}
void dfs(int x, int f, int s) 
{
	fa[x][0] = f; dep[x] = dep[f] + 1; shu[x] = s; ++siz[s];
	for (int i = 1; i <= 17; ++i)
		fa[x][i] = fa[fa[x][i - 1]][i - 1];
	change(root[f], root[x], 1, cnt, val[x], 1);
	for (int i = head[x]; i; i = e[i].nt)
		if (e[i].to != f)dfs(e[i].to, x, s);
}
void hui(int &k, int fa) 
{
	if (!k)return;
	if (lson[k] != lson[fa])hui(lson[k], lson[fa]);
	if (rson[k] != rson[fa])hui(rson[k], rson[fa]);
	zhan[++top] = k; lson[k] = rson[k] = 0; k = 0;
}
void dfs2(int x, int fa) 
{
	for (int i = head[x]; i; i = e[i].nt)
		if (e[i].to != fa)dfs2(e[i].to, x);
	hui(root[x], root[fa]);
}
int ask(int a, int b, int c, int d, int l, int r, int k) 
{
	if (l == r)return l;
	int mid = (l + r) >> 1, tmp = sum[lson[a]] + sum[lson[b]] - sum[lson[c]] - sum[lson[d]];
	if (k <= tmp)return ask(lson[a], lson[b], lson[c], lson[d], l, mid, k);
	else return ask(rson[a], rson[b], rson[c], rson[d], mid + 1, r, k - tmp);
}
int main() 
{
	cin >> Tcase >> n >> m >> T;
	for (int i = 1; i <= n; ++i)val[i] = b[i] = read();
	sort(b + 1, b + 1 + n);
	cnt = unique(b + 1, b + 1 + n) - b - 1;
	for (int i = 1; i <= n; ++i)val[i] = lower_bound(b + 1, b + 1 + cnt, val[i]) - b;
	for (int i = 1; i <= m; ++i) 
	{
		x = read(); y = read();
		add(x, y); add(y, x);
	}
	for (int i = 1; i <= n; ++i)
		if (!shu[i])dfs(i, 0, i);
	while (T--) 
	{
		scanf("%s", opt);
		if (opt[0] == 'Q') 
		{
			x = read()^lastans; y = read()^lastans; k = read()^lastans;
			lca = LCA(x, y); falca = fa[lca][0];
			printf("%d\n", lastans = b[ask(root[x], root[y], root[lca], root[falca], 1, cnt, k)]);
		} 
		else 
		{
			x = read()^lastans; y = read()^lastans;
			if (siz[shu[x]] > siz[shu[y]])dfs2(shu[y], 0), dfs(y, x, shu[x]);
			else dfs2(shu[x], 0), dfs(x, y, shu[y]);
			add(x, y); add(y, x);
		}
	}
	fclose(stdin); fclose(stdout);
	return 0;
}
posted @ 2020-01-11 22:22  wljss  阅读(242)  评论(0编辑  收藏  举报