1. 程式人生 > >Glad You Came (線段樹)

Glad You Came (線段樹)

Glad You Came

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 768    Accepted Submission(s): 264

Problem Description

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(iai) 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

                             li=min((f[3i−2]modn)+1,(f[3i−1]modn)+1);

                             ri=max((f[3i−2]modn)+1,(f[3i−1]modn)+1);

                             vi=f[3i]mod230(i=1,2,⋯,m).

Input

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.

Output

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

 

Sample Input

4

1 10 100 1000 10000

10 100 1000 10000 100000

100 1000 10000 100000 1000000

1000 10000 100000 1000000 10000000

 

Sample Output

1031463378 1446334207 351511856 47320301347

 

Hint

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.

 

 

Source

2018 Multi-University Training Contest 5

 

題意:

        給你一個整數陣列a[],每個元素初始化為0. 然後進行 m 個操作,更新a[]中的元素,最後輸出 (每個元素a[i]*i) 的 異或和;

m個操作—— 是首先確定f[]陣列(利用題中給的函式算出每個f[i]).然後這m個操作就開始了。(題中紅色文字,複製下來有點不好看)。

利用f[]算出 l,r,v。然後更新 a[] 陣列從 l 到 r 的元素。a[i]=min(a[i],v);

思路:

        線段樹暴力查詢修改;

程式碼:

#include<map>
#include<vector>
#include<cstdio>
#include<string>
#include<math.h>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int MM=1000010;
const int INF=0x3f3f3f3f;
const double PI=3.1415926;
const int MOD=(1<<30);
unsigned f[MM<<4];
ll a[MM];
unsigned X,Y,Z;
unsigned get()
{
    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,l,mid,rt<<1);
    if(R>mid)
        update(L,R,w,mid+1,r,rt<<1|1);
    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,l,mid,rt<<1);
    if(R>mid)
        ans=query(L,R,mid+1,r,rt<<1|1);
    return ans;

}
int main()
{
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        int n,m;
        mem(a,0);
        scanf("%d%d%d%d%d",&n,&m,&X,&Y,&Z);
        for(int i=1; i<=3*m; i++)
        {
            f[i]=get();
        }
        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^=(w*i);
        }

        printf("%lld\n",sum);
    }
    return 0;
}