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. LeetCode 5264 在受污染的二叉树中查找元素 Find Elements in a Contaminated Binary Tree

    地址 https://leetcode-cn.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binar ...

  2. Android Debug 之 Log 最佳实践

    本文微信公众号「AndroidTraveler」首发. 背景 在开发过程中,调试是必不可少的一项工作. 当我们要确定项目的逻辑时,当我们要了解界面的生命周期时,当我们发现新写的逻辑与期望效果不一致时, ...

  3. Linux(ubuntu)下切换root用户

    输入命令su root切换用户,会提示输入root密码,如果不记得或者是没设置过,那么可以输入sudo passwd root来设置密码,会让你输入两次密码确认.输入完即可使用su root命令切换r ...

  4. pat 1124 Raffle for Weibo Followers(20 分)

    1124 Raffle for Weibo Followers(20 分) John got a full mark on PAT. He was so happy that he decided t ...

  5. C#控制打印机通过不同纸盒/进纸口进纸打印

    通常我们是通过程序操作打印机打印我们设置好的内容,但基本都是打印机默认进纸口打印:最近有一个通过C#程序控制两个进纸口分别进一张纸进行打印的需求,通过偿失找到了解决方案如下: 关于C#调用打印机打印的 ...

  6. &#128293;《手把手》系列基础篇之2-python+ selenium-打开和关闭浏览器(详细)

    1. 简介 本节介绍如何初始化一个webdriver实例对象driver,然后打开和关闭firefox浏览器.要用selenium打开fiefox浏览器.首先需要去下载一个driver插件geckod ...

  7. PIC18F45K80串口1和串口2异步收发通信实例

    PIC18F45K80串口1和串口2异步收发通信实例 一:配置串口1初始化函数 首先打开技术手册,查看异步串口的操作流程以及配置. 需要将串口对应引脚的方向寄存器设置为输入

  8. vue学习笔记(八)组件校验&通信

    前言 在上一章博客的内容中vue学习笔记(七)组件我们初步的认识了组件,并学会了如何定义局部组件和全局组件,上一篇内容仅仅只是对组件一个简单的入门,并没有深入的了解组件当中的其它机制,本篇博客将会带大 ...

  9. Unity 工作经历+近期面试经历(二)

    注册博客园后,我原本打算每一份工作经历都记录下来.但是,这份工作已经换了半年了,好几次想要写,又不知道该怎么写.太多的负能量.我始终相信,情绪是会传染的.我基本决定放弃写这篇文章了.就让时间去淡化经历 ...

  10. 使用python脚本执行地理处理工具

    桌面ArcGIS包含800多种可在Python脚本中运行的地理处理工具. 通过Python脚本来运行地理处理工具,可以处理复杂的工作和执行批处理任务. 案例一:使用脚本执行地理处理工具(以裁剪为例) ...