zoukankan      html  css  js  c++  java
  • 51Nod--1295 XOR key (可持久化tire树)

    题目链接 1295 XOR key 

    可持久化tire树模版题

    数组一定要开够 不然数组不够的话就容易tle 吃了两次亏

    #include<bits/stdc++.h>
    using namespace std;
    #define maxn 500000
    #define LL long long
    struct ac{
       LL sum,nex[3];
       void init(){
          sum=0;
          memset(nex,0,sizeof(nex));
       }
    }tre[maxn*3];
    LL tot,root[maxn];
    void init(){
       memset(tre,0,sizeof(tre));
       tot=0;
       memset(root,0,sizeof(root));
    }
    void add(LL x,LL y,LL &z){
       z=++tot;
       tre[z].init();
       tre[z].sum=tre[y].sum+1;
       LL k=z;
       for(LL j=31;j>=0;j--){
          bool fa=((1LL*1<<j)&x);
          tre[k].nex[fa^1]=tre[y].nex[fa^1];
          tre[k].nex[fa]=++tot;
          k=tot;
          y=tre[y].nex[fa];
          tre[k].sum=tre[y].sum+1;
       }
    }
    LL query(LL l,LL r,LL x){
       LL ans=0;
       for(LL j=31;j>=0;j--){
          bool fa=((1LL*1<<j)&x);
          LL ll=tre[l].nex[fa^1];
          LL rr=tre[r].nex[fa^1];
          LL z=tre[rr].sum-tre[ll].sum;
          if(z){
             ans+=1LL*1<<j;
             l=ll,r=rr;
          }else{
             l=tre[l].nex[fa];
             r=tre[r].nex[fa];
          }
    
       }
       return ans;
    }
    int main(){
       LL n,m;
       cin>>n>>m;
       init();
       for(LL j=1;j<=n;j++){
          long long x;
          scanf("%lld",&x);
          add(x,root[j-1],root[j]);
       }
       for(LL j=1;j<=m;j++){
           LL l,r,x;
           scanf("%lld%lld%lld",&x,&l,&r);
           printf("%lld
    ",query(root[l],root[r+1],x));
       }
    }
  • 相关阅读:
    爬虫的基本原理
    爬虫的分类
    gcc编译
    C++字符串总结
    PE文件格式学习笔记
    学习SDR过程中的参考网页
    Linux下源码编译安装遇到的问题
    web | jsp考试复习要点整理
    爬虫 | php封装 | file_get_contents
    re | [NPUCTF2020]EzObfus-Chapter2
  • 原文地址:https://www.cnblogs.com/Dvelpro/p/9797419.html
Copyright © 2011-2022 走看看