杭电多校第五场 Glad You Came(线段树+剪枝)

Glad You Came

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


 

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

⎧⎩⎨⎪⎪lirivi=min((f3i−2modn)+1,(f3i−1modn)+1)=max((f3i−2modn)+1,(f3i−1modn)+1)=f3imod230(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

题意:n个数初始都为0,每次随机生成一个区间l,r和v,把区间[l,r]里所有小于等于v的数都改为v,最后输出求n个数的 ( i×a[i] )异或和

思路:线段树维护区间的最小值和最大值,某个区间的最小值大于v,就不用更新,某个区间的最大值小于v,区间成段更新,加lazy标记,否则更新到子节点

#include <bits/stdc++.h>

using namespace std;
typedef unsigned int UI;
typedef long long LL;
const int MAXN = 1e5 + 100;
const int MAXM = 3 * 5e6 + 100;
const UI MOD = (1 << 30);
struct node
{
    int l,r;
    UI Min,Max,lazy;
}tree[MAXN << 2];
UI ans[MAXN];
UI f[3 * MAXM];
UI x,y,z;
UI fun()
{
    LL w;
    x = x ^ (x << 11);
    x = x ^ (x >> 4);
    x = x ^ (x << 5);
    x = x ^ (x >> 14);
    w = x ^ (y ^ z);
    x = y;
    y = z;
    z = w;
    return z;
}
void pushup(int num)
{
    tree[num].Max = max(tree[num << 1].Max,tree[num << 1 | 1].Max);
    tree[num].Min = min(tree[num << 1].Min,tree[num << 1 | 1].Min);
}
void build(int num,int l,int r)
{
    tree[num].l = l;
    tree[num].r = r;
    tree[num].Max = tree[num].Min = tree[num].lazy = 0;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(num << 1,l,mid);
    build(num << 1 | 1,mid + 1,r);
}
void pushdown(int num)
{
    if(tree[num].lazy) {
        tree[num << 1].Max = tree[num << 1].Min = tree[num << 1].lazy = tree[num].lazy;
        tree[num << 1 | 1].Max = tree[num << 1 | 1].Min = tree[num << 1 | 1].lazy = tree[num].lazy;
        tree[num].lazy = 0;
    }
}

void update(int num,int l,int r,UI v)
{
    if(tree[num].Min >= v) return;
    if(tree[num].l == l && tree[num].r == r) {
        if(tree[num].Max <= v) {
            tree[num].Max = tree[num].Min = tree[num].lazy = v;
            return;
        }
        if(tree[num].Min >= v) return;
    }
    pushdown(num);
    int mid = (tree[num].l + tree[num].r) >> 1;
    if(r <= mid) update(num << 1,l,r,v);
    else if(l > mid) update(num << 1 | 1,l,r,v);
    else {
        update(num << 1,l,mid,v);
        update(num << 1 | 1,mid + 1,r,v);
    }
    pushup(num);
}
void query(int num,int l,int r)
{
    if(tree[num].Min == tree[num].Max) {
        for(int i = l; i <= r; i++) {
            ans[i] = tree[num].Min;
        }
        return;
    }
    pushdown(num);
    int mid = (l + r) >> 1;
    query(num << 1,l,mid);
    query(num << 1 | 1,mid + 1,r);
}
int main(void)
{
    int T,n,m;
    UI l,r,v;
    scanf("%d",&T);
    while(T--) {
        scanf("%d %d %u %u %u",&n,&m,&x,&y,&z);
        for(int i = 1; i <= 3 * m; i++) {
            f[i] = fun();
        }
        build(1,1,n);
        for(int i = 1; i <= m; i++) {
            l = min(f[3 * i - 2] % n,f[3 * i - 1] % n) + 1;
            r = max(f[3 * i - 2] % n,f[3 * i - 1] % n) + 1;
            v = f[3 * i] % MOD;
            if(v == 0) continue;
            update(1,l,r,v);
        }
        query(1,1,n);
        LL res = 0;
        for(int i = 1; i <= n; i++) {
            res = res ^ (i * (LL)ans[i]);
        }
        printf("%lld\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/81480100
今日推荐