HDU-6579 Operation(多校1B)(线性基)

终于又做了一道线性基!

前缀线性基求区间最大异或值

Operation

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1073 Accepted Submission(s): 337

Problem Description
There is an integer sequence a of length n and there are two kinds of operations:
0 l r: select some numbers from al…ar so that their xor sum is maximum, and print the maximum value.

1 x: append x to the end of the sequence and let n=n+1.

Input
There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
For each test case:
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,…,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It’s guaranteed that ∑n≤106,∑m≤106,0≤x<230.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero:
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.

Output
For each type 0 operation, please output the maximum xor sum in a single line.

Sample Input
1
3 3
0 1 2
0 1 1
1 3
0 3 4

Sample Output
1
3

题意:给定一个数组,有两个操作

  1. 1,x 在数组末尾插入一个x(但x需要解码)
  2. 0,l,r 在数组[l,r]之间任取数字进行异或,求出最大可能的异或值(l,r也需要解码)
//#pragma comment(linker, "/STACK:102400000,102400000")
#include "bits/stdc++.h"
#define pb push_back
#define ls l,m,now<<1
#define rs m+1,r,now<<1|1
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}

const int maxn = 5e5+10;
const int mod = 1e9+7;
const double eps = 1e-9;

int f[maxn][32], pos[maxn][32]; //f用来保存f[r]出的前缀线性基,pos[i][j]用来记录f[i][j]在原数组中的位置

inline void add(int i, int x) {
    int k=i;
    for(int j=0; j<=30; ++j) f[i][j]=f[i-1][j], pos[i][j]=pos[i-1][j];
    for(int j=30; j>=0; --j) if(x>>j) {
        if(!f[i][j]) {
            f[i][j]=x, pos[i][j]=k;
            break;
        }
        else {
            if(k>pos[i][j]) swap(k,pos[i][j]), swap(x,f[i][j]); //贪心的将高位相同但位置更靠后的放在高位
            x^=f[i][j];
        }
    }
}

int main() {
    //ios::sync_with_stdio(false);
    int T=read();
    while(T--) {
        int n=read(), m=read();
        for(int i=1; i<=n; ++i) add(i,read());
        int ans=0;
        while(m--) {
            if(read()) add(++n,read()^ans);
            else {
                int l=(read()^ans)%n+1, r=(read()^ans)%n+1; //由于l,r,x是编码过的,所以读入后要记得解码
                if(l>r) swap(l,r);
                ans=0;
                for(int i=30; i>=0; --i)
                    if((ans^f[r][i])>ans&&pos[r][i]>=l) ans^=f[r][i]; //还是正常的求最大值,但是这个基底必须是由[l,r]区间内的数得到的。
                printf("%d\n", ans);
            }
        }
        for(int i=1; i<=n; ++i)
            for(int j=0; j<=30; ++j) f[i][j]=pos[i][j]=0;
    }
}
全部评论

相关推荐

牛客62533758...:华为不卡双非,而是卡院校hhhh
点赞 评论 收藏
分享
05-04 09:38
已编辑
门头沟学院 引擎开发
个人9本海硕,本硕期间一直在投游戏相关实习/校招,岗位由客户端-&gt;引擎-&gt;TA-&gt;AIGC。最终目标肯定是独游制作人,所以程序策划美术都点了些,感觉也没谁了。值此春招末尾总结下技术向校招要点,算是回馈牛客社区了。也附上我的Github和个人博客,欢迎各种交流讨论。&nbsp;前言&nbsp;首先是个人惯例的劝退游戏行业。参见缅怀故人&nbsp;和永远有多远&nbsp;,相比于互联网,游戏薪资大概相当但要求更高,加班严重且更为局限。如果你只是带着一腔热情想入这行,建议先找个日常实习了解下真实的游戏行业再做选择。&nbsp;准备&nbsp;当然,在你决定踏出这步后,第一步就是准备相关的笔试面试。这里先建议找到你感兴趣的公司岗位的JD,然后...
牛客28967172...:说的还是有道理的,我校招时就拿到过网易雷火好几个顶级项目组方向的offer,基本上流程和你说的一样。 但本质还是劝退互联网的游戏方向,本质上是代价更高,而且职业生涯容错率很低,方向比较窄。 代价是众所周知的严重加班,游戏大版本赶工基本上通宵无休,甚至国庆五一都没放假是常态。 职业生涯性价比低是因为游戏行业本质上就是赢家通吃,但你要跳槽只有腾讯网易等头部,要么就是米哈游莉莉丝库洛三七等少数中厂,然后就没了,公司是断崖的少 游戏开发相比互联网方向岗位非常非常少,比如网易整个雷火也才五六百人,里面十几个工作室,招人比例非常低,其他游戏公司也是一样。 而且方向也很窄,你做引擎开发就只能跳相关,你做游戏客户端也只能跳相关(游戏客户端都算吃香的,但市场hc也非常非常少,跳槽机会更少),基本上很难转回互联网 这里对比传统互联网,大厂多的都说不过来,而且容错率很大,你做搜索方向可以跳推荐,你做推荐方向可以跳广告,要求远没有游戏行业那么严,甚至你之前干测试都能跳槽研发方向
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务