题面:

https://www.codechef.com/problems/CBAL

题解:

可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26,

所以我们状态压缩,记 a[i]表示 s[1..i]所有字符的奇偶性状态,

那么子串 s[L..R]是平衡字符串当且仅当a[L-1]=a[R]。

我们对 a 离散化后就可以让其在[1,n]的范围内。

如果没有强制在线,那么我们很容易用莫队算法解决。

记录当前范围所有状态的出现位置下标的 0~2 次方之和,

利用(a-b)2=a2-2ab+b2可以很方便地实现在首尾添加元素。

那么这题强制在线,我们用分块算法即可。

记录 ans[i][j][type]表示块 i~块 j 的 type权值,

f[i][j][k]表示前 i 个块中权值 j 出现位置下标的 k 次方和,

那么查询一个区间我们可以先得到其完整覆盖的块的答案以及状态,

然后用上述方法在块的首尾加入剩下的元素并更新答案即可。

code:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 using namespace std;
  7 char ch;
  8 bool ok;
  9 void read(int &x){
 10     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
 11     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
 12     if (ok) x=-x;
 13 }
 14 typedef long long int64;
 15 const int maxs=320;
 16 const int maxn=100005;
 17 char s[maxn];
 18 int T,n,lim,siz,cnt,q,x,y,op;
 19 int a[maxn],tmp[maxn],bel[maxn];
 20 int64 f[maxs][maxn][3],ans[maxs][maxs][3],sum[maxn][3],A,B;
 21 struct Data{
 22     int l,r;
 23 }block[maxn];
 24 void init(){
 25     for (int i=1;i<=n;i++) a[i]=a[i-1]^(1<<(s[i]-'a'));
 26     memcpy(tmp,a,sizeof(tmp)),sort(tmp,tmp+n+1),cnt=unique(tmp,tmp+n+1)-tmp;
 27     for (int i=0;i<=n;i++) a[i]=upper_bound(tmp,tmp+cnt,a[i])-tmp;
 28     memset(block,-1,sizeof(block));
 29     for (int i=0;i<=n;i++){
 30         bel[i]=i/siz+1;
 31         if (block[bel[i]].l==-1) block[bel[i]].l=i;
 32         block[bel[i]].r=i;
 33     }
 34     lim=bel[n];
 35     for (int i=1;i<=lim;i++){
 36         for (int j=1;j<=cnt;j++) for (int k=0;k<=2;k++) f[i][j][k]=f[i-1][j][k];
 37         for (int j=block[i].l;j<=block[i].r;j++){
 38             f[i][a[j]][0]++;
 39             f[i][a[j]][1]+=j;
 40             f[i][a[j]][2]+=1LL*j*j;
 41         }
 42     }
 43     for (int i=1;i<=lim;i++){
 44         for (int j=i;j<=lim;j++){
 45             for (int k=0;k<=2;k++) ans[i][j][k]=ans[i][j-1][k];
 46             for (int k=block[j].l;k<=block[j].r;k++){
 47                 ans[i][j][0]+=sum[a[k]][0];
 48                 ans[i][j][1]+=1LL*k*sum[a[k]][0]-sum[a[k]][1];
 49                 ans[i][j][2]+=1LL*k*k*sum[a[k]][0]-2LL*k*sum[a[k]][1]+sum[a[k]][2];
 50                 sum[a[k]][0]++;
 51                 sum[a[k]][1]+=k;
 52                 sum[a[k]][2]+=1LL*k*k;
 53             }
 54         }
 55         for (int j=1;j<=cnt;j++) for (int k=0;k<=2;k++) sum[j][k]=0;
 56     }
 57 }
 58 void query(int l,int r,int op){
 59     if (l>r) swap(l,r); l--;
 60     int st=bel[l],ed=bel[r]; int64 res[3]={0,0,0},tmp[3];
 61     if (st!=ed){
 62         if (l>block[st].l) st++;
 63         if (r<block[ed].r) ed--;
 64         for (int i=0;i<=2;i++) res[i]+=ans[st][ed][i];
 65         if (st!=bel[l]){
 66             for (int i=block[bel[l]].r;i>=l;i--){
 67                 for (int j=0;j<=2;j++) tmp[j]=f[ed][a[i]][j]-f[st-1][a[i]][j]+sum[a[i]][j];
 68                 res[0]+=tmp[0];
 69                 res[1]+=tmp[1]-1LL*i*tmp[0];
 70                 res[2]+=1LL*i*i*tmp[0]-2LL*i*tmp[1]+tmp[2];
 71                 sum[a[i]][0]++;
 72                 sum[a[i]][1]+=i;
 73                 sum[a[i]][2]+=1LL*i*i;
 74             }
 75         }
 76         if (ed!=bel[r]){
 77             for (int i=block[bel[r]].l;i<=r;i++){
 78                 for (int j=0;j<=2;j++) tmp[j]=f[ed][a[i]][j]-f[st-1][a[i]][j]+sum[a[i]][j];
 79                 res[0]+=tmp[0];
 80                 res[1]+=1LL*i*tmp[0]-tmp[1];
 81                 res[2]+=1LL*i*i*tmp[0]-2LL*i*tmp[1]+tmp[2];
 82                 sum[a[i]][0]++;
 83                 sum[a[i]][1]+=i;
 84                 sum[a[i]][2]+=1LL*i*i;
 85             }
 86         }
 87         if (ed!=bel[r]){
 88             for (int i=block[bel[r]].l;i<=r;i++){
 89                 sum[a[i]][0]--;
 90                 sum[a[i]][1]-=i;
 91                 sum[a[i]][2]-=1LL*i*i;
 92             }
 93         }
 94         if (st!=bel[l]){
 95             for (int i=block[bel[l]].r;i>=l;i--){
 96                 sum[a[i]][0]--;
 97                 sum[a[i]][1]-=i;
 98                 sum[a[i]][2]-=1LL*i*i;
 99             }
100         }
101     }
102     else{
103         if (l==block[st].l&&r==block[ed].r) res[op]=ans[st][ed][op];
104         else{
105             for (int i=l;i<=r;i++){
106                 res[0]+=sum[a[i]][0];
107                 res[1]+=1LL*i*sum[a[i]][0]-sum[a[i]][1];
108                 res[2]+=1LL*i*i*sum[a[i]][0]-2LL*i*sum[a[i]][1]+sum[a[i]][2];
109                 sum[a[i]][0]++;
110                 sum[a[i]][1]+=i;
111                 sum[a[i]][2]+=1LL*i*i;
112             }
113             for (int i=l;i<=r;i++){
114                 sum[a[i]][0]--;
115                 sum[a[i]][1]-=i;
116                 sum[a[i]][2]-=1LL*i*i;
117             }
118         }
119     }
120     A=B,B=res[op];
121     printf("%lld\n",res[op]);
122 }
123 int main(){
124     for (read(T);T;T--){
125         scanf("%s",s+1),n=strlen(s+1),siz=sqrt(n),A=B=0,init();
126         for (read(q);q;q--) read(x),x=(x+A)%n+1,read(y),y=(y+B)%n+1,read(op),query(x,y,op);
127     }
128     return 0;
129 }

转载于:https://www.cnblogs.com/chenyushuo/p/5278117.html

CodeChef CBAL相关推荐

  1. codechef ANUCBC(背包)

    题目链接: https://www.codechef.com/problems/ANUCBC 按模数进行背包 取模不要直接取,分开写,不然会T #include<cstdio> #incl ...

  2. CFCC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  3. codechef INSQ15_A(hash+二分)

    思路:首先计算字符串s的所有后缀的hash值,然后根据p分离的两个字符串的最长公共子串,使用二分查找.具体代码参考: https://github.com/wuli2496/OJ/blob/maste ...

  4. CodeChef Cards, bags and coins [DP 泛型背包]

    https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集. 求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 10 ...

  5. Codechef SEAARC Sereja and Arcs (分块、组合计数)

    我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...

  6. Codechef SEAARC Sereja and Arcs (分块)

    我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...

  7. Codechef TRIPS Children Trips (分块、倍增)

    题目链接: https://www.codechef.com/problems/TRIPS 感觉CC有点毒瘤啊.. 题解: 首先有一个性质可能是因为太傻所以网上没人解释,然而我看了半天: 就是正序和倒 ...

  8. CodeChef March Lunchtime 2018 div2

    地址https://www.codechef.com/LTIME58B?order=desc&sortBy=successful_submissions 简单做了一下,前三题比较水,第四题应该 ...

  9. [Bzoj4260]Codechef REBXOR(trie树)

    4260: Codechef REBXOR Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 1534  Solved: 669 [Submit][St ...

最新文章

  1. Ascend学习资源
  2. linux中查看lvm的名称,关于Linux中LVM的使用总结
  3. MIT华人博士研究新冠遭学术霸凌!斯坦福诺奖得主:“你论文少,别说话!”...
  4. javaweb:Cookie
  5. tta部署_YOLOv5项目介绍
  6. 第六天2017/04/11(1:结构体链表基础和相关经典操作)
  7. FreeSwitch通过远程接口验证用户登录
  8. python 三维图片 任意切片_python实现对任意大小图片均匀切割的示例
  9. UE4 蓝图教程(一) 开始,一个转动的香蕉
  10. Docker 镜像制作之DockerFile
  11. vip地址能ping不通_ping不通地址
  12. 微信开通公众号 每日发一篇编程精文 自己搞着玩,不盈利
  13. 关于图像模式识别的几种分类方法概述
  14. snmp+mrtg实现对局域网内的linuxServer的监控(转)
  15. 《神经科学:探索脑》学习笔记(第6章 神经递质系统)
  16. python使用eyed3获取音频信息包含采样率比特率和通道信息等
  17. 【Python/工具】Pycharm中如何查看一个函数需要哪些参数
  18. Axure8.0.0.3303破解及汉化
  19. 【Python】实现中英文互译
  20. 【考研英语语法】将来进行时习题

热门文章

  1. wpf 绘制rectangle 代码
  2. react部署之页面空白
  3. HDU 2079 选课时间
  4. 10-Linux 基本指令
  5. BAPC2014 Bamp;amp;HUNNU11582:Button Bashing(BFS)
  6. 当前节点获取位置汇总
  7. JS实现图片上传时的本地预览,兼容IE和firefox谷歌
  8. oracle常用函数归纳
  9. Android:控件WebView显示网页
  10. 算法------Pow(x, n)