Cdoj 1104 the intersection of the sub-columns of a two-series query interval is less than a number of the Chairmen tree

Source: Internet
Author: User

to find the intersection of the child columns of the two series

Time Limit:1 Sec

Memory limit:256 MB

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

Give two Series A, B, the length is N1, N2, respectively. 保证A中每个元素互不相同,保证B中每个元素互不相同 Make Q-queries, find A[L1...R1] and b[l2 each time. R2] The intersection collection size is:

For example A = {1,2,3,4,5,6,7},b = {7,6,5,4,3,2,1}

Query a[2..4] and b[3..5].. A[2..4] = {2,3,4};b[3..5] = {5,4,3}, with an intersection of {3,4} and a size of 2.

Input

The first line enters N1, the second line enters the number of N1; Similarly, the third row enters N2, and the fourth line enters the N2 number.

Enter Q in line five. Next Q Line input L1, R1, L2, R2.

Guaranteed N1, N2, q in [1,2x5 ] within the scope of1≤l1≤r1≤n1 ,1≤l2≤r2≤n2 , and then count in the [ −9,9] in the range:

Output

Output Q line, indicating the answer.

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

2HINT

Test instructions

Exercises

Chairman Tree naked, the query interval is less than a number of how many will be good

Code:

//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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200500#defineMoD 1001#defineEPS 1e-9#definePi 3.1415926intNum;//const int INF=0X7FFFFFFF;Constll inf=999999999; inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//*************************************************************************************inta[maxn],b[maxn],lb,n,m;structsegmenttree{intL,r,s;} TREE[MAXN* -];intnode;intROOT[MAXN];intSearchintx) {    intL=0, r=Lb,mid;  while(l<r) {Mid=l+r+1>>1; if(b[mid]<=x) l=mid; Elser=mid-1; }    returnl;}intBuildintLintR) {    intk=++node; Tree[k].s=0; if(L==R)returnK; intM=l+r>>1; if(l<=m) tree[k].l=build (L,M); if(r>m) Tree[k].r=build (m+1, R); returnK;}intChangeintRtintLintRintx) {    intk=++node,root=K,mid; TREE[K]=Tree[rt]; Tree[k].s++;  while(l<r) {Mid=l+r>>1; 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+1; }    }    returnRoot;}intQueryintLintRintLintRintx) {    intmid,ans=0;  while(l<r) {Mid=l+r>>1; if(mid>=x) {R=mid; L=TREE[L].L; R=TREE[R].L; }        Else{L=mid+1; 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 0; Else returnans+tree[r].s-Tree[l].s; }}int Get(intLintRintval) {Val=Search (val); if(val==0)return 0; returnQuery (root[l-1],root[r],1, Lb,val);}intA[MAXN];intMain () {intnn=read ();  for(intI=1; i<=nn;i++) A[i]=read (); Sort (A+1, a+nn+1); intn=read ();  for(intI=1; i<=n;i++)    {        intx=read (); intL =1, r =nn;  while(l<=r) {intMid = (l+r) >>1; if(a[mid]==x) {A[i]=x; Break;} if(a[mid]<x) l=mid+1; Elser = mid-1; }        if(a[i]==0) a[i]=inf; } lb=1;  for(intI=1; i<=n;i++) B[i]=A[i]; LB=1; Sort (b+1, B +1+N);  for(intI=2; i<=n;i++)        if(b[i]!=b[lb]) b[++lb]=C[i]; Node=0; root[0]=build (1, LB);  for(intI=1; i<=n;i++) Root[i]=change (root[i-1],1, Lb,search (A[i])); intm=read ();  for(intI=1; i<=m;i++)    {        intN1=read (), N2=read (), N3=read (), n4=read (); printf ("%d\n",Get(N3,N4,N2)-Get(n3,n4,n1-1)); }}

Cdoj 1104 the intersection of the sub-columns of a two-series query interval is less than a number of the Chairmen tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.