题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点

解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询的任意一个点的。这时候我们可以建立点集的补集,以线段的形式,如果点集的补集线段包含了某条给出的线段,那么被包含的那条线段肯定不会包括任意一个点,那么该组查询的答案ans--即可。 用树状数组做,离线读入数据,按容易被包含的线段优先排个序,然后扫一遍,边统计边修改即可。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <algorithm>
  7. using namespace std;
  8. #define N 1000007
  9.  
  10. int c[N],n,m,ans[],maxi;
  11. struct node
  12. {
  13. int l,r,ind;
  14. }a[N];
  15.  
  16. int lowbit(int x){ return x&-x; }
  17.  
  18. void modify(int x)
  19. {
  20. while(x <= maxi)
  21. {
  22. c[x]++;
  23. x += lowbit(x);
  24. }
  25. }
  26.  
  27. int getsum(int x)
  28. {
  29. int ans = ;
  30. while(x > )
  31. {
  32. ans += c[x];
  33. x -= lowbit(x);
  34. }
  35. return ans;
  36. }
  37.  
  38. int cmp(node ka,node kb) //容易被覆盖的线段放在前面
  39. {
  40. if(ka.l == kb.l)
  41. {
  42. if(ka.r == kb.r)
  43. return ka.ind < kb.ind;
  44. return ka.r < kb.r;
  45. }
  46. return ka.l > kb.l;
  47. }
  48.  
  49. int main()
  50. {
  51. int i,j,k,x,pre,cnt;
  52. maxi = N-;
  53. while(scanf("%d%d",&n,&m)!=EOF)
  54. {
  55. for(i=;i<=n;i++)
  56. scanf("%d%d",&a[i].l,&a[i].r),a[i].ind = ;
  57. memset(ans,,sizeof(ans));
  58. memset(c,,sizeof(c));
  59. int tot = n;
  60. for(i=;i<=m;i++)
  61. {
  62. scanf("%d",&cnt);
  63. scanf("%d",&x);
  64. if(x > )
  65. a[++tot].l = , a[tot].r = x-, a[tot].ind = i;
  66. pre = x;
  67. for(j=;j<cnt;j++)
  68. {
  69. scanf("%d",&x);
  70. if(x- >= pre+)
  71. a[++tot].l = pre+, a[tot].r = x-, a[tot].ind = i;
  72. pre = x;
  73. }
  74. a[++tot].l = pre+, a[tot].r = maxi, a[tot].ind = i;
  75. }
  76. sort(a+,a+tot+,cmp);
  77. for(i=;i<=tot;i++)
  78. {
  79. if(a[i].ind > )
  80. ans[a[i].ind] += getsum(a[i].r);
  81. else
  82. modify(a[i].r);
  83. }
  84. for(i=;i<=m;i++)
  85. printf("%d\n",n-ans[i]);
  86. }
  87. return ;
  88. }

Codeforces 369E Valera and Queries --树状数组+离线操作的更多相关文章

  1. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  2. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  3. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  4. CodeForces 828E DNA Evolution(树状数组)题解

    题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...

  5. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  6. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  7. Codeforces 635D Factory Repairs【树状数组】

    又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...

  8. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  9. codeforces E. DNA Evolution(树状数组)

    题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...

随机推荐

  1. javascript 之注意url特殊字符限制

    引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器url参数操作的时候,经常会用到encodeURIComponent()和decodeURIComponent()以及encode ...

  2. linux shell 编程

    1,获取命令执行的结果,字符串拼接(脚本最常使用的功能)   cmd_result=$(date +%Y%b%d)        //使用变量获取命令执行的结果 或者 cmd_result=`date ...

  3. 如何rename sqlserver database

    Problem Sometimes there is a need to change the name of your database whether this is because the or ...

  4. HTML中行内元素的竖直方向的padding和margin是否真的无效

    参考资料:Inline elements and padding 今天写一个导航栏时遇到了一个问题:行内元素的padding-top,padding-bottom和margin-top,margin- ...

  5. 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 ...

  6. 迭代器 iterator(二): 用iterator遍历arraylist

           迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些 ...

  7. iOS 工厂方法模式

    iOS工厂方法模式 什么是工厂方法模式? 工厂方法模式和简单工厂模式十分类似,大致结构是基本类似的.不同在于工厂方法模式对工厂类进行了进一步的抽象,将之前的一个工厂类抽象成了抽象工厂和工厂子类,抽象工 ...

  8. 深入理解Android的startservice和bindservice

    一.首先,让我们确认下什么是service?         service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比 ...

  9. animation of android (4)

    TimeAnimator: 与objectAminator不同,它反馈的时间间隔.也就是说TimeAnimator不产生实际的动画效果,他反馈的时间间隔和时间值. 而你并不关心 interpolate ...

  10. Android开源项目汇总【转】

    主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView ...