2018杭电多校:G: Glad You Came

题目描述

Steve has an integer array a of length n (1-based). He assigned all the elements as zero at the beginning. After that, he made m operations, each of which is to update an interval of a with some value. You need to figure out ⨁ni=1(i⋅ai) after all his operations are finished, where ⨁ means the bitwise exclusive-OR operator.
In order to avoid huge input data, these operations are encrypted through some particular approach.
There are three unsigned 32-bit integers X,Y and Z which have initial values given by the input. A random number generator function is described as following, where ∧ means the bitwise exclusive-OR operator, << means the bitwise left shift operator and >> means the bitwise right shift operator. Note that function would change the values of X,Y and Z after calling.

Let the i-th result value of calling the above function as fi (i=1,2,⋯,3m). The i-th operation of Steve is to update aj as vi if aj<vi (j=li,li+1,⋯,ri), where

⎧⎩⎨⎪⎪lirivi=min((f3i−2modn)+1,(f3i−1modn)+1)=max((f3i−2modn)+1,(f3i−1modn)+1)=f3imod230(i=1,2,⋯,m).

输入

The first line contains one integer T, indicating the number of test cases.
Each of the following T lines describes a test case and contains five space-separated integers n,m,X,Y and Z.
1≤T≤100, 1≤n≤105, 1≤m≤5⋅106, 0≤X,Y,Z<230.
It is guaranteed that the sum of n in all the test cases does not exceed 106 and the sum of m in all the test cases does not exceed 5⋅107.

输出

For each test case, output the answer in one line.

样例输入

4
1 10 100 1000 10000
10 100 1000 10000 100000
100 1000 10000 100000 1000000
1000 10000 100000 1000000 10000000

样例输出

1031463378
1446334207
351511856
47320301347

提示

In the first sample, a = [1031463378] after all the operations.
In the second sample, a = [1036205629, 1064909195, 1044643689, 1062944339, 1062944339, 1062944339, 1062944339, 1057472915, 1057472915, 1030626924] after all the operations.

解题:根据所给条件先设定所要有的原数组,构造线段树。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
const int MOD=1<<30;//不能用define
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef  long long LL;
LL a[maxn];
unsigned f[maxn<<4];
unsigned X,Y,Z;
unsigned RNG(){
    X=X^(X<<11);
    X=X^(X>>4);
    X=X^(X<<5);
    X=X^(X>>14);
    unsigned W=X^(Y^Z);
    X=Y;
    Y=Z;
    Z=W;
    return Z;
} 
void push(int rt){
    a[rt]=min(a[rt<<1],a[rt<<1|1]);
}
void update(int L,int R,LL w,int l,int r,int rt){
    if (a[rt]>=w)
       return ;
    if (l==r){
        a[rt]=w;
        return;
    }
    int mid=(l+r)>>1;
    if (L<=mid)
       update(L,R,w,lson);
    if (R>mid)
       update(L,R,w,rson);
    push(rt);
}
LL query(int L,int R,int l,int r,int rt){
    if (L<=l&&r<=R)
        return a[rt];
    LL ans=0;
    int mid=(l+r)>>1;
    if (L<=mid)
       ans=query(L,R,lson);
    if (R>mid)
       ans=query(L,R,rson);
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--){
        int n,m;
        memset(a,0,sizeof(a));
        scanf("%d%d%d%d%d",&n,&m,&X,&Y,&Z);
        for (int i=1;i<=3*m;i++){
            f[i]=RNG();
        }
        for (int i=1;i<=m;i++){
            int l=min((f[3*i-2]%n)+1,(f[3*i-1]%n)+1);
            int r=max((f[3*i-2]%n)+1,(f[3*i-1]%n)+1);
            int v=f[3*i]%MOD;
            update(l,r,v,1,n,1);
        }
        LL sum=0;
        for (int i=1;i<=n;i++){
            LL w=query(i,i,1,n,1);
            sum^=(1LL*w*i);
        }
        printf("%lld\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40911499/article/details/81480081