求两个数列的子列的交集

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/1104

Description

给两个数列A, B,长度分别为n1, n2,保证A中每个元素互不相同,保证B中每个元素互不相同。。进行Q次询问,每次查找A[l1...r1]和B[l2..r2]的交集 集合 大小是多少。。

比如 A = {1,2,3,4,5,6,7},B = {7,6,5,4,3,2,1}

查询A[2..4]和B[3..5]。。A[2..4] = {2,3,4};B[3..5] = {5,4,3},交集为{3,4},大小为2。。

Input

第一行输入n1,第二行输入n1个数;同样,第三行输入n2,第四行输入n2个数。

第五行输入Q。。接下来Q行输入l1, r1, l2, r2。。

保证 n1, n2, Q在[1, 2×105]范围内,1≤l1≤r1≤n1,1≤l2≤r2≤n2,然后数在[−109,109]范围内。。

Output

输出Q行,表示答案。。

Sample Input

7
1 2 3 4 5 6 7
7
7 6 5 4 3 2 1
1
2 4 3 5

Sample Output

2

HINT

题意

题解:

主席树裸题,查询区间小于a的数有多少个就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200500
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//*************************************************************************************
int a[maxn],b[maxn],lb,n,m;
struct segmenttree
{
int l,r,s;
}tree[maxn*];
int node;
int root[maxn];
int search(int x)
{
int l=,r=lb,mid;
while (l<r)
{
mid=l+r+>>;
if(b[mid]<=x) l=mid;
else r=mid-;
}
return l;
}
int build(int l,int r)
{
int k=++node;
tree[k].s=;
if(l==r) return k;
int m=l+r>>;
if(l<=m) tree[k].l=build(l,m);
if(r>m) tree[k].r=build(m+,r);
return k;
}
int change(int rt,int l,int r,int x)
{
int k=++node,root=k,mid;
tree[k]=tree[rt];
tree[k].s++;
while (l<r)
{
mid=l+r>>;
if(x<=mid)
{
rt=tree[rt].l;
tree[k].l=++node;
k=node;
tree[k]=tree[rt];
tree[k].s++;
r=mid;
}
else
{
rt=tree[rt].r;
tree[k].r=++node;
k=node;
tree[k]=tree[rt];
tree[k].s++;
l=mid+;
}
}
return root;
}
int query(int L,int R,int l,int r,int x)
{
int mid,ans=;
while (l<r)
{
mid=l+r>>;
if(mid>=x)
{
r=mid;
L=tree[L].l;
R=tree[R].l;
}
else
{
l=mid+;
ans+=tree[tree[R].l].s-tree[tree[L].l].s;
L=tree[L].r;
R=tree[R].r;
}
}
if(l==r)
{
if(x<l) return ;
else return ans+tree[R].s-tree[L].s;
}
}
int get(int l,int r,int val)
{
val = search(val);
if(val==)return ;
return query(root[l-],root[r],,lb,val);
}
int A[maxn];
int main()
{ int nn=read();
for(int i=;i<=nn;i++)
A[i]=read();
sort(A+,A+nn+);
int n=read();
for(int i=;i<=n;i++)
{
int x=read();
int l = ,r = nn;
while(l<=r)
{
int mid = (l+r)>>;
if(A[mid]==x)
{a[i]=x;break;}
if(A[mid]<x)l=mid+;
else r = mid-;
}
if(a[i]==)a[i]=inf;
}
lb=;
for(int i=;i<=n;i++)
b[i]=a[i];
lb=;
sort(b+,b++n);
for(int i=;i<=n;i++)
if(b[i]!=b[lb])b[++lb]=b[i];
node = ;
root[]=build(,lb);
for(int i=;i<=n;i++)
root[i]=change(root[i-],,lb,search(a[i]));
int m=read();
for(int i=;i<=m;i++)
{
int n1=read(),n2=read(),n3=read(),n4=read();
printf("%d\n",get(n3,n4,n2)-get(n3,n4,n1-));
}
}

CDOJ 1104 求两个数列的子列的交集 查询区间小于A的数有多少个 主席树的更多相关文章

  1. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  2. SPOJ DQUERY 求区间内不同数的个数 主席树

    这题跟HDU3333差不多吧. 离线的做法很简单,不再说了 以前做过. 主席树的做法就比较暴力了.. 什么是主席树呢.. 其实是某种称号. 在该题中的体现是可持久化的线段树. 对于一个数 如果以前没出 ...

  3. hdu4417 主席树求区间小于等于K

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417   Problem Description Mario is world-famous plum ...

  4. PTA题---求两个有序序列中位数所体现的思想。

    ---恢复内容开始--- 近日,在做PTA题目时,遇到了一个这样的题,困扰了很久.题目如下:已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数.有序序列A​0​​,A​1​​, ...

  5. Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. 求两个有序数组的中位数或者第k小元素

    问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...

  7. 二叉树系列 - 求两节点的最低公共祖先,例 剑指Offer 50

    前言 本篇是对二叉树系列中求最低公共祖先类题目的讨论. 题目 对于给定二叉树,输入两个树节点,求它们的最低公共祖先. 思考:这其实并不单单是一道题目,解题的过程中,要先弄清楚这棵二叉树有没有一些特殊的 ...

  8. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  9. JavaScript求两个数字之间所有数字的和

    这是在fcc上的中级算法中的第一题,拉出来的原因并不是因为有什么好说的,而是我刚看时以为是求两个数字的和, 很显然错了.我感觉自己的文字理解能力被严重鄙视了- -.故拉出来折腾折腾. 要求: 给你一个 ...

随机推荐

  1. Android开发之IP拨号器原理

    IP拨号器,使用了Android的广播接收者(BroadCastReceiver),在广播中把已保存的ip号码放在拨打电话号码的前面(getResultData()),然后把修改后的号码设置到广播中( ...

  2. POJ2528 线段树的区间操作

    首先应该对该[0,10000000]进行离散化 即先将点集进行排序,然后从小到大缩小其中的间距,使得最后点数不会超过2*n 然后就是线段树操作 只需进行染色,然后最后用nlgn进行一个个查询颜色记录即 ...

  3. tyvj1161聚会的名单(trie树)

    背景 Background 明天就是candy的生日,candy又会邀请自己的一大堆好友来聚会了!哎!又要累坏飘飘乎居士了!! 描述 Description     明天就是candy的生日.晚上,c ...

  4. 【转】angular通过$http与服务器通信

    http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View angular是一个前端框架,实现了可交互式的页面,但是对于一个web应用,页面 ...

  5. Android ViewTreeObserver简介

    Android ViewTreeObserver简介   一.结构 public final class ViewTreeObserver extends Object java.lang.Objec ...

  6. C#数据上传方法

    /// <summary> /// 连接成功后开始调用数据上传程序 /// </summary> public void CallDataUpload() { //指定上传日期 ...

  7. Mac OS 终端常用命令【搜藏】

    基础概念 OS X 采用的Unix文件系统,所有文件都挂在跟目录“ /” 下面,所以不在要有Windows 下的盘符概念.比如什么“C:”你在桌面上看到的硬盘都挂在 /Volumes 下.比如接上个叫 ...

  8. 2014年国人开发的最热门的.NET开源项目 TOP 25

    原文地址:http://www.cnphp6.com/archives/72213 1 奎宇工作室 / DotNetCodes C# 一些常用的功能性代码,可以减少许多开发时间,而且类与类之间没有什么 ...

  9. uva 11991 Easy Problem from Rujia Liu? vector+map

    水题 学习一下数据的存储方法. #include<iostream> #include<cstdio> #include<cstdlib> #include< ...

  10. CDR绘制绚丽五角星※※

    CDR绘制绚丽五角星 1.绘制一个五角星,在多边形工具下拉的第二个就是 2.选中五角星,点击颜色即可.给五角星加上颜色 3.用立体化工具进行延伸. 4.点击图形中心向下拉. 看到了中间的一个长方条了没 ...