Link

场上思路出的最快的一题,但没调出来。

反着考虑全为回文串需满足哪些情况。

若 \(k = 1\),没有限制条件。

若 \(k = 2\),对于任意三个位置 _ _ _,先填 \(x\) \(x\) _,然后二三也要回文,第三位只能是 \(x\),最终整段区间全部相同。

若 \(k = 3\),全部相同的情况肯定满足,考虑出现不同元素:

  • _ _ _ _
  • \(x\) _ \(x\) _
  • \(x\) \(y\) \(x\) _

此时二到四段也要回文,最终 \(x\) \(y\) \(x\) \(y\) 交替出现。

以此类推 \(k > 3\) 的情况,得到结论:

奇数需间隔排列或全相等,偶数只能全相等

此处有一特殊情况,若 \(k = r - l + 1\),那么只要整段不回文,就有 \(r - l + 1\) 的贡献。

如何快速判断区间全部相等?

只需维护 \(lst[i]\) 表示前一个与 \(s[i]\) 不同的元素的位置,最后判断 \(lst[r] < l\)。

具体实现:

	vector<int> lst(n, -1);
for(int i = 1; i < n; ++ i) {
if(s[i] != s[i - 1]) lst[i] = i - 1;
else lst[i] = lst[i - 1];
}

如何快速判断元素交替出现?

维护 \(f[i][pre]\) 表示从 \(i\) 起,前一位的值是 \(pre\) 的交替段向左延伸的最大长度。

这里再维护一个 \(L[i] = i - f[i][s[i - 1]] + 1\) 得到区间左端点,判断 \(L[r] <= l\)。

	vector<vector<int>> f(n, vector<int>(128, 1));
vector<int> L(n, 0);
for(int i = 1; i < n; ++ i) {
f[i][s[i - 1]] = 1 + f[i - 1][s[i]];
L[i] = i - f[i][s[i - 1]] + 1;
}

如何判断区间是否回文?

可以 \(manacher\),但我不会,这里用字符串哈希解决。

代码:

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define per(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
using ll = unsigned long long;
constexpr int N = 2e5 + 5, B = 131, P = 1e9 + 7; ll pre[N], pw[N] = {1};
ll h[N], t[N];
ll H(int l, int r) {return (h[r] - (h[l - 1] * pw[r - l + 1]) % P + P) % P;}
ll T(int l, int r) {return (t[r] - (t[l - 1] * pw[r - l + 1]) % P + P) % P;} void init(ll *a, string &s) {
a[0] = s[0];
for(int i = 1; i < s.length(); ++ i) {
a[i] = (a[i - 1] * B + s[i]) % P;
}
} void solve() {
int n, m; cin >> n >> m;
string s; cin >> s;
string _ = s;
reverse(_.begin(), _.end());
init(h, s);
init(t, _); vector<int> lst(n, -1);
for(int i = 1; i < n; ++ i) {
if(s[i] != s[i - 1]) lst[i] = i - 1;
else lst[i] = lst[i - 1];
} vector<vector<int>> f(n, vector<int>(128, 1));
vector<int> L(n, 0);
for(int i = 1; i < n; ++ i) {
f[i][s[i - 1]] = 1 + f[i - 1][s[i]];
L[i] = i - f[i][s[i - 1]] + 1;
} for(int i = 0; i < m; ++ i) {
int l, r; cin >> l >> r;
-- l, -- r;
if(lst[r] < l) cout << 0 << '\n';
else {
ll len = r - l + 1;
ll ans = (len - 1) * len / 2;
ans --;
if(L[r] <= l) {
ans -= pre[len - 1];
}
if(H(l, r) != T(n - r - 1, n - l - 1)) ans += len;
cout << ans << '\n';
}
}
} int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
for(int i = 3; i <= 2e5; ++ i) pre[i] = pre[i - 1] + (i & 1) * i;
for(int i = 1; i <= 2e5; ++ i) pw[i] = pw[i - 1] * B % P;
int T = 1;
cin >> T;
while(T --) solve();
return 0;
}

Codeforces Round 934 2D/1B的更多相关文章

  1. Codeforces Round #486 (Div. 3) D. Points and Powers of Two

    Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...

  2. Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest

    Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以 ...

  3. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  8. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  9. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  10. Codeforces Round #370 - #379 (Div. 2)

    题意: 思路: Codeforces Round #370(Solved: 4 out of 5) A - Memory and Crow 题意:有一个序列,然后对每一个进行ai = bi - bi  ...

随机推荐

  1. BABYRE 【攻防世界】 Reverse

    题目 丢进Exeinfo PE里面,得到64bit,无壳 丢进IDA pro(x64)里面,找到主函数 代码中有一个花指令:*(unsigned int (__fastcall **)(char *) ...

  2. Vue3项目-生成Cron表达式组件

    最近做的一个vue3项目过程中,需要用到cron表达式功能,而对于普通业务人员,他们是不懂cron表达式规则的,所以需要做一个可手动配置生成cron表达式的功能.从网上查找了一些相关资料,然后结合vu ...

  3. 一秒变身艺术家!U2Net 跨界肖像画,让你的头像瞬间细节完美复刻,打造个性化头像新风潮!

    效果 测试图片来自网络,如有侵权,联系删除. 项目 关注微信公众号,回复关键字:"一秒变身艺术家",获取程序! 模型信息 Inputs ---------------------- ...

  4. mysql统计所有分类下的数量,没有的也要展示

    要求统计所有分类下的数量,如果分类下没有对应的数据也要展示.这种问题在日常的开发中很常见,每次写每次忘,所以在此记录下. 这种统计往往不能直接group by,因为有些类别可能没有对应的数据 这里有两 ...

  5. KingbaseES Json 系列八:Json记录操作函数三

    KingbaseES Json 系列八--Json记录操作函数三(JSON_TABLE) JSON 数据类型是用来存储 JSON(JavaScript Object Notation)数据的.King ...

  6. archlinux 时移(timeshift)不会自动创建快照

    这是因为没有开启定时任务服务 解决办法 1.安装定时任务服务 sudo pacman -S cronie 2.设置自启动 sudo systemctl enble cronie

  7. 【放假第1天】采购季倒计时 2G 50/年,4G 618/3年 云服务器选购攻略 阿里云 腾讯云 京东云对比 搭建网站、数据分析

    ​ 更新日期:4月4日(阿里云价格回调,京东云采购季持续进行) <最新对比表>已更新在文章头部-腾讯云文档,文章具有时效性,请以腾讯文档为准! https://docs.qq.com/do ...

  8. Avalonia的模板控件(Templated Controls)

    在Avalonia的UI框架中,TemplatedControl是一个核心组件,它提供了一种强大的方式来创建可重用且高度可定制的控件. 本文将深入探讨TemplatedControl的概念.其带来的优 ...

  9. MogDB 操作系统优化指南

    MogDB 操作系统优化指南 本文出处:https://www.modb.pro/db/413280 在性能调优过程中,可以根据实际业务情况修改关键操作系统(OS)配置参数,以提升 MogDB 数据库 ...

  10. 优先队列的基本实现【数据结构与算法—TypeScript 实现】

    笔记整理自 coderwhy 『TypeScript 高阶数据结构与算法』课程 特性 效率比普通队列高 每个出队元素拥有最高优先级 可以用 数组.链表 等数据结构实现,但是 堆结构 是最常用的实现方式 ...