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

本文介绍了一种解决区间覆盖查询问题的有效算法。通过反向思维,将问题转化为计算未被点覆盖的区间数目,利用树状数组优化查询过程,实现高效解答。

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 position li and ends in position ri (we will mark it as [li, ri]). Your task is to process m queries, each consists of number cnti and a set of cnti coordinates of points located on the Ox 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. The i-th line contains two positive integers li, ri (1 ≤ li ≤ ri ≤ 106) — the borders of the i-th segment.

Next m lines contain the description of the queries, one per line. Each line starts from integer cnti (1 ≤ cnti ≤ 3·105) — the number of points in the i-th query. Then the line contains cnti distinct positive integers p1, 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 exceed 3·105.

Output

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

Examples
Input
3 3
1 3
4 5
6 7
3 1 4 7
2 4 5
1 8
Output
3
1
0

题目大意:


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


思路(源自:http://blog.youkuaiyun.com/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 1003000
struct 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]);
    }
}










### CodeForces 上关于树状数组目列表 在CodeForces平台上,存在多个涉及树状数组的数据结构问。以下是部分精选目及其简短描述: #### 1. **799C - Fountains** 此要求实现单点更新操作,在`A[]`数组中修改某一位置的值并相应地更新辅助数组`tree[]`。具体来说,当执行增加操作时,会通过循环不断更新受影响节点直到超出范围为止[^1]。 ```cpp void add(int k, int num) { while (k <= n) { tree[k] += num; k += k & (-k); } } ``` #### 2. **1042D - Array and Operations** 该挑战涉及到利用树状数组或线段树来高效处理特定类型的查询请求。核心思路在于将原始问转化为计算前缀和中小于给定阈值的数量统计问,从而简化了解决方案的设计过程[^2]。 #### 3. **1635F - Closest Pair** 这道难不仅考察了参赛者对树状数组的理解程度,还测试了其解决复杂逻辑的能力。目背景设定在一个有序序列基础上,目标是在指定范围内找到具有最小化评估函数的一对索引组合[^3]。 #### 4. **703D - Mishka and Interesting Contest** 本聚焦于如何有效地求解区间内所有不同数值的异或结果。采用的方法是预先按右端点排序各区间,并记录每种元素最后一次出现的位置;之后借助树状数组完成后续所需的快速查找与更新工作[^4]。 这些例子展示了树状数组作为一种强大工具的应用场景,能够显著提升算法效率特别是在面对大规模数据集的情况下表现尤为突出。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值