题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点

解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询的任意一个点的。这时候我们可以建立点集的补集,以线段的形式,如果点集的补集线段包含了某条给出的线段,那么被包含的那条线段肯定不会包括任意一个点,那么该组查询的答案ans--即可。 用树状数组做,离线读入数据,按容易被包含的线段优先排个序,然后扫一遍,边统计边修改即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 1000007 int c[N],n,m,ans[],maxi;
struct node
{
int l,r,ind;
}a[N]; int lowbit(int x){ return x&-x; } void modify(int x)
{
while(x <= maxi)
{
c[x]++;
x += lowbit(x);
}
} int getsum(int x)
{
int ans = ;
while(x > )
{
ans += c[x];
x -= lowbit(x);
}
return ans;
} int cmp(node ka,node kb) //容易被覆盖的线段放在前面
{
if(ka.l == kb.l)
{
if(ka.r == kb.r)
return ka.ind < kb.ind;
return ka.r < kb.r;
}
return ka.l > kb.l;
} int main()
{
int i,j,k,x,pre,cnt;
maxi = N-;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d%d",&a[i].l,&a[i].r),a[i].ind = ;
memset(ans,,sizeof(ans));
memset(c,,sizeof(c));
int tot = n;
for(i=;i<=m;i++)
{
scanf("%d",&cnt);
scanf("%d",&x);
if(x > )
a[++tot].l = , a[tot].r = x-, a[tot].ind = i;
pre = x;
for(j=;j<cnt;j++)
{
scanf("%d",&x);
if(x- >= pre+)
a[++tot].l = pre+, a[tot].r = x-, a[tot].ind = i;
pre = x;
}
a[++tot].l = pre+, a[tot].r = maxi, a[tot].ind = i;
}
sort(a+,a+tot+,cmp);
for(i=;i<=tot;i++)
{
if(a[i].ind > )
ans[a[i].ind] += getsum(a[i].r);
else
modify(a[i].r);
}
for(i=;i<=m;i++)
printf("%d\n",n-ans[i]);
}
return ;
}

Codeforces 369E Valera and Queries --树状数组+离线操作的更多相关文章

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

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  2. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  3. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  4. CodeForces 828E DNA Evolution(树状数组)题解

    题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...

  5. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  6. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  7. Codeforces 635D Factory Repairs【树状数组】

    又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...

  8. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  9. codeforces E. DNA Evolution(树状数组)

    题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...

随机推荐

  1. 四、MyBatis主配置文件

    //备注:该博客引自:http://limingnihao.iteye.com/blog/1060764 在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码 收藏 ...

  2. php学习笔记:对文件的增删查改等操作

    文件的创建: 采用touch()函数,当文件不存在会被创建 例如: <?php header("Content-type: text/html; charset=utf-8" ...

  3. 将HTML5封装成android应用APK文件的几种方法(转)

    作为下一代的网页语言,HTML5拥有很多让人期待已久的新特性.HTML5的优势之一在于能够实现跨平台游戏编码移植,现在已经有很多公司在移动 设备上使用HTML5技术.随着HTML5跨平台支持的不断增强 ...

  4. Rendering Problems: No Android SDK found. Please configure an Android SDK. 怎解决?

    Rendering Problems No Android SDK found. Please configure an Android SDK.

  5. .net学习总结

    .NET 学前入门 了解.Net能做什么 了解.NET,C#语言及其特点(分清.NET和C#的关系),对.Net学习有系统全面的认识. C#基础 变量,赋值运算符.数据类型转换等. 选择结构控制(if ...

  6. Windows Azure 上传 VM

    One of the great features of Windows Azure is VHD mobility. Simply put it means you can upload and d ...

  7. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q127-Q130)

    Question 127You create a custom list named Products.You need to perform a Representational State Tra ...

  8. Swift开发第六篇——操作运算符也可以重载& func 的参数修饰

    本篇分为两部分: 1.Swift 中重载操作运算符的使用 2.Swfit 中 func 的参数修饰 1.Swift 中重载操作运算符的使用 与别的语言不同,Swift 支持运算符的重载,运算符指的是“ ...

  9. UIButton 内部介绍

    **     设置内容距btn边框距离     **/    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);        /** 将lab ...

  10. Android bitmap高效显示和优化

    第一部分:Bitmap高效显示 应用场景:有时候我们想在界面上显示一个网络图片或者显示一张本地的图片,但是图片本身是很大的有几兆,但是显示的位置很小或者说我们可以用更小的图片来满足这样的需求,如果把整 ...