Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)

题目大意:

给出很多条分布在 x 轴上的线段。

然后给出很多点集,问这些点集分布在多少条不同的线段上。

思路分析:

把点集分散成若干条线段。

如果点集做出的线段包含了某一条给出的线段的话,也就是说这个点集上不会有点在这条线段上。

所以我们就是求出 点集做出的线段包含了多少个给出的线段就可以了。

那么也就是比较l r的大小,排序之后用BIT

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define lowbit(x) (x&(-x))
#define maxn 1000005

using namespace std;

struct node
{
    int l,r,p;
    bool operator < (const node &cmp)const
    {
        if(l!=cmp.l)return l>cmp.l;
        if(r!=cmp.r)return r<cmp.r;
        return p<cmp.p;
    }
} line[maxn];
int bit[maxn],ans[maxn],n,m,S=1e6+1;
int sum(int x)
{
    int res=0;
    for(;x;x-=lowbit(x))res+=bit[x];
    return res;
}
int add(int x)
{
    for(;x<=S;x+=lowbit(x))bit[x]++;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d%d",&line[i].l,&line[i].r);
    int T=n,cnt,p,x;
    for(int i=1;i<=m;i++)
    {
        ans[i]=n;
        scanf("%d%d",&cnt,&p);
        if(p>1)line[++T].l=1,line[T].r=p-1,line[T].p=i;
        for(;--cnt;p=x)
        {
            scanf("%d",&x);
            if(x>p+1)line[++T].l=p+1,line[T].r=x-1,line[T].p=i;
        }
        line[++T].l=p+1,line[T].r=S,line[T].p=i;
    }
    sort(line+1,line+1+T);
    for(int i=1;i<=T;i++)
        if(line[i].p)
            ans[line[i].p]-=sum(line[i].r);
        else add(line[i].r);
    for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
    return 0;
}

Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)

时间: 2024-08-28 02:28:39

Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)的相关文章

Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求不包含这个cnt个点的线段的总数, 那么不包含这些点的线段必然在cnt个点之间(这里需要再加两个点一个是0, 一个是MAX), 我们可以把所有线段按Ri 分类, 然后按右端点遍历,对于当前的线段可以在Li 处+1, 然后对于每一次询问中两个点(x, y)之间线段的个数, 只需要查询 左端点大于等于x

Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak has fallen in love with an attractive girl called Parmida s

Codeforces Round #258 (Div. 2) A. Game With Sticks(数学题)

题目链接:http://codeforces.com/contest/451/problem/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Description Input Output Sample Input 51 2 1 2 1 Sample Output 1 1 1 2 2 题解:这个题有做慢了,这种题做慢了和没做出来区别不大... 读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那

Codeforces Round #216 (Div. 2)---C. Valera and Elections

The city Valera lives in is going to hold elections to the city Parliament. The city has n districts and n?-?1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in

Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer number in range[1, a

Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)

http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\sum_{i=0}^{n}(n-i)(m-i) $ 化简得:$\left [ n(n+1)-\frac{n(n+1)}{2}\right ]*m+\frac{n(n+1)(n+2)}{6}-\frac{n(n+1)}{2}*n$ 将n作为小的数,枚举n即可. 1 #include<iostream>

Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)

http://codeforces.com/problemset/problem/150/B 题意: 给出n,m,k,n表示字符串的长度为n,m表示字符种类个数,k表示每k个数都必须是回文串,求满足要求的不同字符串有多少种. 思路:分奇偶推一下,当k为偶数时,容易发现如果n=k,那么有最多有k/2种不同的字符可填,如果n>k,你会发现此时所有位置都必须一样. 奇数的话会稍微麻烦一点,如果n=k,那么最多有k/2+1种不同的字符可填,如果n>k,你会发现此时最后只有2中不同的字符可填. 1 #i