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值作为第二关键字进行排序. 都是 ...
随机推荐
- linux中使用arcpy
切换到对应目录 即下图的 server安装路径 /home/arcgis/arcgis/server/tools 然后输入 ./python (这一步要注意 python这个命令 ...
- Can you answer these queries?---hdu4027
题目链接 有n个数:当操作为1时求L到R的和: 当操作为0时更新L到R为原来的平方根: 不过如果仔细演算的话会发现一个2^64数的平方根开8次也就变成了 1,所以也更新不了多少次,所以可以每次更新到底 ...
- Python-OpenCV —— 基本操作详解
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.MacOS操作系统上.它轻量级而且高效——由一系列 C 函数和少量C++类构成,同时提供了Pyt ...
- mysql获取下一篇和上一篇文章的ID
mysql获取上一篇和下一篇文章的ID只要在当前页面读取上一个和下一个的ID就可以了.假设当前ID为10:搜索上一个的ID:select id from table where id<10 ...
- JDK 伪共享解决方案
关于AtomicReference AtomicReference是由JAVA5引入的,用于对一个对象引用进行原子操作,我们可以看到AtomicReference的实现是用CAS技术对引用进行指令级别 ...
- python中各种数据类型
数字类型 整型int 作用:年纪,等级,身份证号,qq号等与整型数字有关 定义: age=10 #本质age=int(10) 浮点型float 作用:薪资,身高,体重等与浮点数相关 salary=3. ...
- PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...
- 修改class文件
http://yucaifu1989.iteye.com/blog/1850500 http://blog.csdn.net/hexin373/article/details/6669813 使用ja ...
- sklearn总览
- Python Pandas找到缺失值的位置
python pandas判断缺失值一般采用 isnull(),然而生成的却是所有数据的true/false矩阵,对于庞大的数据dataframe,很难一眼看出来哪个数据缺失,一共有多少个缺失数据,缺 ...