【牛客 - 371牛客OI周赛7-提高组B】小睿睿的询问(RMQ,ST表维护下标)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/87894139

题干:

链接:https://ac.nowcoder.com/acm/contest/371/B
来源:牛客网
 

小睿睿的n个妹纸排成一排,每个妹纸有一个颜值val[i]。有m个询问,对于每一个询问,小睿睿想知道区间[L,R]颜值最高而编号最小的妹纸是哪一个

对于妹纸们的颜值val[i],其生成函数为:

void generate_array(int n,int seed)
{
    unsigned x = seed;
    for (int i=1;i<=n;++i)
    {
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        val[i]=x%100;
    }
}

对于每一组询问,区间[L,R]的生成函数为:

void generate_ask(int n,int m,int seedx,int seedy)
{
    unsigned x=seedx,y=seedy;
    for (int i=1;i<=m;++i)
    {
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        y ^= y << 13;
        y ^= y >> 17;
        y ^= y << 5;
        L=(x^lastans)%n+1,R=(y^lastans)%n+1;
        if (L>R)swap(L,R);
        //解决询问
    }
}

其中lastans为上个询问的答案,对于第一个询问,lastans为0

输入描述:

第1行2个整数n,m,分别表示序列长度和询问次数

第2行3个整数seed,seedx,seedy,意义如题所示

输出描述:

一行一个整数,表示所有询问的答案的异或和

示例1

输入

复制

10 5
3 5 7

输出

复制

2

说明

生成序列:

7 11 47 53 3 7 63 36 55 55

各组询问及答案:

询问:4 6

该询问答案:4

询问:2 6

该询问答案:4

询问:2 2

该询问答案:2

询问:4 8

该询问答案:7

询问:1 9

该询问答案:7

所有询问的答案的异或和:2

示例2

输入

复制

100000 10000000
1 2 3

输出

复制

5042

备注:

对于30%的数据,n,m<=1000

对于50%的数据,m<=1000000

对于100%的数据,n<=100000,m<=10000000,seedx,seedy,seed<=1000

解题报告:

   跟一般的ST表不同,这里的ST表不是维护一个最大值,而是维护最大值所对应的下标,因为题目中说要维护的答案是下标,如果有多个最大值的话返回左边的下标。而通过ST表的原理我们不难得出结论,对于下标也同样可以维护。

AC代码:

#include<cstdio>
#include<assert.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 6e5 + 6;
int lastans = 0;
int seed,seedx,seedy,n,m;
int val[MAX],Log[MAX];
int dp[MAX][25];
void generate_array(int n,int seed) {
    unsigned x = seed;
    for (int i=1; i<=n; ++i) {
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        val[i]=x%100;
    }
}
inline int fff(int x,int y) {
    if(val[x] < val[y]) return y;
    if(val[x] > val[y]) return x;
    return x<y ? x : y;
}
int cal(int l,int r) {
    int k = Log[r-l+1];
    return fff(dp[l][k],dp[r- (1<<k) + 1][k]);
}
int generate_ask(int n,int m,int seedx,int seedy) {
    unsigned x=seedx,y=seedy;
    int L,R;
    int ans = 0;
    for (int i=1; i<=m; ++i) {
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        y ^= y << 13;
        y ^= y >> 17;
        y ^= y << 5;
        L=(x^lastans)%n+1,R=(y^lastans)%n+1;
        if (L>R)swap(L,R);
        //解决询问
        lastans = cal(L,R);
        ans ^= lastans ;
    }
    return ans;
}
void init() {
    for(int i = 1; i<=n; i++) dp[i][0] = i;
    for(int j = 1; (1<<j) <= n; j++) {
        for(int i = 1; i<=n; i++) {//枚举数字
            dp[i][j] = fff(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
    //找到小于等于i的那个二进制
    for(int i = 1; i<=n; i++) {
        int k = 0;
        while((1<<(k+1)) <= i)  k++;
        Log[i] = k;
    }
     
}
int main()
{
    cin>>n>>m>>seed>>seedx>>seedy;
    int ST = clock();
    int ans = 0;
    generate_array(n,seed);
    init();
    cout << generate_ask(n,m,seedx,seedy) << endl;
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/87894139