传送门

没想到这题还这能用莫队……本来看看以为复杂度会挂的……

预处理出每个字母开头往后\(k\)个的字符串的哈希值,然后大概就是那道小z的袜子了

而且据说这题的哈希得用自然溢出

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll unsigned long long
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
const int N=3e6+5,Base=233;
int n,m,cnt[N],rt[N],k,q,S,l,r,tot;char s[N];ll bin[N],a[N],b[N],sum[N],ans[N],res;
struct node{
int l,r,id;
inline bool operator <(const node &b)const
{return rt[l]==rt[b.l]?rt[l]&1?r<b.r:r>b.r:l<b.l;}
}qq[100005];
inline void add(R int x){res+=cnt[a[x]],++cnt[a[x]];}
inline void del(R int x){--cnt[a[x]],res-=cnt[a[x]];}
int main(){
// freopen("testdata.in","r",stdin);
scanf("%d%d%d",&n,&q,&k),S=n/sqrt(q);if(S==0)S=1;
scanf("%s",s+1);
bin[0]=1;fp(i,1,n)bin[i]=bin[i-1]*Base,sum[i]=sum[i-1]*Base+s[i]-'a',rt[i]=(i-1)/S+1;
n=n-k+1;
fp(i,1,n)b[i]=a[i]=sum[i+k-1]-bin[k]*sum[i-1];
sort(b+1,b+1+n),m=unique(b+1,b+1+n)-b-1;
fp(i,1,n)a[i]=lower_bound(b+1,b+1+m,a[i])-b;
fp(i,1,q){
scanf("%d%d",&l,&r);
if(l<=n){
if(r>n)r=n;
++tot,qq[tot].l=l,qq[tot].r=r,qq[tot].id=i;
}
}sort(qq+1,qq+1+tot);
res=0,l=1,r=0;
fp(i,1,tot){
while(l>qq[i].l)add(--l);
while(r<qq[i].r)add(++r);
while(l<qq[i].l)del(l++);
while(r>qq[i].r)del(r--);
ans[qq[i].id]=res;
}fp(i,1,q)printf("%lld\n",ans[i]);return 0;
}

P5112 FZOUTSY的更多相关文章

  1. 洛谷P5112 FZOUTSY

    卡map还行.....手写hash表即可. 我一开始以为这个k会变......在sam上想各种奇技淫巧. k不变就是问一段区间有多少对长度为k的子串相同. 然后hash把子串转化为数字,就是区间有多少 ...

  2. luogu P5112 FZOUTSY

    传送门 毒瘤出题人,卡我常数 如果后缀排序后将两两之间height\(\ge k\)的后缀放在一组,那么每次询问的答案就是\(\sum_{i} \binom{num[i]}{2}\)(num[i]是第 ...

  3. 【LGP5112】FZOUTSY

    题目 如果是\(hash\)做法的话显然就是把每一个位置后面的\(k\)个位置的hash值拿出来做一个莫队板子就好了 考虑一下牛逼的\(SAM\) 我们完全可以构造出来一棵后缀树,对于每个点找到其祖先 ...

随机推荐

  1. 关于Activity启动模式(launchMode)和intent设置测试后 发现和网上说的不符 是不是我错了 求解

    看了很多关于Activity启动模式(launchMode)和intent设置intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);发现网上说的和我测试结果 ...

  2. 【Leetcode】经典的Jump Game in JAVA

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. 【dotnet跨平台】&quot;dotnet restore&quot;和&quot;dotnet run&quot;都做了些什么?

    [dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...

  4. uva 10806 Dijkstra, Dijkstra. (最小费最大流)

    uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了 ...

  5. CentOS挂载优盘

    插入优盘前: [root@centOS5 mnt]# fdisk -l Disk /dev/hdd: bytes heads, sectors/track, cylinders Units = cyl ...

  6. HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】

    The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belongin ...

  7. html-基本form元素---ShinePans

    <html> <meta http-equiv="content-type" content="text/html;charset=UTF-8" ...

  8. HDU 6108 小C的倍数问题 【数学】 (2017"百度之星"程序设计大赛 - 初赛(A))

    小C的倍数问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. regmap使用介绍【转】

    本文转载自:http://blog.csdn.net/hellowxwworld/article/details/10737569 内核3.1引入一套新的API regmap,目的是提取出关于I2C ...

  10. POJ3045 Cow Acrobats —— 思维证明

    题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...