# 洛谷3604 美好的每一天

## 鬼畜卡常

Posted by yjjr's blog on March 7, 2018

# 题目

m行，每行一个数表示答案

## 样例

### 输入样例#1

6 6
zzqzzq
1 6
2 4
3 4
2 3
4 5
1 1


### 输出样例#1

16
4
2
2
3
1


# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=6e4+6;
int n,m,t,blo,belong[maxn],a[maxn],ans[maxn],re;
unsigned short cnt[1<<26];
char s[maxn];

return belong[a.l]^belong[b.l]?belong[a.l]<belong[b.l]:belong[a.l]&1?a.r<b.r:a.r>b.r;
}
void insert(int x){
rep(i,0,t-1)re+=cnt[x^(1<<i)];
re+=cnt[x]++;
}
void erase(int x){
rep(i,0,t-1)re-=cnt[x^(1<<i)];
re-=--cnt[x];
}
int main()
{
rep(i,1,n)t=max(t,s[i]-'a'+1);
blo=n/sqrt(m*2/3);
rep(i,1,n)belong[i]=(i-1)/blo,a[i]=a[i-1]^(1ll<<s[i]-'a');
sort(q+1,q+1+m,cmp);
for(int i=1,l=1,r=0;i<=m;i++){
while(l>q[i].l)insert(a[--l]);
while(r<q[i].r)insert(a[++r]);
while(l<q[i].l)erase(a[l++]);
while(r>q[i].r)erase(a[r--]);
insert(a[l-1]);
ans[q[i].pos]=re;
erase(a[l-1]);
}
rep(i,1,m)cout<<ans[i]<<endl;
return 0;
}