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’ ...
随机推荐
- javascript 之注意url特殊字符限制
引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器url参数操作的时候,经常会用到encodeURIComponent()和decodeURIComponent()以及encode ...
- linux shell 编程
1,获取命令执行的结果,字符串拼接(脚本最常使用的功能) cmd_result=$(date +%Y%b%d) //使用变量获取命令执行的结果 或者 cmd_result=`date ...
- 如何rename sqlserver database
Problem Sometimes there is a need to change the name of your database whether this is because the or ...
- HTML中行内元素的竖直方向的padding和margin是否真的无效
参考资料:Inline elements and padding 今天写一个导航栏时遇到了一个问题:行内元素的padding-top,padding-bottom和margin-top,margin- ...
- An unexpected error has occurred" error appears when you try to create a SharePoint Enterprise Search Center on a Site Collection
The Enterprise Search Center requires that the Publishing feature be enabled. To enable the Publishi ...
- 迭代器 iterator(二): 用iterator遍历arraylist
迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些 ...
- iOS 工厂方法模式
iOS工厂方法模式 什么是工厂方法模式? 工厂方法模式和简单工厂模式十分类似,大致结构是基本类似的.不同在于工厂方法模式对工厂类进行了进一步的抽象,将之前的一个工厂类抽象成了抽象工厂和工厂子类,抽象工 ...
- 深入理解Android的startservice和bindservice
一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比 ...
- animation of android (4)
TimeAnimator: 与objectAminator不同,它反馈的时间间隔.也就是说TimeAnimator不产生实际的动画效果,他反馈的时间间隔和时间值. 而你并不关心 interpolate ...
- Android开源项目汇总【转】
主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView ...