2023冲刺国赛模拟6

A.A

缩成 \(ABABA..\) 每次删去两个,于是猜结论,取前 \((n - 1) / 2\)

code
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

int read(){
	int x = 0; char c = getchar();
	while(!isdigit(c))c = getchar();
	do{x = x * 10 + (c ^ 48); c = getchar();}while(isdigit(c));
	return x;
}
const int maxn = 1e6 + 55;
int n, w[maxn], top;
char s[maxn];

int main(){
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	n = read(); scanf("%s",s + 1);
	for(int i = 1; i <= n; ++i)w[i] = read();
	top = 1;
	for(int i = 2; i <= n; ++i)if(s[i] == s[i - 1])w[top] = max(w[top], w[i]); else w[++top] = w[i];
	if(top <= 2){printf("0\n");return 0;}
	int cnt = (top - 1) / 2;
	sort(w + 2, w + top);
	ll ans = 0;
	for(int i = 1; i <= cnt; ++i)ans += w[top - i];
	printf("%lld\n",ans);
	return 0;
}

B.B

对操作分块,处理 \(f_{i, j}\) 表示经过第 \(i\) 个块后 \(j\) 会到哪个位置

散块暴力搞

考虑如何处理一个块的信息

把涉及到的点拿出来建立虚树,两个点之间路径上的点用双端队列维护

对其他点处理在 \(t\) 时刻会出现在虚树上位置 \(x\)

用并查集(其实是个链表) 把重合的点缩一块,简单讨论每个位置是向上走还是向下走即可

写起来比较愉悦身心

不长,真的不长

code
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int read(){
	int x = 0; char c = getchar();
	while(!isdigit(c))c = getchar();
	do{x = x * 10 + (c ^ 48); c = getchar();}while(isdigit(c));
	return x;
}
const int maxn = 1e5 + 5, sq = 450;
int n, m, q, fa[maxn], a[maxn], B;
struct graph{
	int head[maxn], e[maxn];
	void add(int u, int v){
		e[v] = head[u];
		head[u] = v;
	}
}t, it;
int son[maxn], si[maxn], dep[maxn], top[maxn], dfn[maxn], id[maxn], tim;
void dfs1(int x){
	si[x] = 1;
	for(int v = t.head[x]; v; v = t.e[v]){
		dep[v] = dep[x] + 1;
		dfs1(v); si[x] += si[v];
		if(si[son[x]] < si[v])son[x] = v;
	}
}
void dfs2(int x, int tp){
	dfn[x] = ++tim; id[tim] = x; top[x] = tp;
	if(son[x])dfs2(son[x], tp);
	for(int v = t.head[x]; v; v = t.e[v])
		if(v != son[x])dfs2(v, v);
	si[x] = si[x] + dfn[x] - 1;
}
int LCA(int u, int v){
	while(top[u] != top[v]){
		if(dep[top[u]] > dep[top[v]])u = fa[top[u]];
		else v = fa[top[v]];
	}
	return dep[u] < dep[v] ? u : v;
}
int kfa(int x, int k){
	int tmp = dfn[x] - dfn[top[x]] + 1;
	while(k >= tmp){
		k -= tmp; x = fa[top[x]];
		tmp = dfn[x] - dfn[top[x]] + 1;
	}
	return id[dfn[x] - k];
}
int move(int x, int y){
	if(x == y)return x;
	if(dfn[x] <= dfn[y] && dfn[y] <= si[x])return kfa(y, dep[y] - dep[x] - 1);
	return fa[x];
}
int tr[sq][maxn], tres[maxn], L[sq], R[sq], bl[maxn];
deque<int>que[maxn];
int node[maxn + maxn], tot, thq[maxn];
bool cmp(int x, int y){return dfn[x] < dfn[y];}
struct dsu{
	int f[maxn];
	void init(){for(int i = 1; i <= n; ++i)f[i] = i;}
	int fa(int x){return f[x] == x ? x : f[x] = fa(f[x]);}
	void merge(int x, int y){f[fa(y)] = x;}
}S;
vector<pii>rmg[sq];
int mxlen, nfa[maxn], nid[maxn];
void dfs(int x, int len, int las){
	if(len == mxlen)return;
	if(!thq[x])rmg[len].emplace_back(x, las);
	for(int v = t.head[x]; v; v = t.e[v]){
		if(thq[v])dfs(v, 0, v);
		else dfs(v, len + 1, las);
	}
}
void move_to(int x, int &pos){
	if(x == 0)return;
	if(pos == 0)pos = x;
	else S.merge(pos, x);
}
void change(int x, int ban){
	for(int v = it.head[x]; v; v = it.e[v])if(v != ban){
		if(que[v].size()){
			move_to(que[v].back(), nid[x]);
			que[v].pop_back();
			que[v].push_front(nid[v]);
		}else move_to(nid[v], nid[x]);
		nid[v] = 0; change(v, 0);
	}
}
void moveto(int v){
	int x = 0;
	while(true){
		change(v, x);
		x = v; v = nfa[v];
		if(v){
			if(que[x].size()){
				move_to(que[x].front(), nid[x]);
				que[x].pop_front();
				que[x].push_back(nid[v]);
			}else move_to(nid[v], nid[x]);
			nid[v] = 0;
		}else break;
	}
}
void get_tr(int bl){
	tot = 0; mxlen = R[bl] - L[bl] + 1;
	for(int i = L[bl]; i <= R[bl]; ++i)node[++tot] = a[i];
	sort(node + 1, node + tot + 1, cmp);
	for(int i = tot; i > 1; --i)node[++tot] = LCA(node[i], node[i - 1]); 
	node[++tot] = 1; sort(node + 1, node + tot + 1, cmp);
	tot = unique(node + 1, node + tot + 1) - node - 1;
	for(int i = 1; i <= tot; ++i)nid[node[i]] = node[i], thq[node[i]] = node[i];
	for(int i = 2; i <= tot; ++i){
		int x = fa[node[i]]; 
		while(!thq[x])que[node[i]].push_back(x), thq[x] = node[i], x = fa[x];
		nfa[node[i]] = x; 
		it.add(x, node[i]);
	}
	dfs(1, 0, 1); S.init();
	for(int i = L[bl]; i <= R[bl]; ++i){
		int rt = a[i]; moveto(rt);
		for(pii v : rmg[i - L[bl] + 1]){
			int iq = thq[v.second];
			if(iq == v.second)move_to(v.first, nid[iq]);
			else move_to(v.first, que[iq][dep[iq] - dep[v.second] - 1]);
		}
		rmg[i - L[bl] + 1].clear();
	}
	for(int i = 2; i <= tot; ++i){
		int x = fa[node[i]], cid = 0;
		while(x != nfa[node[i]]){
			tres[que[node[i]][cid]] = x;
			x = fa[x]; ++cid;
		}
	}
	for(int i = 1; i <= tot; ++i)tres[nid[node[i]]] = node[i];
	for(int i = 1; i <= n; ++i){
		tr[bl][i] = tres[S.fa(i)];
		if(!tr[bl][i])tr[bl][i] = kfa(i, R[bl] - L[bl] + 1);
	}
	for(int i = 1; i <= n; ++i)tres[i] = 0;
	for(int i = 1; i <= n; ++i)thq[i] = 0;
	for(int i = 1; i <= tot; ++i)it.head[node[i]] = 0;
	for(int i = 1; i <= tot; ++i)que[node[i]].clear();
}
int trans(int l, int r, int x){
	int thel = bl[l], ther = bl[r];
	if(thel == ther){
		for(int i = l; i <= r; ++i)x = move(x, a[i]);
		return x;
	}
	for(int i = l; i <= R[thel]; ++i)x = move(x, a[i]);
	for(int i = thel + 1; i < ther; ++i)x = tr[i][x];
	for(int i = L[ther]; i <= r; ++i)x = move(x, a[i]);
	return x;
}
int main(){
	freopen("b.in","r",stdin);
	freopen("b.out","w",stdout);
	n = read(), m = read(), q = read();	B = max((int)sqrt(m * 0.5), 1); 
	for(int i = 2; i <= n; ++i)t.add(fa[i] = read(), i);
	for(int i = 1; i <= m; ++i)a[i] = read();
	for(int i = 1; i <= m; ++i)bl[i] = (B + i - 1) / B;
	for(int i = 1; i <= bl[m]; ++i)L[i] = (i - 1) * B + 1;
	for(int i = 1; i <= bl[m]; ++i)R[i] = i * B;
	R[bl[m]] = m; dfs1(1); dfs2(1, 1);
	for(int i = 1; i <= bl[m]; ++i)get_tr(i);
	for(int i = 1; i <= q; ++i){
		int l = read(), r = read();
		printf("%d\n",trans(l, r, read()));
	}
	return 0;
}

C.C

没改。

posted @ 2023-05-31 20:05  Chen_jr  阅读(21)  评论(0编辑  收藏  举报