【HDU-6356】Glad You Came 【线段树】

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 i = 1 n ( i a i ) 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 f i ( i = 1 , 2 , , 3 m ) . The i-th operation of Steve is to update a j as v i if a j < v i ( ( j = l i , l i + 1 , , r i ) ), where
{ l i = min ( ( f 3 i 2 mod n ) + 1 , ( f 3 i 1 mod n ) + 1 ) r i = max ( ( f 3 i 2 mod n ) + 1 , ( f 3 i 1 mod n ) + 1 ) v i = f 3 i mod 2 30 ( 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.

题意:序列a是全为0,之后m次操作,每次将区间 [ l i , r i ] 中小于x的数都替换为x,问m次操作之后的a序列
分析:维护区间最小就可以剪枝一部分。
代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = (int)1e5 + 11;
const int M = (int)15e6 + 11;
const int MOD = (int)1e9 + 7;
const int INF = (int) 0x3f3f3f3f;


unsigned int gung(unsigned int &x, unsigned int &y, unsigned int &z){
    x = x ^ (x << 11);
    x = x ^ (x >> 4);
    x = x ^ (x << 5);
    x = x ^ (x >> 14);
    unsigned int w = x ^ (y ^ z);
    x = y;
    y = z;
    z = w;
    return z;
}
unsigned tag[N << 2], mn[N << 2], f[M];
void up(int rt){
    mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]);
}
void down(int rt){
    if(tag[rt]){
        tag[rt << 1] = max(tag[rt << 1], tag[rt]);
        tag[rt << 1 | 1] = max(tag[rt << 1 | 1], tag[rt]);
        mn[rt << 1] = max(mn[rt << 1], tag[rt]);
        mn[rt << 1 | 1] = max(mn[rt << 1 | 1], tag[rt]);
        tag[rt] = 0;
    }
}
void update(int rt, int L, int R, int le, int ri, unsigned int val){
    if(R < le || L > ri || mn[rt] >= val) return ; // 剪枝
    if(le <= L && R <= ri){
        tag[rt] = max(tag[rt], val);
        mn[rt] = max(mn[rt], val);
        return;
    }
    down(rt);
    int mid = (L + R) >> 1;
    update(rt << 1, L, mid, le, ri, val);
    update(rt << 1 | 1, mid + 1, R, le, ri, val);
    up(rt);
}
ll query(int rt, int L, int R){
    if(L == R) return mn[rt] * 1ll * L;
    int mid = (L + R) >> 1;
    down(rt);
    ll ans = 0;
    ans ^= query(rt << 1, L, mid);
    ans ^= query(rt << 1 | 1, mid + 1, R);
    return ans;
}
int main(){
    int _;
    for(scanf("%d",&_); _; _--){
        unsigned int n, m, x, y, z;  scanf("%u%u%u%u%u", &n,&m,&x, &y, &z);
        memset(tag, 0 ,sizeof(tag));
        memset(mn, 0, sizeof(mn));
        for(int i = 1; i <= 3 * m; i++) f[i] = gung(x ,y ,z);
        for(int i =1; i <= m ;i++){
            unsigned le = min(f[3 * i - 2] % n + 1, f[3 * i  - 1] % n + 1);
            unsigned ri = max(f[3 * i - 2] % n + 1, f[3 * i  - 1] % n + 1);
            unsigned int v = f[3 * i] % (1 << 30);
            update(1, 1, n, le, ri, v);
        }
        printf("%lld\n", query(1, 1, n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37383726/article/details/81479414
今日推荐