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的个数就好了,这里因为是按右端点从小到大遍历的,所以不用考虑用端点了。
发现, 大多数的离线处理都是先把其中的一维从小到大排序, 然后处理另一维。 这样相当于降维。
#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 树状数组 离线处理的更多相关文章
- Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)
标题效果: 给很多分布 x 行轴. 然后给出了一个非常的多点集,问该组点分布多少不同段. IDEAS: 分散成多个线段点集的. 给出的线段的话,也就是说这个点集上不会有点在这条线段上. 所以我们就是求 ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Codeforces Round #216 (Div. 2) D. Valera and Fools
题目链接:http://codeforces.com/contest/369/problem/D 注意题意:所有fools都向编号最小的fool开枪:但每个fool都不会笨到想自己开枪,所以编号最小的 ...
- Codeforces Round #216 (Div. 2) B. Valera and Contest
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
- Codeforces Round #216 (Div. 2)A. Valera and Plates
#include <iostream> using namespace std; int main(){ int n, m , k; cin >> n >> m & ...
- Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...
- 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 ...
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- 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 ...
随机推荐
- android 蓝牙4.0 开发介绍
最近一直在研究一个蓝牙功能 由于本人是菜鸟 学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说 真是硬伤 , 一些没必要的描 ...
- C#调用百度云存储接口上传文件
因前几日见园子里有人说可以把网站静态文件放在百度上,于是去百度开放平台看了看,发现之前那篇文章不是调的云存储接口啊... 于是自己写了个C#能调百度云存储的例子(百度云开放平台只提供php.java. ...
- Difference Between XML and XAML.
XML, or Extensible Markup Language, is a subset of the more complex SGML (Standard Generalized Mark ...
- iOS中webView加载URL需要处理特殊字符
今天在项目中遇到webView加载URL时,因为URL中有特殊字符,导致页面无法加载,而且在- (BOOL)webView:(UIWebView )webView shouldStartLoadWit ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
- filter过滤器执行顺序
浏览器请求---->进入过滤器---->进入doFilter方法--->执行chain.doFilter()方法就会放行----->进入业务逻辑方法------>进入过滤 ...
- 如果数据为null,则转成数据库可识别的DBNULL.Value
// <summary> /// 如果数据为null,则转成数据库可识别的DBNULL.Value /// </summary> /// <param name=&quo ...
- Linux redhat
挂载U盘 fdisk -l 可以列出所有的分区,包括没有挂上的分区和usb设备.我一般用这个来查找需要挂载的分区的位置,比如挂上u盘. mount /dev/sdb1 usb/
- 页面插入Flash方式
法一 <!-- 播放Flash动画代码 --> <div class="logoFlash"> <object classid="clsid ...
- php定时删除文件夹下文件(清理缓存文件)
<?php ignore_user_abort(); //客户端断开时,可以让脚本继续在后台执行 set_time_limit(0); //忽略php.ini设置的脚本运行时间限制 $inter ...