2018 Multi-University Training Contest 5 HDU - 6356 Glad You Came

本文介绍了解决特定数组更新与查询问题的两种高效算法:线段树剪枝和倍增思想。通过这两种方法,可以在O(log n)的时间复杂度内完成区间更新及查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bryce1010模板

https://vjudge.net/problem/HDU-6356

题意:数组有n个数初始为0,m个询问,每个询问给出L R V(按照给定函数生成),将数组的下标L到R的数与V取较大值,最后输出给定的公式结果。

思路:目前知道的做法有三种,线段树(维护最大值最小值剪枝)、倍增维护区间更新、据说2e5可以覆盖整个随机数所以用nth_element晒出前2e5大的数。

1、线段树剪枝

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const unsigned int MOD= 1<<30;
const unsigned int MAXN=3*5e6+10;
const int MAXM=1e5+10;
const int INF=0x3f3f3f3f;
unsigned int F[MAXN];
unsigned int  L[MAXN],R[MAXN],V[MAXN];
unsigned int  a[MAXN];
unsigned int X,Y,Z;
unsigned int RNG61()
{
    X=X^(X<<11);
    X=X^(X>>4);
    X=X^(X<<5);
    X=X^(X>>14);
    unsigned int W;
    W=X^Y^Z;
    X=Y;
    Y=Z;
    Z=W;
    return Z;
}
//建立区间更新线段树,维护最大值和最小值
struct Node
{
    int l,r;
    unsigned int Min,Max,lazy;
}node[MAXM<<2];

void PushUp(int rt)
{
    node[rt].Min=min(node[rt<<1].Min,node[rt<<1|1].Min);
    node[rt].Max=max(node[rt<<1].Max,node[rt<<1|1].Max);

}


void PushDown(int rt)
{
    if(node[rt].lazy)
    {
        node[rt<<1].lazy=node[rt].lazy;
        node[rt<<1|1].lazy=node[rt].lazy;
        node[rt<<1].Min=node[rt].lazy;
        node[rt<<1|1].Min=node[rt].lazy;

        node[rt<<1].Max=node[rt].lazy;
        node[rt<<1|1].Max=node[rt].lazy;
        node[rt].lazy=0;
    }
}
void build(int l,int r,int rt)
{
    node[rt].lazy=0;
    node[rt].Max=0;
    node[rt].Min=0;
    node[rt].l=l;
    node[rt].r=r;
    if(l==r)
    {
        return;

    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);

}
//区间更新
void update(int L,int R,int c,int l,int r,int rt=1)
{
    // 如果区间最小值大于vi则截断剪枝
    if(node[rt].Min>=c)
        return;
    if(L<=l&&r<=R)
    {
        if(node[rt].Max<c)
        {
            node[rt].lazy=c;
            node[rt].Max=c;
            node[rt].Min=c;
            return;
        }
    }
    PushDown(rt);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(m<R)update(L,R,c,rson);
    PushUp(rt);
}

void query(int L,int R,int l,int r,int rt=1)
{
    if(node[rt].Max==node[rt].Min)
    {
        for(int i=node[rt].l;i<=node[rt].r;i++)
        {
            a[i]=node[rt].Max;
        }
        return;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    if(L<=m)
        query(L,R,lson);
    if(R>m)
        query(L,R,rson);
}




int main()
{
    unsigned int n,m;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        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]=RNG61();
//            cout<<F[i]<<" ";
        }
//        cout<<endl;
        build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            L[i]=min(F[3*i-2]%n+1,F[3*i-1]%n+1);
            R[i]=max(F[3*i-2]%n+1,F[3*i-1]%n+1);
            V[i]=F[3*i]%MOD;
//            cout<<L[i]<<" "<<R[i]<<" "<<V[i]<<endl;
            update(L[i],R[i],V[i],1,n,1);
        }

//        for(int i=1;i<=n;i++)
//            cout<<a[i]<<" ";
//        cout<<endl;
        ll sum=0;
        query(1,n,1,n,1);
        for(int i=1;i<=n;i++)
            sum=sum^((ll)i*(ll)a[i]);
        cout<<sum<<endl;
    }

    return 0;
}

2. 倍增思想

a[i][j]表示以i为开始位置,长度为j的区间

# include <bits/stdc++.h>
# define lson l,mid,id<<1
# define rson mid+1,r,id<<1|1
using namespace std;
typedef long long LL;
typedef unsigned int ui;
const int maxn = (5e6+30)*3;
ui x, y, z, a[100003][18], b[maxn];
int lg[100003]={0};
ui fun()
{
    x = x^(x<<11);
    x = x^(x>>4);
    x = x^(x<<5);
    x = x^(x>>14);
    ui w = x^(y^z);
    x = y;
    y = z;
    z = w;
    return z;
}
int main()
{
    int T, n, m;
    for(int i=2; i<=100000; ++i) lg[i] = lg[i>>1]+1;
    for(scanf("%d",&T); T; --T)
    {
        scanf("%d%d%u%u%u",&n,&m,&x,&y,&z);
        int mx = max(n, 3*m), imax=0;
        memset(a, 0, sizeof(a));
        for(int i=1; i<=mx; ++i)
            b[i] = fun();
        for(int i=1; i<=m; ++i)
        {
            int l = min(b[3*i-2]%n+1, b[3*i-1]%n+1);
            int r = max(b[3*i-2]%n+1, b[3*i-1]%n+1);
            ui vi = b[3*i]%(1<<30);
            int d = lg[r-l+1];
            r = r+1-(1<<d);
            a[l][d] = max(a[l][d], vi);
            a[r][d] = max(a[r][d], vi);
            imax = max(imax, d);
        }
        for(int i=imax; i>=1; --i)
        {
            for(int j=1, d=1<<i; j<=n; ++j)
            {
                if(j+d-1>n) break;
                a[j][i-1] = max(a[j][i-1], a[j][i]);
                a[j+(d>>1)][i-1] = max(a[j+(d>>1)][i-1], a[j][i]);
            }
        }
        LL ans = 0;
        for(int i=1; i<=n; ++i)
           ans ^= 1LL*i*a[i][0];
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值