51nod 1295(可持久化字典树)

题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1295

思路:板子题

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(1e9+7)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
const int N=3000008;
struct as{
int v;
int ne[2];
void init() {v=0;REW(ne,-1);}
}tr[N];
int pos,rt[50008],n,q,l,r,x;
void push(int now){tr[now].v=tr[tr[now].ne[0]].v+tr[tr[now].ne[1]].v;}
void add(int pre,int &now,int i,int x)
{
    now=++pos;
    tr[now].v=tr[pre].v;
    if(i<0) {tr[now].v++;return;}
    int ant=(x>>i)&1;
    tr[now].ne[ant^1]=tr[pre].ne[ant^1];
    add(tr[pre].ne[ant],tr[now].ne[ant],i-1,x);
    push(now);
}
int query(int l,int r,int i,int x)
{
    if(i<0) return 0;
    int ant=(x>>i)&1;
    int sum=tr[tr[r].ne[ant^1]].v-tr[tr[l].ne[ant^1]].v;
    if(sum) return query(tr[l].ne[ant^1],tr[r].ne[ant^1],i-1,x)+(1<<i);
    else return query(tr[l].ne[ant],tr[r].ne[ant],i-1,x);
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q;
    FOR(i,1,n) si(x),add(rt[i-1],rt[i],31,x);
    while(q--)
    {
        si(x),si(l),si(r);
        l++,r++;
        printf("%d\n",query(rt[l-1],rt[r],31,x));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值