CSU1955: 一棵数据结构

来源:互联网 发布:java项目流程怎么说 编辑:程序博客网 时间:2024/06/06 00:11

1955: 一棵数据结构

        Time Limit: 12 Sec     Memory Limit: 512 Mb     Submitted: 19     Solved: 5    

Description

长者对小明施加了膜法,使得小明每天起床就像马丁的早晨一样。 今天小明早上醒来后发现自己变成了一名数据结构大师!

给出两个数组A和B,均有n个元素。之后有m次查询,对于每次查询有4个整数L, R, x, y 对于每组查询,你需要计算有多少个下标k ∈ [L, R],且满足A[k]≥xB[k]≥y

Input

第一行包含一个整数T,表示T组测试数据 对于每组测试数据,第一行两个整数n, m(1 ≤ n, m ≤ 105)。 第二行n个整数Ai(1 ≤ Ai ≤ 105)。 第三行n个整数Bi(1 ≤ Bi ≤ 105)。 接下来m行,每行4个整数L, R, x, y(1 ≤ L ≤ R ≤ n, 1 ≤ x, y ≤ 105),表示查询。

Output

对于每次查询输出结果。

Sample Input

15 51 2 3 4 57 6 5 4 31 2 1 11 5 1 12 4 3 42 2 2 63 3 3 3

Sample Output

25211

Hint

Source

2017年湖南多校对抗赛第12场

Author

csust


(三维偏序CDQ分治+树状数组)

#include"cstdio"#include"cstring"#include"iostream"#include"algorithm"#define lson l,mid#define rson mid+1,rusing namespace std;typedef long long ll;const int maxn = 1e5;const int maxm = 1e6+7;int n,m;int a[maxn+3], b[maxn+3], vx[maxn+3], vy[maxn+3], ans[maxn+3];int cnt,acnt;struct node{    int type,id,x,y,aid;    node(){}    node(int t, int i, int xx, int yy, int ai){        type = t;        id = i;        x = xx;        y = yy;        aid = ai;    }    bool operator < (const node & rhs) const {        if(id == rhs.id)            return type <  rhs.type;        return id < rhs.id;    }}que[maxm],temp[maxm];void add(int id, int v){    for(int i = id; i <= maxn; i+=i&(-i))        a[i] += v;}void Clear(int id){    for(int i = id; i <= maxn; i+=i&(-i)){        if(a[i]) a[i] = 0;        else break;    }}int query(int id){    int ret = 0;    for(int i = id; i > 0; i-=i&(-i))        ret += a[i];    return ret;}bool cmp(node a, node b){    return a.x > b.x;}void cdq(int l, int r){    if(l==r) return;    int mid = l+r>>1;    cdq(lson);  cdq(rson);    int p = l,  q = mid+1, now = l;    while(p <= mid && q <= r){        if(que[p].x >= que[q].x){            if(que[p].type == -2) add(que[p].y,1);            temp[now++] = que[p++];        }        else{            if(que[q].type == -1 || que[q].type == 1)                ans[que[q].aid] += que[q].type* (query(maxn) - query(que[q].y-1));            temp[now++] = que[q++];        }    }    while(p <= mid) temp[now++] = que[p++];    while(q <= r){        if(que[q].type == -1 || que[q].type == 1)            ans[que[q].aid] += que[q].type* (query(maxn) - query(que[q].y-1));        temp[now++] = que[q++];    }    for(int i = l; i <= r; i++){        que[i] = temp[i];        if(temp[i].type == -2) Clear(que[i].y);    }}int main(){    int T,x,y,l,r;    scanf("%d",&T);    while(T--){        cnt = 0;        memset(ans,0,sizeof(ans));        scanf("%d%d",&n,&m);        for(int i = 1; i <= n; i++)            scanf("%d",&vx[i]);        for(int i = 1; i <= n; i++)            scanf("%d",&vy[i]);        for(int i = 1; i <= n; i++){            que[++cnt] = node(-2,i,vx[i],vy[i],0);        }        for(int i = 1; i <= m; i++){            scanf("%d%d%d%d",&l,&r,&x,&y);            que[++cnt] = node(-1,l-1,x,y,i);            que[++cnt] = node(1,r,x,y,i);        }        sort(que+1,que+1+cnt);        cdq(1,cnt);        for(int i = 1; i <= m; i++)            printf("%d\n",ans[i]);    }    return 0;}


原创粉丝点击