题意:n个数,每个数有一个值,每次询问一个区间,问你这个区间能分成连续的几段(比如7 1 2 8 就是两端 1 2 和 7 8)

思路:莫队。因为L、R移动顺序wa了20发...问了一下别人,都是先扩大范围,再缩小...以后就这样写吧...

代码:

#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = + ;
int vis[maxn], arr[maxn], ans[maxn];
int T, n, m, ret;
struct node{
int l, r;
int pos, id;
bool operator < (const node &x) const{
if(pos == x.pos) return r < x.r;
return pos < x.pos;
}
}p[maxn];
void add(int x){
vis[x] = ;
if(vis[x - ] && vis[x + ]) ret--;
else if(!vis[x - ] && !vis[x + ]) ret++;
}
void del(int x){
vis[x] = ;
if(vis[x - ] && vis[x + ]) ret++;
else if(!vis[x - ] && !vis[x + ]) ret--;
}
void solve(){
memset(vis, ,sizeof(vis));
int L = , R = ;
ret = ;
for(int i = ; i < m; i++){
int l = p[i].l, r = p[i].r;
if(r < L || l > R){
ret = ;
for(int i = L; i <= R; i++)
vis[arr[i]] = ;
for(int i = l; i <= r; i++)
add(arr[i]);
L = l, R = r;
}
while(L > l){
L--;
add(arr[L]);
}
while(R < r){
R++;
add(arr[R]);
}
while(L < l){
del(arr[L]);
L++;
}
while(R > r){
del(arr[R]);
R--;
}
ans[p[i].id] = ret;
}
}
int main(){
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
int block = sqrt(n * 1.0);
for(int i = ; i <= n; i++)
scanf("%d", &arr[i]);
for(int i = ; i < m; i++){
scanf("%d%d", &p[i].l, &p[i].r);
p[i].id = i;
p[i].pos = p[i].l / block;
}
sort(p, p + m);
solve();
for(int i = ; i < m; i++){
printf("%d\n", ans[i]);
}
}
return ;
}

HDU 4638 Group(莫队)题解的更多相关文章

  1. hdu 4638 Group 莫队算法

    题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...

  2. HDU 5145 分块 莫队

    给定n个数,q个询问[l,r]区间,每次询问该区间的全排列多少种. 数值都是30000规模 首先考虑计算全排列,由于有同种元素存在,相当于每次在len=r-l+1长度的空格随意放入某种元素即$\bin ...

  3. HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】

    根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...

  4. HDU 5213 Lucky 莫队+容斥

    Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...

  5. HDU 4638Group (莫队)

    Group Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is ...

  6. Lucky HDU - 5213 (莫队,容斥)

    WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...

  7. hdu 4638 Group

    http://acm.hdu.edu.cn/showproblem.php?pid=4638 问题其实就是求[L,R]中有多少个连续的段 若每一个人都是一个段 那么[L,R]中每一个朋友关系就会减少一 ...

  8. HDU 4638 Group(分组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:给出一个数列,若干询问.每次询问区间[L,R]的最少有多少段?每一段是连续的一段且这段内的 ...

  9. HDU 4638 Group (线段树 | 树状数组 + 离线处理)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. C# SQLite 数据库操作

    C# SQLite 数据库操作学习 运行环境:Window7 64bit,.NetFramework4.61,C# 7.0 参考: SQLite 官网 SQL As Understood By SQL ...

  2. web基础,用html元素制作web页面

    用div,form制作登录页面,尽可能做得漂亮. 练习使用下拉列表选择框,无序列表,有序列表,定义列表. 观察常用网页的HTML元素,在实际的应用场景中,用已学的标签模仿制作. <!DOCTYP ...

  3. 【安装虚拟机三】设置Linux IP地址

    环境 VMware 10 CentOS-6.5-x86_64 第一步:查看IP信息linux:ifconfig (windows:ipconfig) 第二步:编辑网卡信息 vi /etc/syscon ...

  4. BufferReader BufferWriter

    Copying information from one file to another with 'BufferReader BufferWriter' public class Demo5 { p ...

  5. SQL优化(转)

    1. 负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好习惯 可以优化为in查询: ...

  6. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  7. Deprecated: getEntityManager is deprecated since Symfony 2.1

    PHP5.3应用中,登陆后台管理时提示错误: Deprecated: getEntityManager is deprecated since Symfony 2.1. Use getManager  ...

  8. JAVA基本语法测试

    一: 1,JAVA的基本运行单位是类 2,类的成员:成员变量,构造方法,普通方法和内部类 3,成员变量种类:字符类型:char        布尔类型:boolean     数值类型:byte, s ...

  9. Array和ArrayList不同

    Employee[] array = new Employee[10]; ArrayList<Employee> staff = new ArrayList<>(); 不同 A ...

  10. 不懂RPC实现原理怎能实现架构梦

    RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...