Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给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 --树状数组+离线操作的更多相关文章
- Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理
题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...
- CodeForces - 369E Valera and Queries(树状数组)
CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- CodeForces 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- Codeforces 635D Factory Repairs【树状数组】
又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- codeforces E. DNA Evolution(树状数组)
题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...
随机推荐
- java猜数字游戏
import java.util.Scanner; //导入包 class GuessNum { public static void main(String[] args) { int num = ...
- mybatis中#和$符号的区别
mybatis做为一个轻量级ORM框架在许多项目中使用,因其简单的入门受到了广大开发者的热爱.在近期项目中再做一个相关的开发,碰到了#.$符号这样的问题,之前没怎么注意过,通过学习之后,有了点感悟,分 ...
- JSON的三种解析方式
一.什么是JSON? JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度. JSON就是一串字符串 只不过元素会使用特定 ...
- (八)play之yabe项目【身份验证】
(八)play之yabe项目[身份验证] 博客分类: 框架@play framework 添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...
- 微信公共平台开发2 .net
成功的走出第一步后,我们紧接着趁热打铁开始下一步: 成为了开发者之后微信平台会给您AppId和AppSecret,在订阅号中是没有的,所以因该申请一下服务号, 若没有请注意上一篇http://www. ...
- This application is currently offline. To enable the application, remove the app_offline.htm file from the application root directory.
IIS提示:This application is currently offline. To enable the application, remove the app_offline.htm f ...
- SharePoint 2013 点击"关注" 报错
现象: 点击"关注" 报错. 解决办法: 1.确保bin文件夹下的.dll版本与web.config一致. 2.设置user porfile权限. 2.重启iis 结果如下:
- How does Web Analytics works under sharePoint 2010
[http://gokanx.wordpress.com/2013/06/15/how-does-web-analytics-works-under-sharepoint-2010/] You nee ...
- 桥牌笔记:Show up Squeeze显露挤牌法
南主打4S,注意一个叫牌过程,西家叫过加倍,东家应叫过2D. 西连打红桃K.A,然后再打红桃J让东家将吃.东家上手后,回小方块.此时庄家已经失了3墩了,如何完成这个4S? 庄家必须拿到所有剩下的牌墩. ...
- Java或Android开发中,去掉块注释格式化后每行出现的星号(*)的解决方案。(Eclipse)
找到子项,在 Window->Prefrences->Java->Code Style->Formatter,点击New新建 Active profile,然后在Comment ...