题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数。

由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来。

首先反着做,先求不包含这个cnt个点的线段的总数, 那么不包含这些点的线段必然在cnt个点之间(这里需要再加两个点一个是0, 一个是MAX),

我们可以把所有线段按R分类, 然后按右端点遍历,对于当前的线段可以在Li 处+1, 然后对于每一次询问中两个点(x, y)之间线段的个数,

只需要查询 左端点大于等于x的个数就好了,这里因为是按右端点从小到大遍历的,所以不用考虑用端点了。

发现, 大多数的离线处理都是先把其中的一维从小到大排序, 然后处理另一维。 这样相当于降维。

 #include <bits/stdc++.h>

 const int maxn = 1e6 + ;
typedef std::pair <int, int> pii;
std::vector <int> seg[maxn], q[maxn];
std::vector <pii> rq[maxn];
namespace FenwickTree{
int arr[maxn];
void inc(int x, int d){
while (x < maxn){
arr[x] += d;
x += x & -x;
}
}
int query(int x){
int res = ;
while (x > ){
res += arr[x];
x -= x & -x;
}
return res;
}
int query(int ua, int ub){
return query(ub) - query(ua-);
}
}
int ans[maxn];
void init(){
memset(ans, , sizeof (ans));
for (int i = ; i < maxn; i++){
seg[i].clear();
q[i].clear();
rq[i].clear();
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, m;
while (~ scanf ("%d%d", &n, &m)){
init();
for (int i = ; i < n; i++){
int ua, ub;
scanf("%d%d", &ua, &ub);
ua++, ub++;
seg[ub].push_back(ua);
}
for (int i = ; i < m; i++){
int cnt, p;
scanf ("%d", &cnt);
q[i].push_back();
while (cnt--){
scanf ("%d", &p);
p++;
q[i].push_back(p);
}
q[i].push_back(maxn-);
for (int j = ; j < q[i].size()-; j++){
int ua = q[i][j]+;
int ub = q[i][j+]-;
rq[ub].push_back(std::make_pair(ua, i));
}
}
for (int i = ; i < maxn; i++){
for (int j = ; j < seg[i].size(); j++){
int tmp = seg[i][j];
FenwickTree::inc(seg[i][j], );
}
for (int j = ; j < rq[i].size(); j++){
int idx = rq[i][j].second;
int tmp = rq[i][j].first;
ans[idx] += FenwickTree::query(rq[i][j].first, i);
}
}
for (int i = ; i < m; i++){
printf("%d\n", n - ans[i]);
}
}
return ;
}

Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理的更多相关文章

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

    标题效果: 给很多分布 x 行轴. 然后给出了一个非常的多点集,问该组点分布多少不同段. IDEAS: 分散成多个线段点集的. 给出的线段的话,也就是说这个点集上不会有点在这条线段上. 所以我们就是求 ...

  2. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  3. Codeforces Round #216 (Div. 2) D. Valera and Fools

    题目链接:http://codeforces.com/contest/369/problem/D 注意题意:所有fools都向编号最小的fool开枪:但每个fool都不会笨到想自己开枪,所以编号最小的 ...

  4. Codeforces Round #216 (Div. 2) B. Valera and Contest

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

  5. Codeforces Round #216 (Div. 2)A. Valera and Plates

    #include <iostream> using namespace std; int main(){ int n, m , k; cin >> n >> m & ...

  6. Codeforces 369E Valera and Queries --树状数组+离线操作

    题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...

  7. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  8. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

  9. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组

    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...

随机推荐

  1. CSS Clip属性

    Clip属性在W3C官网是这样进行描述的:“通过对元素进行剪切来控制元素的可显示区域,默认情况下,元素是不进行任何剪切的,但是也有可能剪切区域也显示的设置了clip属性”. .selector { c ...

  2. 修改docker默认172.17网段

    docker启动时默认使用172.17.x.x作为容器的ip地址,可以通过以下方法自定义该网段: sudo service docker stop通过命令route -n查看docker0是否存在,若 ...

  3. linux设置中文环境

    确认当前环境 [root@Oracle11g ~]# df –h Filesystem            Size  Used Avail Use% Mounted on /dev/sda1    ...

  4. JSP标准标签库的安装以及自定义标签的创建

    JSTL 库安装 Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载 ...

  5. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  6. jQuery-弹窗登录

    在jQuery中实现弹窗常要用到的方法有: width()  :元素的宽度 outerWidth()  元素的宽度 盒子的padding+border 总的宽度 scrollTop()  鼠标滚轮自上 ...

  7. 谨慎使用php的strtotime()函数

    我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会涉及到时间问题.例如,我们想查询某个用户距离当前时间1个月的订单情况,在这个时候,我们有些会用到strtotime( ...

  8. CMD下查询Mysql中文乱码的解决方法

    我的MySQL是默认utf8编码的,所建数据库也是设置utf8编码,使用程序可以新增中文数据,在cmd中使用SQL语句新增数据则报类似Incorrect string value: '\xB2\xE2 ...

  9. TC2.0中怎样调用汇编程序

    转载于: TC2.0中怎样调用汇编程序 一.概述 TC是美国BORLAND 公司在IBM PC机上开发的一个高效.优化的C编译程序,它自带高效的全屏幕编辑程序,在集成开发环境下可支持编辑.编译.连接调 ...

  10. bzoj1233: [Usaco2009Open]干草堆tower

    Description 奶牛们讨厌黑暗. 为了调整牛棚顶的电灯的亮度,Bessie必须建一座干草堆使得她能够爬上去够到灯泡 .一共有N大包的干草(1<=N<=100000)(从1到N编号) ...