HDU 4638Group (莫队)
Group
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
题目大意:
多组数据
给出n和m,a[i]为1到n的不重复数
询问m次
问一段区间内,连续的一段可分为一组(可以打乱顺序),问至少分多少组,
莫队板子题。
这里纠正一下莫队细节
先加再删,不要先删再加
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define maxn 100100
using namespace std;
int n,m,zz,k;
int a[maxn];
int belong[maxn];
int vis[maxn];
int ans;
inline int read()
{
int x=0,f=1;char s=getchar();
while('0'>s||s>'9') {
if(s=='-') f=-1;
s=getchar();
}
while('0'<=s&&s<='9') {
x=x*10+s-'0';
s=getchar();
}
return x*f;
}
struct edge {
int x,y,id;
int ans;
bool operator < (const edge &a) const {
return belong[x]==belong[a.x] ? y<a.y : x<a.x;
}
} q[maxn];
inline int cmp(const edge &a,const edge &b) {
return a.id<b.id;
}
inline void Add(int x) {
int dsr=vis[x+1]+vis[x-1];
dsr==0 ? ++ans : dsr==2?--ans:ans;
++vis[x];
}
inline void Delet(int x) {
int dsr=vis[x+1]+vis[x-1];
dsr==0? --ans : dsr==2?++ans:ans;
--vis[x];
}
int main() {
zz=read();
while(zz--) {
memset(a,0,sizeof(a));
memset(belong,0,sizeof(belong));
memset(q,0,sizeof(q));
memset(vis,0,sizeof(vis));
ans=0;
n=read();
m=read();
k=sqrt(n);
for(int i=1; i<=n; ++i) {
a[i]=read();
belong[i]=(i-1)/k+1;
}
for(int i=1,x,y; i<=m; ++i) {
q[i].x=read();
q[i].y=read();
q[i].id=i;
}
sort(q+1,q+1+m);
for(register int i=1,l=1,r=0; i<=m; ++i) {
register int x=q[i].x,y=q[i].y;
while(r < y) Add(a[++r]);
while(r > y) Delet(a[r--]);
while(l < x) Delet(a[l++]);
while(l > x) Add(a[--l]);
q[i].ans=ans;
}
sort(q+1,q+1+m,cmp);
for(int i=1; i<=m; ++i)
printf("%d\n",q[i].ans);
}
return 0;
}
HDU 4638Group (莫队)的更多相关文章
- Hdu 5213-Lucky 莫队,容斥原理,分块
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213 Lucky Time Limit: 6000/3000 MS (Java/Others) Me ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...
- HDU 4358 莫队算法+dfs序+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)T ...
- HDU 4638 (莫队)
题目链接:Problem - 4638 做了两天莫队和分块,留个模板吧. 当插入r的时候,设arr[r]代表r的位置的数字,判断vis[arr[r-1]]和vis[arr[r+1]]是否访问过,如果两 ...
- HDU 4638 莫队算法
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 5145(莫队算法+逆元)
NPY and girls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 6333 莫队+组合数
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- HDU 6534 莫队+ 树状数组
题意及思路:https://blog.csdn.net/tianyizhicheng/article/details/90369491 代码: #include <bits/stdc++.h&g ...
- HDU 5145 NPY and girls (莫队分块离线)
题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...
随机推荐
- pip或easy_install安装库报错:SSL: CERTIFICATE_VERIFY_FAILED
使用pip和easy_install安装那个lxml.pyspider这些库或者框架一直提示以下错误: Collecting pyspider Could not fetch URL https:// ...
- centos7.2 源码编译安装php7.2.4 apache2.4.37 https证书安装
一.php7.2.11源码安装 1.下载php7.2.11 wget http://cn2.php.net/downloads.php/php-7.2.11.tar.gz#### 2.安装依赖 yum ...
- python 经验:把全局变量放在一个类中
注:红色是我增加的注释 add by zhj:其实python中的import, from-import语句是创建新的同名变量指向引入的模块和模块属性,这也就解释了下面的情况. 我们应尽量不用全局变量 ...
- TcMalloc的介绍以及Windows下安装使用
本文由博主(SunboyL)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/Introduction_TcMalloc.html 介绍: TcMalloc(Threa ...
- java.io.File实战
There are many things that can go wrong: A class works in Unix but doesn't on Windows (or vice versa ...
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- kettle中源和目标表结构不一致的情况处理
创建数据仓库的过程中,往往会遇到这样的问题,例如:源表由于业务原因新增了字段,而ETL程序中是按照之前的源表结构进行抽取的,那么如果不重新构建ETL程序,新的指标就不会流入DW,问题如下图所示 创建了 ...
- my.cnf 详解
[client] port =3306 socket =/tmp/mysql.sock [mysqld] port =3306 socket =/tmp/mysql.sock basedir =/us ...
- 浅谈远程登录时,ssh的加密原理
SSH:Secure Shell,是一种网络安全协议,主要用于登录远程计算机的加密过程. 登录方式主要有两种: 1.基于用户密码的登录方式: 加密原理: 当服务器知道用户请求登录时,服务器会把 ...
- container / pull-left
<div class="container"> <h2>实例</h2> <div class="pull-left"& ...