题意: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. 修改docker默认172.17网段

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

  2. 段落排版--中文字间距、字母间距(letter-spacing, word-spacing)

    中文字间隔.字母间隔设置: 如果想在网页排版中设置文字间隔或者字母间隔就可以使用    letter-spacing 来实现,如下面代码: h1{ letter-spacing:50px; } ... ...

  3. js正则验证方法大全

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. SGU 275 To xor or not to xor(高斯消元)

    题意: 从n个数中选若干个数,使它们的异或和最大.n<=100 Solution 经典的异或高斯消元. //O(60*n) #include <iostream> using nam ...

  5. 我用Emacs,后来转向Vim——Vim学习之Vim键盘图(绝对值得珍藏)

    Emacs本来就比较臃肿,麻烦.当我发现Vim键盘图时,我就渐渐转向Vim,追随Unix/Linux哲学去了.. 我用了Emacs三个月,因为它的学习曲线没Vim陡,这点吸引了,我使用Linux才7. ...

  6. php数组(array)输出三种形式

    $bbbb=array("11"=>"aaa","22"=>"bbb"); //只能输出值value不能输出 ...

  7. wordpress整站搬家总结

    去年图便宜,也没准备认真写博文,所以花了几百元钱买了个国内空间(域名已经备案).购买了以后,放了一个wordpress博客,没事的时候写写博文,但从没有抽出时间去写,文章的质量也不追求.一开始还可以, ...

  8. JQUERY1.9学习笔记 之可见性过滤器(二) 可见选择器

    描述:选择所有可见的元素. 例:点击时让所有的可见的div元素变黄. <!doctype html><html lang="en"> <head> ...

  9. 总结几种C#窗体间通讯的处理方法

    摘要:本文介绍了C#窗体间通讯的几种处理方法,即传值.继承.事件回调,希望对大家有用. http://www.cnblogs.com/jara/p/3439603.html 应用程序开发中,经常需要多 ...

  10. STM32学习笔记——DMA控制器(向原子哥学习)

    一.DMA简介 DMA,全称为:Direct Memory Access,即直接存储器访问,DMA 用来提供在外设和存储器之间或者存储器和存储器之间的高速数据传输.当 CPU 初始化这个传输动作,传输 ...