HUST - 1616 给出字符串,求[l,r]内最大的子序列的hash值

You've got string S and m queries. Each query asks you to calculate the hash value for the lexicographically maximum subsequence of  substring S[l….r]. 
We'll call a non-empty string S[  p1p2...   pk] =   sp1sp2...   spk(1 ≤   p1 <   p2 < ... <  pk ≤ |  s|) a   subsequence of string   s =   s1s2...   s  |s|
String   x =   x1x2...   x  |x| is   lexicographically larger than string   y =  y1y2...   y  |y|, if either |  x| > |  y| and   x1 =   y1,   x2 =   y2, ... ,   x  |y| =   y  |y|, or exists such number   r (  r < |  x|,   r < |  y|), that   x1 =   y1,   x2 =   y2, ... ,   xr =  yr and   xr   + 1 >   yr   + 1. Characters in lines are compared like their ASCII codes. 
The hash value for string s of length n is  . 
InputThere are multiple cases, process to the  EOF. Each cases begins with a line contains a non-empty string   s, consisting only of lowercase English letters. The string's length doesn't exceed 10^6. The second line contains an integer m(1<=m<=10^6), indicate that there are m queries. Then the next m lines contains m queries, each line describe the query with two integer l and r.  
OutputPrint the hash value of lexicographically maximum subsequence of string   sfor each query in separate line. 
Sample Input
abeced
2
1 3
1 6
kjihgfedcba
1
1 11
Sample Output
101
514298749
888634539

子序列是指:“abeced”的最大子序列为eed。

2traits:

1、每个区间的最优序列首字母一定是【该区间内最大值的首个出现位置】

2、对于每个区间[l,r]内的最优子序列,r这个点一定会被选进去

3、两个不同的点a,b,且a>b,往前的最优祖先一定一样

#include
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
#define lowbit(x) (x) & (-x)  
using namespace std;
int pre[1000005];
int len[1000005];
ll sum[1000005];
ll g[1000005];
int id[30];
int w[1000005];
int c2[1000005];
char x[1000005];
int n;   

const int MAXN = 1000005;
int dp[MAXN][22];  
void makermq(int n,int b[])  
{  
    int i,j;  
    for(i=1;i<=n;i++)  
        dp[i][0]=b[i];  
    for(j=1;(1<= b[dp[i+(1<<(j-1))][j-1]]? dp[i][j-1]:dp[i+(1<<(j-1))][j-1];  
}  
int rmqIndex(int s,int v,int b[])  
{  
    int k=(int)(log((v-s+1)*1.0)/log(2.0));  
    return b[dp[s][k]]>=b[dp[v-(1<


你可能感兴趣的:(困难)