Range Minimum/Maximum Query是指处理多次查询一段区间内最大最小值的算法。ST表可以o(nlogn)预处理,o(1)查询。
建表过程如下
for(int j=1;j<=19;j++)
{
for(int i=0;i<n;i++)
{
if(i+(1<<j)-1<n)
{
f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);//gcd为max,min一类函数,后面会有说明
}
}
}
查询如下
LL getgcd(int l,int r)
{
int j=31-__builtin_clz(r-l+1);
return gcd(f[l][j],f[r-(1<<j)+1][j]);
}
其中有关__builtin_clz函数的用法,转自以下blog
http://blog.csdn.net/luyuncheng/article/details/11674123
— Built-in Function: int __builtin_ffs (unsigned int x)
返回右起第一个‘1’的位置。//__builtin_ctz +1
— Built-in Function: int __builtin_clz (unsigned int x)
返回左起第一个‘1’之前0的个数。
— Built-in Function: int __builtin_ctz (unsigned int x)
返回右起第一个‘1’之后的0的个数。
— Built-in Function: int __builtin_popcount (unsigned int x)
返回‘1’的个数。
— Built-in Function: int __builtin_parity (unsigned int x)
返回‘1’的个数的奇偶性。
用31-__builtin_clz(x)就可以代替蠢的一比的log函数了.
2000ms的题用 __builtin_clz 400ms 过了,用 log T了,我只能说log要你何用.
下面进入正题。
CF475D. CGCDSSQ
time limit per test2 seconds
memory limit per test256 megabytes
Given a sequence of integers a1, …, an and q queries x1, …, xq on it. For each query xi you have to count the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, …, ar) = xi.
is a greatest common divisor of v1, v2, …, vn, that is equal to a largest positive integer that divides all vi.
Input
The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line contains n space separated integers a1, …, an, (1 ≤ ai ≤ 109).
The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then follows q lines, each contain an integer xi, (1 ≤ xi ≤ 109).
Output
For each query print the result in a separate line.
题意要求统计所有gcd=xi的区间数.很容易想到枚举所有l,r,计算gcd存下来o(1)查询.
难点有2:
1.枚举l,r是o(n^2)的
2.查询l-r的gcd是o(n)的
加起来就是o(n^3),远远不够。
对于2,可以用RMQ预处理达到o(1).
因为gcd和min在某种意义上是一样的,同样满足元素越多值越小,相同值多次出现对结果不产生影响(这是ST表的关键所在,因为中间重复部分对结果不影响使得查询部分中这句话成立:return gcd(f[l][j],f[r-(1<<j)+1][j]);
)同理,max~最小公倍数.
对于1,需要注意到一个很关键的事实:当一个以x为起点的区间向右扩张时,最多下降logx次(下降是显然的),因为即使x的因子只有2,每次下降也至少是/=2,所以至多有logx个连续相等区间,我们只需二分找到每个连续相等区间的右端点,再将这段区间加到map里面。其中ST表起到了二分比较中点时的o(1)查询作用。
经过以上两步,复杂度到了nlogx(logn)^2,够了.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int f[105000][20];
int n;
int gcd(int x,int y)
{
if(x==0)return y;
return gcd(y%x,x);
}
int getgcd(int l,int r)
{
int j=31-__builtin_clz(r-l+1);
return gcd(f[l][j],f[r-(1<<j)+1][j]);
}
int main()
{
//for(int i=1;i<=20;i++)printf("%d %d\n",i,Log(i));
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)scanf("%d",&f[i][0]);
for(int j=1;j<=19;j++)
{
for(int i=0;i<n;i++)
{
if(i+(1<<j)-1<n)
{
f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
}
map<int,LL>mp;
for(int i=0;i<n;i++)
{
int ed=i;
while(ed<n)
{
int g=getgcd(i,ed);
int l=ed,r=n-1;
int m,tag=-1;
while(l<=r)
{
if(r-l<=1)
{
if(getgcd(i,r)==g)tag=r;
else tag=l;
break;
}
m=l+r>>1;
if(getgcd(i,m)==g)l=m;
else r=m;
}
mp[g]+=tag-ed+1;
ed=tag+1;
}
}
int q;
scanf("%d",&q);
while(q--)
{
int tmp;
scanf("%d",&tmp);
printf("%I64d\n",mp[tmp]);
}
}
return 0;
}
CF386C. Diverse Substrings
time limit per test1 second
memory limit per test256 megabytes
String diversity is the number of symbols that occur in the string at least once. Diversity of s will be denoted by d(s). For example , d(“aaa”)=1, d(“abacaba”)=3.
Given a string s, consisting of lowercase Latin letters. Consider all its substrings. Obviously, any substring diversity is a number from 1 to d(s). Find statistics about substrings diversity: for each k from 1 to d(s), find how many substrings of s has a diversity of exactly k.
Input
The input consists of a single line containing s. It contains only lowercase Latin letters, the length of s is from 1 to 3·105.
Output
Print to the first line the value d(s). Print sequence t1, t2, …, td(s) to the following lines, where ti is the number of substrings of s having diversity of exactly i.
将一个串的状态分解成26个0-1状态,0表示没有该字母,1表示有.这个状态的扩展是递增的,并且重复的部分不影响,并且最多上升26次.搞清了这三点,那这个题和上题几乎一样.事实上,因为懒,我直接在上题的代码上改了一点点,连gcd的函数名都没改……
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
char rec[305000];
LL f[305000][20];
int n;
LL gcd(LL a,LL b)
{
return a|b;
}
LL getgcd(int l,int r)
{
int j=31-__builtin_clz(r-l+1);
return gcd(f[l][j],f[r-(1<<j)+1][j]);
}
int main()
{
//for(int i=1;i<=20;i++)printf("%d %d\n",i,Log(i));
while(~scanf("%s",&rec))
{
n=strlen(rec);
for(int i=0;i<n;i++){f[i][0]=1<<(rec[i]-'a');}
for(int j=1;j<=19;j++)
{
for(int i=0;i<n;i++)
{
if(i+(1<<j)-1<n)
{
f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
}
map<int,LL>mp;
int M=0;
for(int i=0;i<n;i++)
{
int ed=i;
while(ed<n)
{
LL g=getgcd(i,ed);
int l=ed,r=n-1;
int m,tag=-1;
while(l<=r)
{
if(r-l<=1)
{
if(getgcd(i,r)==g)tag=r;
else tag=l;
break;
}
m=l+r>>1;
if(getgcd(i,m)==g)l=m;
else r=m;
}
M=max(M,__builtin_popcount(g));
mp[__builtin_popcount(g)]+=tag-ed+1;
ed=tag+1;
}
}
printf("%d\n",M);
for(int i=1;i<=M;i++)
{
printf("%I64d\n",mp[i]);
}
}
return 0;
}
总结一下,以上两题的共同点在于:
1.状态值相似于max和min
2.下降(上升)的次数有限(一个是logx,另一个干脆就是26)
这样的题就都可以可以用ST表+二分做了