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
5 2
3 1 2 5 4
1 5
2 4

Sample Output

1
2

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

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

AC代码为:

 #include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int M = + ; int a[M], vis[M], ans, ret[M];
struct node
{
int id, l, r, pos;
}v[M]; int cmp(node x, node y)
{
if (x.pos<y.pos)
return ;
else if (x.pos == y.pos&&x.r<y.r)
return ;
return ;
} inline void insert(int x)
{
x = a[x];
vis[x] = ;
if (vis[x - ] && vis[x + ])
{
ans--;
}
else if (!vis[x - ] && !vis[x + ])
{
ans++;
}
} inline void erase(int x)
{
x = a[x];
vis[x] = ;
if (vis[x - ] && vis[x + ])
{
ans++;
}
else if (!vis[x - ] && !vis[x + ])
{
ans--;
}
} int main()
{
int t, n, m, i, j, k;
int l, r;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (i = ; i <= n; i++)
scanf("%d", &a[i]);
int x = sqrt(n);
for (i = ; i <= m; i++)
{
scanf("%d%d", &v[i].l, &v[i].r);
v[i].id = i;
v[i].pos = v[i].l / x;
}
sort(v + , v + m + , cmp);
memset(vis, , sizeof(vis));
ans = ;
insert();
int p = , q = ;
for (i = ; i <= m; i++)
{
l = v[i].l;
r = v[i].r;
while (q<r) insert(++q);
while (q>r) erase(q--);
while (p<l) erase(p++);
while (p>l) insert(--p);
ret[v[i].id] = ans;
}
for (i = ; i <= m; i++)
printf("%d\n", ret[i]);
}
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. java-optional-快速使用-教程

    前言: 在公司中开发项目时碰到一个从Java8引入的一个Optional类,以前jdk版本使用的比较低,没有使用过,于是我在网上浏览了一些文档写篇文章学习总结一下,希望没有用过的朋友们都能够快速学习到 ...

  2. 微服务SpringCloud之GateWay熔断、限流、重试

    纯洁的微笑的Spring Cloud系列博客终于学完了,也对Spring Cloud有了初步的了解. 修改请求路径的过滤器 StripPrefix Filter 是一个请求路径截取的功能,我们可以利用 ...

  3. 微信小程序引入全局或公共样式

    在开发的过程中,总会遇到很多可复用性的样式,为了代码更加的简洁和减少微不住道的文件体积,我抽取了一部分的公共样式,并全局引入,不知是否妥当,如有更好的想法,欢迎一起探讨 在app.wxss中引入 然后 ...

  4. 解决vuex的数据刷新(F5)后会被初始化的问题

    介绍一个vuex的数据刷新(F5)后会被初始化的问题处理的插件:vuex-localstorage 实现的原理大概就是监听浏览器的刷新,关闭事件,把vuex的值存储到本地localstorage,刷新 ...

  5. Linux下编写-makefile-详细教程(跟我一起写-Makefile-Markdown整理版)

    目录 概述 关于程序的编译和链接 Makefile 介绍 Makefile的规则 一个演示例子 make是怎样工作的 makefile中使用变量 让make自己主动推导 另类风格的makefile 清 ...

  6. 一个简单的C#爬虫程序

    这篇这篇文章主要是展示了一个C#语言如何抓取网站中的图片.实现原理就是基于http请求.C#给我们提供了HttpWebRequest和WebClient两个对象,方便发送请求获取数据,下面看如何实 1 ...

  7. [ERROR]Unable to locate package

    刚更换了DNS,需要更新下apt-get $ apt-get update

  8. C博客作业05--2019-指针

    0.展示PTA总分 1.本章学习总结 1.1 学习内容总结 1.2 本章学习体会 2.PTA实验作业 2.16 -7 输出月份英文名 2.1.1 伪代码 char* getmonth(int n) { ...

  9. EntityFramework Core 3.0查询

    前言 随着.NET Core 3.0的发布,EF Core 3.0也随之正式发布,关于这一块最近一段时间也没太多去关注,陆续会去对比之前版本有什么变化没有,本节我们来看下两个查询. 分组 我们知道在E ...

  10. EasyCode实现数据库到Swagger全自动化

    简介 EasyCode是基于IntelliJ IDEA开发的代码生成插件,通过自定义生成模板可以完成定制化的 Mapper Service Controller 生成,结合数据库 Comment还可以 ...