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. #根号分治#洛谷 3645 [APIO2015]雅加达的摩天楼

    题目传送门 分析 设 \(d[i][j]\) 表示 所处位置为 \(i\),跳跃能力为 \(j\) 的步数, 若 \(j\leq \sqrt{n}\),这样的状态最多有 \(n\sqrt{n}\) 个 ...

  2. 大咖齐聚!OpenHarmony技术峰会豪华嘉宾阵容揭晓

      第一届开放原子开源基金会OpenHarmony技术峰会即将来袭 重量级嘉宾和行业大咖高能集结 展示OpenHarmony操作系统技术革新 1场主论坛.6场分论坛干货拉满 2月25日,一起解锁更多精 ...

  3. 带你玩转OpenHarmony AI-基于海思NNIE的AI能力自定义

    简介 相信大家从玩转OpenAtom OpenHarmony(简称"OpenHarmony")AI系列专题的其他文章中,已经拓展了OpenHarmony AI在智慧出行.智慧办公等 ...

  4. C# 循环与条件语句详解

    C# Switch 语句 使用 switch 语句选择要执行的多个代码块中的一个. 示例: switch(expression) { case x: // 代码块 break; case y: // ...

  5. Docker学习路线4:Docker基础知识

    Docker是一个平台,简化了在轻量.可移植的容器中构建.打包和部署应用程序的过程.在本节中,我们将介绍Docker的基础知识.其组件以及您需要开始使用的关键命令. 容器是什么? 容器是一个轻量级.独 ...

  6. Python 中的数字类型与转换技巧

    Python中有三种数字类型: int(整数) float(浮点数) complex(复数) 当您将值分配给变量时,将创建数字类型的变量: 示例:获取您自己的Python服务器 x = 1 # int ...

  7. Health Kit申请验证有问题?解决方案全解析

    在接入Health Kit的过程中,应用上线前需要完成申请验证环节,获得正式的运动健康权限. 我们贴心整理了申请验证被驳回的高频问题,您可以在申请前阅读以下内容,避免在您的申请材料中出现下述问题影响审 ...

  8. RPM打包教程

    一.rpm是什么 rpm是一种安装包的格式.就像在Windows系统上我们常见的安装包格式是exe和msi一样,在linux上常见的安装包格式是deb和rpm.一般在红帽系列的系统上,不支持deb,所 ...

  9. Python调用动态库,获取BSTR字符串

    今天客户在用Python调用我们的动态库的时候,遇到一个问题,调用动态库中的函数,函数返回的是BSTR字符串,但是客户接收到的是一个8位长度的数字. 动态库函数原型:EXTERN_C BSTR ELO ...

  10. Python操作临时文件---tempfile

    # 使用标准库中 tempfile 下的 TemporaryFile,NamedTemporaryFile # TemporaryFile(mode='w+b', bufsize=1, suffix= ...