Group

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

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
 题意:t组数据 给你一个长度为n的序列 m个询问[l,r] 问 l到r 的值可以组成多少个连续的段 
 题解:例如序列3 1 2 5 4  
查询 [1,5]  值为{1,2,3,4,5}  只有一个连续的段
查询 [2,4] 值为{1,2} {5} 有两个连续的段
莫队处理;
 
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#define ll __int64
using namespace std;
int n,m;
struct node
{
int l,r,id;
} N[];
int p[];
int block;
int a[];
int x[];
int mp[];
ll ans=;
ll re[];
int cmp(struct node aa,struct node bb)
{
if(p[aa.l]==p[bb.l])
return aa.r<bb.r;
else
return p[aa.l]<p[bb.l];
}
void update(int w,int h)
{
if(h==)
{
mp[a[w]]=;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans++;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans--;
}
else
{
mp[a[w]]=;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans++;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans--;
}
}
int t;
int main()
{
scanf("%d",&t);
for(int o=;o<=t;o++)
{
for(int i=;i<;i++)
mp[i]=;
scanf("%d %d",&n,&m);
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
for(int i=; i<=m; i++)
{
scanf("%d %d",&N[i].l,&N[i].r);
N[i].id=i;
}
block=(int)sqrt((double)n);
for(int i=; i<=n; i++)
p[i]=(i-)/block+;
sort(N+,N++m,cmp);
ans=;
for(int i=,l=,r=; i<=m; i++)
{
for(; r<N[i].r; r++) update(r+,);
for(; l>N[i].l; l--) update(l-,);
for(; r>N[i].r; r--) update(r,-);
for(; l<N[i].l; l++) update(l,-);
re[N[i].id]=ans;
}
for(int i=; i<=m; i++)
printf("%I64d\n",re[i]);
}
return ;
}

HDU 4638 莫队算法的更多相关文章

  1. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  2. hdu 5145(莫队算法+逆元)

    NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. HDU 4638 (莫队)

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

  4. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  5. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  6. HDU 3333 Turing Tree 莫队算法

    题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...

  7. HDU 5381 The sum of gcd (技巧,莫队算法)

    题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...

  8. HDU 4358 Boring counting dfs序+莫队算法

    题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...

  9. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

随机推荐

  1. Hyperledger Fabric CouchDB as the State Database——使用CouchDB

    使用CouchDB作为状态数据库 状态数据库选项 状态数据库包括LevelDB和CouchDB.LevelDB是嵌入在peer进程中的默认键/值状态数据库,CouchDB是一个可选的外部状态数据库.与 ...

  2. Python Requests库入门——应用实例-百度、360搜索关键词提交

    百度的关键词接口: http://www.baidu.com/s?wd=keyword 360的关键词接口: http://www.so.com/s?q=keyword keyword就是需要查找的关 ...

  3. Scrum立会报告+燃尽图(十一月二十五日总第三十三次):展示博客

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2413 项目地址:https://git.coding.net/zhang ...

  4. Java的继承,final关键字,super关键字

    1.继承的初始化顺序: 父类—>父类的初始化对象中的属性—>父类的构造方法—>子类—>子类的初始化对象中的属性—>子类的构造方法 若有构造方法:则先执行属性,再执行构造方 ...

  5. Python:元组操作总结

    Python的元组和列表类似,不同之处在于元组中的元素不能修改(因此元组又称为只读列表),且元组使用小括号而列表使用中括号,如下: tup1=('physics','chemistry',1997,2 ...

  6. FPGA论文

    基于 NetFPGA 的 VCP 网络的设计与实现 --可变结构拥塞控制协议(VCP),适应于高带宽时延乘积网络的显式拥塞控制协议 无源光网络(PON) 1.区块链技术发展,物联网设备激增,服务器压力 ...

  7. BETA阶段冲刺集合

    冲刺开始: https://www.cnblogs.com/LZTZ/p/9097296.html 第一天: https://www.cnblogs.com/LZTZ/p/9097303.html 第 ...

  8. 陈爽 软件工程导论week2.1

    软件工程导论week2.1 第一章概论问题:1.程序=算法+数据结构  软件=程序+软件工程软件工程的目标是创造足够好的软件,可以从用户满意度,可靠性,软件流程的质量,可维护性等方面判断,但是我们没有 ...

  9. 03_Java基础语法_第3天(Scanner、Random、流程控制语句)_讲义

    今日内容介绍 1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scan ...

  10. 2nd 词频统计效能测试

    词频统计效能测试 使用性能分析工具分析结果如下 :