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中dip,dp,sp,pt和px的区别
dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...
- openURL的使用方法
openURL的使用方法 openURL的使用方法: view plaincopy to clipboardprint? [[UIApplication sharedApplication] open ...
- Android 学习手札(一) 应用程序架构
1.资源列表 Android支持的资源列表 目 录 资源类型 描述 res/anim XML 该目录用于存放帧(frame).动画或补间(tweened)动画文件 res/drawable ...
- TestNG扩展
1. TestNG API 本章节将讨论如何使用TestNG API来创建自己的TestNG对象.TestNG的API基本由接口组成,这样做是为了容易模拟TestNG返回的对象. 1.1 org.te ...
- 百度地图实现车辆轨迹移动播放(baidu map api)
开发技术:jquery,js baidu map api,json,ajax QQ1310651206
- Oracle数据库之PL/SQL触发器
Oracle数据库之PL/SQL触发器 1. 介绍 触发器(trigger)是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是 ...
- LI 导航
HTML: <div class="my_nav"> <ul> <li class="tiao cur" id="ord ...
- Redis 中的事务
Redis支持简单的事务 Redis与mysql事务的对比 Mysql Redis 开启 start transaction muitl 语句 普通sql 普通命令 失败 rollback 回滚 di ...
- 用Web Picasa API搭建站内相册
在flickr时代,为了专门把站内嵌入相册,还专门写了一篇文章把Flickr相册搬回家.flickr被墙之后,我就把个人相册转到了Web Picasa上.用Picasa Web就简单多了,官方提供了S ...
- angular post发送请求和GET发送请求,服务器端接收不到信息的问题
参数可能因为编码原因,服务器端无法接收到传递的值, 这时需要用到补丁来解决这个问题 1,下载一个http.patch.js文件,放入YII框架中的js/ng文件架内 2angularjs 创建模型部分 ...