首页 > 其他分享 >2023冲刺国赛模拟6

2023冲刺国赛模拟6

时间:2023-05-31 20:23:46浏览次数:40  
标签:node int top 冲刺 dfn maxn 国赛 2023 nid

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

没改。

标签:node,int,top,冲刺,dfn,maxn,国赛,2023,nid
From: https://www.cnblogs.com/Chencgy/p/17447213.html

相关文章

  • 2023冲刺国赛模拟8
    A.A你大概能看到我发的单篇(无向图最小环问题)code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;typedefunsignedlonglongull;typedefpair<int,int>pii;intread(){ intx=0;charc=getchar(); while(!isdigit(c))c=getchar(); ......
  • 2023冲刺国赛模拟9
    A.哈密顿路考虑哈密顿路一定经过\(1\),那么在这里断开\(f_s\)表示已经走过的点集为\(s\),能作为最后一个点出现的点的集合然后拼起来即可code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;typedefunsignedlonglongull;typedefpair<int......
  • 2023湘潭邀请赛 E(容斥)
    题目跳转:E题意:输入x,k,求大小为k的不同集合个数,其中所有数的gcd+lcm=x。思路:AC代码://-----------------#include<bits/stdc++.h>#definexxfirst#defineyysecond#defineIOSios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);#definefixedf......
  • 【2023 · CANN训练营第一季】——应用开发深入讲解——模型转换的ATC工具
    前言: 做一个推理应用,首先从模型转换开始(当然先得选好一个合适的模型)。在昇腾平台做模型推理,需要将Caffe,TensorFlow等开源框架网络模型转换成Davinci架构专用模型(OM格式)。昇腾张量编译器(AscendTensorCompiler,简称ATC)是异构计算架构CANN体系下的模型转换工具,模型转换过程中,ATC会进......
  • 【2023 · CANN训练营第一季】——开发者套件进阶,玩转智能小车课程笔记
    前言:基于新款开发者套件Atlas200IDKA2的智能小车,采用人工智能的方法,对摄像头采集到实时影像进行推理,产生电机等运动机构的控制指令,在特定环境里,实现自动行驶、自动泊车、目标跟踪等功能。昇腾官方开源了“玩”小车的全部软、硬件资料,还准备了模拟环境,让还没有小车的小伙伴体验自......
  • 某书x-s算法(2023-05-30更新)
     服务器2023-05-30更新了x-s算法,主要位置如下: 将其全部复制下来,放入浏览器测试(HTML代码如下):<!DOCTYPEhtml><html><head><metacharset="utf-8"/><title>X-s,X-t算法测试,技术支持:V:byc6352,日期:2023-5-31</title><scriptsrc="xs.j......
  • 2023广东省程序设计大赛F题
    思路:我们把先把所有状态和值用线段树或树状数组记录下来(因为他是连续的区间,相当于一段区间的不同状态的和)再通过二分或者倍增找出这段区间及找出左右端点1:线段树或者着树状数组维护的有两个,一个是前缀和(不管状态),一个是颜色和就是状态2:左右端点查找:一个点对应一个颜色,我们假设从......
  • pkusc2023 d1t3
    整自闭了,快一个月后才想出来怎么做。设点\(i\)是1的概率为\(p_i\),定义\(P_i(x)=1-p_i+p_ix\)。那么\(p_i\)是\(i\)的儿子节点和自己的\(P(x)\)卷起来后取后一半的系数和。树上修改很魔怔,考虑ddp。维护每个点轻儿子和自己的\(\prodP(x)\),记为\(S_i(x)\),设一共有......
  • 【2023 · CANN训练营第一季】——搭建环境:创建ECS,下载sample仓
    前言:        本文是环境搭建的第一篇笔记。主要包括下面两方面内容:    1、在华为云上创建ECS服务器,并修改Ubuntu源和pip源为国内镜像地址。        2、为了更好的使用ECS,需要在本地安装远程连接和查看代码的工具软件,以Windows为例介绍几个常用的工具软件。......
  • CVE-2023-33246学习
    1.参考学习CVE-2023-33246https://github.com/I5N0rth/CVE-2023-332462.本地搭建环境2.1下载镜像#dockerpullapache/rocketmq:4.9.1#dockerpullapacherocketmq/rocketmq-console:2.0.02.2启动broker、namesrv、console启动namesrvdockerrun-dit-p9876:987......