题目大意:

给定长度为n的数列X={x1,x2,...,xn}和长度为m的数列Y={y1,y2,...,ym},令矩阵A中第i行第j列的值\(A_{ij} = x_i \text{ xor } y_j\)每次询问给定矩形区域\(i \in [u,d],j \in [l,r]\)找出第k大的\(A_{ij}\).

题解:

中午会宿舍的时候看了一眼题面hhhhhhhh.

瞄了一眼300000的数据范围就走了hhhhhhhh.

一中午想了快1h就是没做出来hhhhhhhhh.

直到下午来了机房看到了n<=1000 hhhhhh.

维护n颗可持久化01Trie暴力查询即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 1024;
const int maxm = 300010;
struct Node{
Node *ch[2];
int siz;
}*null,*root[maxm];
Node mem[maxm*40],*it;
inline void init(){
it = mem;null = it++;
null->ch[0] = null->ch[1] = null;
null->siz = 0;root[0] = null;
}
inline Node* insert(Node *rt,int x,int d){
Node *p = it++;*p = *rt;p->siz ++ ;
if(d == -1) return p;
int id = (x >> d)&1;
p->ch[id] = insert(p->ch[id],x,d-1);
return p;
}
int cnt;
Node *lnw[maxn],*rnw[maxn];int val[maxn];
int kth(int k,int d){
if(d == -1) return 0;
int tmp = 0;
for(int i=1,id;i<=cnt;++i){
id = (val[i] >> d) & 1;
tmp += rnw[i]->ch[id^1]->siz - lnw[i]->ch[id^1]->siz;
}
if(k <= tmp){
for(int i=1,id;i<=cnt;++i){
id = (val[i] >> d) & 1;
lnw[i] = lnw[i]->ch[id^1];
rnw[i] = rnw[i]->ch[id^1];
}return (1<<d) | kth(k,d-1);
}else{
for(int i=1,id;i<=cnt;++i){
id = (val[i] >> d) & 1;
lnw[i] = lnw[i]->ch[id];
rnw[i] = rnw[i]->ch[id];
}return kth(k - tmp,d-1);
}
}
int a[maxn],b[maxm];
int main(){
init();
int n,m;read(n);read(m);
for(int i=1;i<=n;++i) read(a[i]);
for(int i=1;i<=m;++i){
read(b[i]);
root[i] = insert(root[i-1],b[i],30);
}
int q;read(q);
while(q--){
int u,d,l,r,k;read(u);read(d);
read(l);read(r);read(k);
cnt = 0;
for(int i=u;i<=d;++i){
lnw[++cnt] = root[l-1];
rnw[cnt] = root[r];
val[cnt] = a[i];
}
printf("%d\n",kth(k,30));
}
getchar();getchar();
return 0;
}

bzoj 4103: 异或运算 可持久化Trie的更多相关文章

  1. BZOJ 4103: [Thu Summer Camp 2015]异或运算 可持久化trie

    开始想了一个二分+可持久化trie验证,比正解多一个 log 仔细思考,你发现你可以直接按位枚举,然后在可持久化 trie 上二分就好了. code: #include <bits/stdc++ ...

  2. [BZOJ4103][Thu Summer Camp 2015]异或运算 可持久化Trie树

    4103: [Thu Summer Camp 2015]异或运算 Time Limit: 20 Sec  Memory Limit: 512 MB Description 给定长度为n的数列X={x1 ...

  3. BZOJ 3689: 异或之 可持久化trie+堆

    和超级钢琴几乎是同一道题吧... code: #include <bits/stdc++.h> #define N 200006 #define ll long long #define ...

  4. 【bzoj4103】[Thu Summer Camp 2015]异或运算 可持久化trie树

    Description 给定长度为n的数列X={x1,x2,...,xn}和长度为m的数列Y={y1,y2,...,ym},令矩阵A中第i行第j列的值Aij=xi xor yj,每次询问给定矩形区域i ...

  5. BZOJ 3261: 最大异或和( 可持久化trie )

    搞成前缀和然后就可以很方便地用可持久化trie维护了.时间复杂度O((N+M)*25) -------------------------------------------------------- ...

  6. BZOJ 3261 最大异或和 可持久化Trie树

    题目大意:给定一个序列,提供下列操作: 1.在数组结尾插入一个数 2.给定l,r,x,求一个l<=p<=r,使x^a[p]^a[p+1]^...^a[n]最大 首先我们能够维护前缀和 然后 ...

  7. [十二省联考2019]异或粽子——可持久化trie树+堆

    题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...

  8. 洛谷.5283.[十二省联考2019]异或粽子(可持久化Trie 堆)

    LOJ 洛谷 考场上都拍上了,8:50才发现我读错了题=-= 两天都读错题...醉惹... \(Solution1\) 先求一遍前缀异或和. 假设左端点是\(i\),那么我们要在\([i,n]\)中找 ...

  9. bzoj3261: 最大异或和 可持久化trie

    题意:给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Qlrx:询问操作,你需要找到一个位置p,满 ...

随机推荐

  1. hdu3579(线性同余方程组)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 《C++游戏开发》笔记十一 平滑动画:不再颤抖的小雪花

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9430645 作者:七十一雾央 新浪微博:http:/ ...

  3. [精]poj2724

    Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5027   Accepted: 1455 ...

  4. MATLAB循环结构:break+continue+嵌套

    break语句:终止当前循环,继续执行循环语句的下一语句: continue语句:跳过循环体的后面语句,开始下一个循环: 例:求[100,200]之间第一个能被21整除的整数 :200 %循环语句 ) ...

  5. CSS3中transform,transition和animation的简单介绍和使用方法演示样例

    transform是一个属性,本质跟width,height是一样的,加上transform也就是为类添加一个变换属性. transition是一个属性.它是用来控制过渡效果的,由于用transfor ...

  6. ABAP操作EXCEL (号称超级版)

    [转自http://www.cnblogs.com/VerySky/articles/2170014.html] *------------------------------------------ ...

  7. mysql怎么在已建好的表中添加自增序列

     alter table 表明 change id id int not null auto_increment unique;

  8. 禁用chrome浏览器的cookie

    Chrome: 1.打开chrome浏览器,点击右上角的“自定义和控制Google Chrome”按钮 2.在下拉菜单中选择设置 3.点击设置页底部的“显示高级设置...” 4.在隐私设置下,点击“内 ...

  9. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  10. Java InetAddress.getByAddress()的使用

    import java.net.*; public class NetDemo { public static void main(String[] args) throws Exception{ S ...