#分块#洛谷 4135 作诗

这是一篇关于洛谷第4135题目的解题分享,主要探讨如何利用分块技术解决作诗问题。文章指出由于题目特性,线段树等数据结构不适用,建议通过理解分块思想来求解。提供的代码实现具有O(n^2)的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目


分析

首先此题好像用不了线段树什么的,只好考虑分块
但是分块这种题目其实看注释会更好啊,时间复杂度 O ( n n ) O(n\sqrt n) O(nn )


代码

#include <cstdio>
#include <cctype>
#include <cmath>
#define rr register
using namespace std;
const int N=100011;
//dp表示块的答案,s表示从某个块的开头到末尾某个种类的个数
int n,m,block,a[N],le[N],cnt[N],st[N],s[318][N],dp[318][318],pos[N],top;
inline signed iut(){
	rr int ans=0; rr char c=getchar();
	while (!isdigit(c)) c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans;
}
inline void print(int ans){
	if (ans>9) print(ans/10);
	putchar(ans%10+48);
}
inline signed pro(int l,int r){
	rr int ans=0;
	if (pos[l]+1<=pos[r]-1) ans=dp[pos[l]+1][pos[r]-1];
	for (rr int i=l;i<le[pos[l]+1];++i) ++cnt[a[i]],st[++top]=a[i];
	for (rr int i=le[pos[r]];i<=r;++i) ++cnt[a[i]],st[++top]=a[i];
	for (;top;--top){
		rr int x=st[top];
		if (cnt[x]){
			if (!(s[pos[l]+1][x]^s[pos[r]][x])) ans+=(cnt[x]&1)^1;//该种类在中间的大块里没有出现
			else if (cnt[x]&1) ans+=((s[pos[l]+1][x]^s[pos[r]][x])&1)?1:-1;//偶数加1,奇数减1
			cnt[x]=0;
		}
	}
	return ans;
}
signed main(){
	n=iut(); iut(); m=iut(); block=sqrt(n);
	for (rr int i=1;i<=n;++i){//分段
		a[i]=iut(),pos[i]=(i-1)/block+1;
		if (pos[i]^pos[i-1]) le[pos[i]]=i;
	}
	pos[n+1]=pos[n]+1,le[pos[n+1]]=n+1;
	for (rr int i=1;i<=pos[n];++i){
		rr int now=0;
		for (rr int j=le[i];j<=n;++j){
		    ++s[i][a[j]];
		    if ((s[i][a[j]]&1)&&(s[i][a[j]]^1)) --now;//奇数个
		        else if (!(s[i][a[j]]&1)) ++now;//正偶数个
		    if (pos[j]^pos[j+1]) dp[i][pos[j]]=now;
		}
	}
	for (rr int ans=0,l,r;m;--m){
		l=(iut()+ans)%n+1,r=(iut()+ans)%n+1;
		if (l>r) l^=r,r^=l,l^=r; ans=top=0;
		if (pos[l]^pos[r]) ans=pro(l,r);
		else{
			for (rr int i=l;i<=r;++i) ++cnt[a[i]],st[++top]=a[i];
			for (;top;--top) if (cnt[st[top]]) 
				    ans+=(cnt[st[top]]&1)^1,cnt[st[top]]=0;//暴力
		}
		print(ans); putchar(10);
	}
	return 0;
} 
### 代码块无法分块运行的解决方法 Jupyter Notebook 是一个功能强大的交互式编程环境,但在使用过程中可能会遇到代码块无法正常分块运行的问题。以下是几种可能的原因及对应的解决方案。 #### 确保单元格类型为 `Code` 在 Jupyter Notebook 中,每个单元格都有其特定的类型,例如 `Code` 或 `Markdown`。如果当前单元格被设置为 `Markdown` 类型,则无法执行代码。可以通过选择单元格并在工具栏中点击 "Code" 按钮将其切换回代码模式[^3]。 #### 检查内核状态 有时,内核(Kernel)可能出现问题,导致代码无法执行。可以尝试重启内核来解决此类问题。在菜单栏中选择 `Kernel > Restart Kernel and Clear All Outputs...`,这将清除所有输出并重新启动内核。 #### 使用魔法命令管理代码执行 Jupyter 提供了一些有用的魔法命令,可以帮助更好地管理和调试代码。例如: - `%run file.py` 可以用来运行外部 Python 脚本文件。 - `%load file.py` 可以将指定脚本的内容加载到当前单元格中。 - `%pycat file.py` 在传呼机中打开脚本。 这些命令可以在调试和测试不同部分的代码时非常有用[^2]。 #### 配置 Jupyter Notebook 设置 如果发现某些默认设置限制了代码执行的方式,可以通过修改配置文件来调整行为。对于 Linux 用户,配置文件通常位于 `~/.jupyter/jupyter_notebook_config.py`;而对于 Windows 用户,则路径应为 `C:\Users\User\.jupyter\jupyter_notebook_config.py`。如果没有找到该文件,可以使用命令 `jupyter notebook --generate-config` 来生成它[^1]。 #### 安装 Anaconda 并使用其环境 有时候,单独安装 Jupyter Notebook 可能会因为依赖项缺失而导致各种问题。推荐使用 Anaconda 分发版,因为它包含了大量预装的数据科学包,并且能够很好地与 Jupyter 集成。通过 Anaconda Navigator 启动 Jupyter Notebook 或者直接在命令行输入 `jupyter notebook` 即可开始使用。 #### 更新软件版本 确保所使用的 Jupyter Notebook 版本是最新的,因为旧版本可能存在已知的问题或 bug。可以通过 pip 或 conda 更新 Jupyter: ```bash pip install --upgrade notebook # 或者使用 conda conda update jupyter ``` 以上方法应该能帮助解决大部分关于代码块无法分块运行的问题。如果上述建议未能解决问题,请考虑查阅官方文档或社区支持资源以获取更多帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值