Codeforces 369E Valera and Queries【逆向思维+离线+树状数组】好题!好题!好题!

来源:互联网 发布:google pixel手机 知乎 编辑:程序博客网 时间:2024/05/21 02:48

E. Valera and Queries
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Valera loves segments. He has recently come up with one interesting problem.

The Ox axis of coordinates has n segments, the i-th segment starts in positionli and ends in positionri (we will mark it as[li, ri]). Your task is to processm queries, each consists of number cnti and a set of cnti coordinates of points located on theOx axis. The answer to the query is the number of segments, such that each of them contains at least one point from the query. Segment[l, r] contains point q, if l ≤ q ≤ r.

Valera found the solution of this problem too difficult. So he asked you to help him. Help Valera.

Input

The first line contains two integers n,m (1 ≤ n, m ≤ 3·105) — the number of segments on the axis of coordinates and the number of queries.

Next n lines contain the descriptions of the segments. Thei-th line contains two positive integers li, ri (1 ≤ li ≤ ri ≤ 106) — the borders of thei-th segment.

Next m lines contain the description of the queries, one per line. Each line starts from integercnti (1 ≤ cnti ≤ 3·105) — the number of points in thei-th query. Then the line contains cnti distinct positive integersp1, p2, ..., pcnti (1 ≤ p1 < p2 < ... < pcnti ≤ 106) — the coordinates of points in the i-th query.

It is guaranteed that the total number of points in all queries doesn't exceed3·105.

Output

Print m non-negative integers, where thei-th number is the response to the i-th query.

Examples
Input
3 31 34 56 73 1 4 72 4 51 8
Output
310

题目大意:


一共有N个区间,有M组询问,每组询问包含一些点,问这组点会在多少个区间内出现(一个区间只会被覆盖一次)。


思路(源自:http://blog.csdn.net/l123012013048/article/details/49314335):

正难则反.

遇事不决先打表.


1、我们正着做很显然有两种查询方式:

①每次拿出一个区间,然后到每组查询中看看是否有点在这个区间内,如果有,ans【这组查询的编号】++即可,这里可以使用二分优化查询速度,但是因为组数比较多,即使二分加速也只能加速一组内的点查询问题,所以时间复杂度还是容不下。

②每次拿出一个组,然后每组中拿出一个点进行查询,询问这个点会在多少个区间内出现过,这个很好处理,但是处理点与点之间去重的问题的时候,就显得非常难了。

既然正着做怎么搞都是那么的难,那么我们不妨引入“正难则反”的思路来想这个题


2、反向思考,那就是对于一组点的查询中,我们查询一共有多少个区间不会被点所覆盖,记数量为x,那么ans=n-x.

对于一组点的查询中,问题转化为求区间:【a[i]+1,a[i+1]-1】所包含的整个区间的个数为多少个。

那么我们考虑将所有区间按照l从大到小排序,然后再按照r从小到大排序。

那么对于排序得到的数组,如果我们查询一个区间【L,R】中有多少个整个区间,那么对应其之前的所有区间的L都一定在这个区间的右边,所以我们只要查询之前的区间中,有多少个区间的r小于等于R即可.

那么这里使用树状数组加速查询。


3、注意如果查询区间【L,R】和原来给出的区间的L,R相等的话,排序优先级是原串优先。

细节处理详情参考代码。主体思路已经尽力描述出来了。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1003000struct node{    int l,r,pos;}a[13080000];int ans[300080];int tree[1004005];//树int cmp(node a,node b){    if(a.l==b.l)    {        if(a.r==b.r)return a.pos<b.pos;        else  return a.r<b.r;    }    else return a.l>b.l;}int lowbit(int x)//lowbit{    return x&(-x);}int sum(int x){    int sum=0;    while(x>0)    {        sum+=tree[x];        x-=lowbit(x);    }    return sum;}void add(int x,int c){    while(x<=N)    {        tree[x]+=c;        x+=lowbit(x);    }}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        int tot=0;        memset(tree,0,sizeof(tree));        memset(ans,0,sizeof(ans));        for(int i=0;i<n;i++)scanf("%d%d",&a[tot].l,&a[tot].r),a[tot++].pos=0;        for(int i=1;i<=m;i++)        {            int pre=-1;            int mi;scanf("%d",&mi);            for(int j=0;j<mi;j++)            {                int x;scanf("%d",&x);                if(pre==-1)                {                    if(x>1)                    {                        a[tot].l=1;a[tot].r=x-1;a[tot++].pos=i;                    }                    pre=x;                }                else                {                    if(x-1>=pre+1)                    {                        a[tot].l=pre+1,a[tot].r=x-1;a[tot++].pos=i;                    }                    pre=x;                }            }            a[tot].l=pre+1,a[tot].r=1000020,a[tot++].pos=i;        }        sort(a,a+tot,cmp);        for(int i=0;i<tot;i++)        {            if(a[i].pos==0)            {                add(a[i].r,1);            }            else            {                ans[a[i].pos]+=sum(a[i].r);            }        }        for(int i=1;i<=m;i++)printf("%d\n",n-ans[i]);    }}










0 0