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. 理解Spark运行模式(一)(Yarn Client)

    Spark运行模式有Local,STANDALONE,YARN,MESOS,KUBERNETES这5种,其中最为常见的是YARN运行模式,它又可分为Client模式和Cluster模式.这里以Spar ...

  2. hdu 2516 取石子游戏 (Fibonacci博弈)

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. Redis单节点数据同步到Redis集群

    一:Redis集群环境准备 1:需要先安装好Redis集群环境并配置好集群 192.168.0.113 7001-7003 192.168.0.162 7004-7006 2:检查redis集群 [r ...

  4. 【SSM】自定义属性配置的使用

    首先,建立xxx.properties 文件在resource文件夹中,此处我们自定义的配置文件是oj-config.properties 然后,在applicationContext.xml中注册这 ...

  5. python:爬虫2——隐藏自己

    一.添加浏览器 方法一: head['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, li ...

  6. RHEL7.2 SSH无密码登录非root用户

    1 修改三台虚拟机的/ect/hosts文件 [hadoop@hadoop01 ~]$ cat /etc/hosts 127.0.0.1 localhost localhost.localdomain ...

  7. 【论文阅读】Clustering Convolutional Kernels to Compress Deep Neural Networks

    文章:Clustering Convolutional Kernels to Compress Deep Neural Networks 链接:http://openaccess.thecvf.com ...

  8. 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa

    docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...

  9. C#变量---xdd

    cshape(c#)学习笔记 1. string str1=Console.ReadLine();//键盘输入的默认为字符串 2.  Console.WriteLine('你的成绩是'+a+'分'); ...

  10. ssm通用 POM

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...