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. kingbaseES sql 优化技巧汇总

    1.整体思路 针对业务系统的出现的慢sql 我们的优化步骤大概分为以下几步 1.识别高负载语句 2.收集性能相关的数据 3.确定性能问题产生的原因 4.实施优化手段 下面我们针对这几个步骤展开进行讲解 ...

  2. 分享本人依照NOI大纲整理的CSPJ复习资料

    2023 CSP-J 复习文件 考纲 复习好 : 基础知识与编程环境 难度 计算机的基本构成 计算机的组成及功能 - 知乎 (zhihu.com) 操作系统的基本概念与常见操作 操作系统基础知识大汇总 ...

  3. NodeJS 实战系列:模块设计与文件分类

    我们从一个最简单的需求开始,来探索我们应该从哪些方面思考模块设计,以及如何将不同的文件分类.之所以说"思考",是因为我在这篇文章里更多的是提供一类解决问题的范式,而非统一的标准答案 ...

  4. #圆方树,树链剖分#P4334 [COI2007] Policija

    题目 分析 先建一棵圆方树,必经点\(z\)就是满足\(z\)在\(x\)和\(y\)之间的路径上, 这个直接用树链剖分做,必经边必须要满足不在环上, 那么这个必经边就可以找到一个方点,就可以转换成必 ...

  5. 在typescript中,Omit是什么意思

    在TypeScript中,Omit<Type, Keys> 是一个工具类型(utility type),它用于创建一个新的类型,这个新类型是从现有类型(Type)中排除了某些指定的属性(K ...

  6. 玩转OpenHarmony社交场景:即时通讯平台

    一.简介 本样例是基于即时通讯(Instant messaging,简称IM)服务实现的OpenAtom OpenHarmony(简称"OpenHarmony")应用,允许两人或多 ...

  7. Makefile 常用命令详解

    在软件开发中,Makefile是一种非常常用的自动化工具.Makefile文件包含了一系列规则,用于编译.打包.测试等操作,可以帮助我们自动化这些操作,提高项目的管理和编译效率.本文将介绍Makefi ...

  8. Python删除文件、文件夹----os

    使用 os  删除文件 import os '''删除文件 语法: os.unlink(path) 示例: 删除 b 文件夹中的 12.txt ''' os.unlink('b/12.txt')   ...

  9. C++ 获取数组大小、多维数组操作详解

    获取数组的大小 要获取数组的大小,可以使用 sizeof() 运算符: 示例 int myNumbers[5] = {10, 20, 30, 40, 50}; cout << sizeof ...

  10. 开发指导—利用CSS动画实现HarmonyOS动效(二)

      注:本文内容分享转载自HarmonyOS Developer官网文档 点击查看<开发指导-利用CSS动画实现HarmonyOS动效(一)> 3. background-position ...