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.

InputFirst 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]. 
OutputFor every query output a number indicate there should be how many group so that the sum of value is max.Sample Input

  1. 1
  2. 5 2
  3. 3 1 2 5 4
  4. 1 5
  5. 2 4

Sample Output

  1. 1
  2. 2

题意:给你N个数,然后给出M个查询;对于每一个查询,该区间内最少可以有多少个连续的序列;

题解:莫队(也可以用树,单位现在还不会,以后补上~~);

AC代码为:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const int M = + ;
  6.  
  7. int a[M], vis[M], ans, ret[M];
  8. struct node
  9. {
  10. int id, l, r, pos;
  11. }v[M];
  12.  
  13. int cmp(node x, node y)
  14. {
  15. if (x.pos<y.pos)
  16. return ;
  17. else if (x.pos == y.pos&&x.r<y.r)
  18. return ;
  19. return ;
  20. }
  21.  
  22. inline void insert(int x)
  23. {
  24. x = a[x];
  25. vis[x] = ;
  26. if (vis[x - ] && vis[x + ])
  27. {
  28. ans--;
  29. }
  30. else if (!vis[x - ] && !vis[x + ])
  31. {
  32. ans++;
  33. }
  34. }
  35.  
  36. inline void erase(int x)
  37. {
  38. x = a[x];
  39. vis[x] = ;
  40. if (vis[x - ] && vis[x + ])
  41. {
  42. ans++;
  43. }
  44. else if (!vis[x - ] && !vis[x + ])
  45. {
  46. ans--;
  47. }
  48. }
  49.  
  50. int main()
  51. {
  52. int t, n, m, i, j, k;
  53. int l, r;
  54. scanf("%d", &t);
  55. while (t--)
  56. {
  57. scanf("%d%d", &n, &m);
  58. for (i = ; i <= n; i++)
  59. scanf("%d", &a[i]);
  60. int x = sqrt(n);
  61. for (i = ; i <= m; i++)
  62. {
  63. scanf("%d%d", &v[i].l, &v[i].r);
  64. v[i].id = i;
  65. v[i].pos = v[i].l / x;
  66. }
  67. sort(v + , v + m + , cmp);
  68. memset(vis, , sizeof(vis));
  69. ans = ;
  70. insert();
  71. int p = , q = ;
  72. for (i = ; i <= m; i++)
  73. {
  74. l = v[i].l;
  75. r = v[i].r;
  76. while (q<r) insert(++q);
  77. while (q>r) erase(q--);
  78. while (p<l) erase(p++);
  79. while (p>l) insert(--p);
  80. ret[v[i].id] = ans;
  81. }
  82. for (i = ; i <= m; i++)
  83. printf("%d\n", ret[i]);
  84. }
  85. return ;

hdu-4638的更多相关文章

  1. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. hdu 4638 Group

    http://acm.hdu.edu.cn/showproblem.php?pid=4638 问题其实就是求[L,R]中有多少个连续的段 若每一个人都是一个段 那么[L,R]中每一个朋友关系就会减少一 ...

  3. HDU 4638 Group(分组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:给出一个数列,若干询问.每次询问区间[L,R]的最少有多少段?每一段是连续的一段且这段内的 ...

  4. HDU 4638 树状数组 想法题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 解题思路: 题意为询问一段区间里的数能组成多少段连续的数.先考虑从左往右一个数一个数添加,考虑当 ...

  5. HDU 4638 (莫队)

    题目链接:Problem - 4638 做了两天莫队和分块,留个模板吧. 当插入r的时候,设arr[r]代表r的位置的数字,判断vis[arr[r-1]]和vis[arr[r+1]]是否访问过,如果两 ...

  6. HDU 4638 Group 树状数组 + 思路

    实际上就是问这个区间编号连续的段的个数,假如一个编号连续的段有(a+b)个人,我把他们分在同一组能得到的分值为(a+b)^2,而把他们分成人数为a和b的两组的话,得到的分值就是a^2+b^2,显然(a ...

  7. HDU 4638 Group ★(树状数组)

    题意 询问一段区间里的数能组成多少段连续的数. 思路 先考虑从左往右一个数一个数添加,考虑当前添加了i - 1个数的答案是x,那么可以看出添加完i个数后的答案是根据a[i]-1和a[i]+1是否已经添 ...

  8. hdu 4638 树状数组

    思路:将查询区间按右节点的升序排列,然后插入第i个数的时候,若nun[i]+1已经插入,那么就update(pre[num[i]+1],-1):pre[]表示的是该数的位置.同样若 num[i]-1存 ...

  9. HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】

    根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...

  10. hdu 4638 Group 莫队算法

    题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...

随机推荐

  1. CentOS7 reset脚本,用于初始化新的虚拟机

    能用,有待完善 CentOS7测试 哈哈 #!/bin/bash #************************************************************** #Au ...

  2. Typescript I: 遍历Array的方法:for, forEach, every等

    Typescript的官方文档 Iterators and Geneators (https://www.typescriptlang.org/docs/handbook/iterators-and- ...

  3. ASP.NET Core 3 使用原生 依赖注入 集成 AspectCore ,实现 AOP 功能

    在NETCORE中可以使用AOP的方式有很多很多,包括国内优秀的开源框架asp.netcore同样可以实现AOP编程模式.   IOC方面,个人喜欢net core 3自带的DI,因为他注册服务简洁优 ...

  4. nyoj 64-鸡兔同笼 (解二元一次方程)

    64-鸡兔同笼 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:26 submit:58 题目描述: 已知鸡和兔的总数量为n,总腿数为m.输入n和m, ...

  5. PostGIS mysql_fdw安装(Linux)

    ##本人在安装过程中,可能因为系统环境因素或是其他原因,参考网上的文章没有一篇是非常顺利的,所以自己总结一下. ##安装过程中非常坎坷,有些地方反复了好几次,弄的有点模糊,但模糊的地方在文中我会指出. ...

  6. 容器镜像服务联手 IDE 插件,实现一键部署、持续集成与交付

    容器技术提供了一种标准化的交付方式,将应用的代码以及代码环境依赖都打包在一起,成为一个与环境无关的交付物,可以被用在软件生命周期的任何阶段,彻底改变了传统的软件交付方式. 甚至可以说,是在容器技术之后 ...

  7. springboot+logback日志输出企业实践(下)

    目录 1.引言 2. 输出 logback 状态数据 3. logback 异步输出日志 3.1 异步输出配置 3.2 异步输出原理 4. springboot 多环境下 logback 配置 5. ...

  8. C#Windows Forms窗体、按钮-xdd

    1.更换窗体图标 方法:单击窗体,更改icon属性. 2.调整窗体打开时默认位置 方法:单击窗体,更改StartPotion属性. 3.修改窗体大小 方法:单击窗体,更改Size属性. 4.设置窗体的 ...

  9. DBEntry.Net 简介

    [尊重作者:本文转自:http://www.cnblogs.com/lephone/]   这是我设计的一个轻量级的 .Net ORM (Object Relational Mapping) 数据访问 ...

  10. [ASP.NET Core 3框架揭秘] 配置[1]:读取配置数据[上篇]

    提到"配置"二字,我想绝大部分.NET开发人员脑海中会立即浮现出两个特殊文件的身影,那就是我们再熟悉不过的app.config和web.config,多年以来我们已经习惯了将结构化 ...