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. 深入了解MySQL存储索引

    (一)关于存储引擎 创建合适的索引是SQL性能调优中最重要的技术之一.在学习创建索引之前,要先了解MySql的架构细节,包括在硬盘上面如何组织的,索引和内存用法和操作方式,以及存储引擎的差异如何影响到 ...

  2. Python登录,输入三次密码

    第一段python代码,写了一天,总算不报错了,值得纪念. 基本要求: 写一个登录界面,登录三次锁定用户 1. 包含一个用户信息文件,用户名和密码 2.黑名单文件 过程: 1.先检查是否在黑名单中,如 ...

  3. USACO 1.2.3 Name That Number 命名那个数字(打开文件)

    Description 在威斯康辛州牛大农场经营者之中,都习惯于请会计部门用连续数字给母牛打上烙印.但是,母牛用手机时并没感到这个系统的便利,它们更喜欢用它们喜欢的名字来呼叫它们的同伴,而不是用像这个 ...

  4. Beta周王者荣耀交流协会第六次会议

    1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:袁玥 2. 时间跨度 2017年11月15日 19:00 — 19:10 ,总计10分钟. 3. 地点 一食堂 ...

  5. 03慕课网《vue.js2.5入门》——Vue-cli的安装,创建webpack模板项目

    安装Vue-cli 第一种 貌似不可以,然后用了第二种,但是重装系统后,第二种不能用了,用了第一种可以 # 全局安装vue -cli命令npm install --global vue-cli # 创 ...

  6. Optimized Flow Migration for NFV Elasticity Control

    NFV弹性控制中的流迁移优化 ABSTRACT 基于动态创建和移除网络功能实例,NFV在网络功能控制上有很大的弹性.比如,网络功能和并,网络功能拆分,负载均衡等等. 那么为了实现弹性控制,就需要网络流 ...

  7. Spring中jdbc Template使用

    http://1358440610-qq-com.iteye.com/blog/1826816

  8. django-registration中的问题

    https://github.com/ubernostrum/django-registration.git https://django-registration.readthedocs.io/en ...

  9. 【移动端debug-1】css3中box-shadow的溢出问题

    今天做项目遇到一个box-shadow的溢出父容器的问题,如下面的代码中,子容器inner的box-shadow在没有任何设置的情况下是溢出父容器的. 代码: <!DOCTYPE html> ...

  10. 第199天:js---扩充内置对象功能总结

    一.数组 1.删除数组中指定索引的数据 /** 删除数组中指定索引的数据 **/ Array.prototype.deleteAt = function (index) { if (index < ...