Group

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17    Accepted Submission(s): 5

Problem Description
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
 
Input
First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
 
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
 
Sample Input
1
5 2
3 1 2 5 4
1 5
2 4
 
Sample Output
1
2
 
Source
 
Recommend
zhuyuanchen520
 

把查询区间按照左端点排序。

然后逐渐从左边删除数,看对后面的影响。

树状数组实现单点更新和求和

  1. /*
  2. * Author:kuangbin
  3. * 1007.cpp
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <algorithm>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <map>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <string>
  15. #include <math.h>
  16. using namespace std;
  17. const int MAXN = ;
  18. int n;
  19.  
  20. int lowbit(int x)
  21. {
  22. return x&(-x);
  23. }
  24. int c[MAXN];
  25. void add(int i,int val)
  26. {
  27. while(i <= n)
  28. {
  29. c[i] += val;
  30. i += lowbit(i);
  31. }
  32. }
  33. int sum(int i)
  34. {
  35. int s = ;
  36. while(i > )
  37. {
  38. s += c[i];
  39. i -= lowbit(i);
  40. }
  41. return s;
  42. }
  43. int a[MAXN];
  44. int num[MAXN];
  45.  
  46. int ans[MAXN];
  47. struct Node
  48. {
  49. int l,r;
  50. int index;
  51. }node[MAXN];
  52. bool cmp(Node a,Node b)
  53. {
  54. return a.l < b.l;
  55. }
  56. int main()
  57. {
  58. //freopen("in.txt","r",stdin);
  59. //freopen("out.txt","w",stdout);
  60. int m;
  61. int T;
  62. scanf("%d",&T);
  63. while(T--)
  64. {
  65. scanf("%d%d",&n,&m);
  66. memset(c,,sizeof(c));
  67. for(int i = ;i <= n;i++)
  68. {
  69. scanf("%d",&a[i]);
  70. num[a[i]]=i;
  71. }
  72. num[] = n+;
  73. num[n+] = n+;
  74. for(int i = ;i <= n;i++)
  75. {
  76. if(i < num[a[i]-] && i < num[a[i]+])
  77. add(i,);
  78. else if(i > num[a[i]-] && i > num[a[i]+])
  79. add(i,-);
  80. }
  81. for(int i = ;i < m;i++)
  82. {
  83. scanf("%d%d",&node[i].l,&node[i].r);
  84. node[i].index = i;
  85. }
  86. sort(node,node+m,cmp);
  87. int i = ;
  88. int j = ;
  89. while(j < m)
  90. {
  91. while(i <= n && i < node[j].l)
  92. {
  93. if(i > num[a[i]-] && i > num[a[i]+])
  94. add(i,-);
  95. else if(i < num[a[i]-] && i < num[a[i]+])
  96. {
  97. int Min = min(num[a[i]-],num[a[i]+]);
  98. int Max = max(num[a[i]-],num[a[i]+]);
  99. add(i,-);
  100. add(Min,);
  101. add(Max,);
  102. }
  103. else if(i < num[a[i]-])
  104. {
  105. add(i,-);
  106. add(num[a[i]-],);
  107. }
  108. else
  109. {
  110. add(i,-);
  111. add(num[a[i]+],);
  112. }
  113. i++;
  114. }
  115. while( j < m && node[j].l <= i)
  116. {
  117. ans[node[j].index]= sum(node[j].r);
  118. j++;
  119. }
  120. }
  121. for(int i = ;i < m;i++)
  122. printf("%d\n",ans[i]);
  123. }
  124. return ;
  125. }

HDU 4638 Group (2013多校4 1007 离线处理+树状数组)的更多相关文章

  1. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  2. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  3. hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...

  4. HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...

  6. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

  7. HDU 4918 Query on the subtree(动态点分治+树状数组)

    题意 给定一棵 \(n\) 个节点的树,每个节点有点权.完成 \(q\) 个操作--操作分两种:修改点 \(x\) 的点权.查询与 \(x\) 距离小于等于 \(d\) 的权值总和. \(1 \leq ...

  8. HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)

    题意:给n个点,求每一个点的满足 x y z 都小于等于它的其他点的个数. 析:三维的,第一维直接排序就好按下标来,第二维按值来,第三维用数状数组维即可. 代码如下: cdq 分治: #pragma ...

  9. NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)

    对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...

随机推荐

  1. java===java基础学习(9)---方法参数

    方法参数注意三要点: 一个方法不能修改一个基本数据类型的参数(数值型或者布尔型). 一个方法可以改变一个对象参数的状态. 一个方法不能让对象参数引用一个新的对象. package testbotoo; ...

  2. 安全测试===sqlmap

    本文转自:https://www.secpulse.com/archives/4213.html   鉴于很多新手对sqlmap的用法不是很熟悉 很多常用sqlmap的也不一定完全会用sqlmap 特 ...

  3. python基础===open()文件处理使用介绍

    本文转自:Python open()文件处理使用介绍 1. open()语法open(file[, mode[, buffering[, encoding[, errors[, newline[, c ...

  4. 64_g5

    golang-github-kr-text-devel-0-0.11.git6807e77.f..> 11-Feb-2017 07:48 14250 golang-github-kr-text- ...

  5. 用selenium 模块控制浏览器

    11.8 用selenium 模块控制浏览器selenium 模块让Python 直接控制浏览器,实际点击链接,填写登录信息,几乎就像是有一个人类用户在与页面交互.与Requests 和Beautif ...

  6. visual studio 个性化设置

    尼马visual studio 的注释建设的真垃圾 Ctrl+K+C Ctrl+K+U, 通过工具->选项->环境->键盘->命令包含中搜索“注释选定内容”,分配成 Ctrl+ ...

  7. BZOJ 3771 生成函数,FFT

    Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看:“是啊是啊!” 水神把斧头扔在一 ...

  8. 安装sysstat出现软件包依赖问题

    需要使用Linux性能分析工具iostat 和sar等, 这就需要安装软件包sysstat 在Ubuntu 12.04下运行sudo apt-get install sysstat,出现如下问题: 开 ...

  9. C#面向对象(OOP)入门—第二天—多态和继承(继承)

    介绍: 第一天的内容主要是不同情形下的方法重载.这一部分则主要讲面向对象中继承的概念.首先用一个要点图形来定义继承. 继承 一个简单的例子: ClassA: class ClassA:ClassB { ...

  10. Single Number I&& II——还没看,倒过头来再看

    Single Number I Given an array of integers, every element appears twice except for one. Find that si ...