题意: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. field_automation源码分析

    field_automation主要通过uvm_field_utils_begin和uvm_field_utils_end来加到自己写的class中. uvm_field_utils_begin,在o ...

  2. VMWare虚拟机 window文件传递

    无论是将虚拟机的文件传到window上或者是将window上文件传到虚拟机上: 都可以选中文件,然后拖动文件到另一个系统上 提前:虚拟机安装了VMWARE Tools 1)window上文件拖到虚拟机 ...

  3. Python 构造一个可接受任意数量参数的函数

    为了能让一个函数接受任意数量的位置参数,可以使用一个* 参数 在这个例子中,rest 是由所有其他位置参数组成的元组.然后我们在代码中把它当成了一个序列来进行后续的计算

  4. 原生JavaScript插件编写指南(转载)

    原生js开发指南 https://www.jianshu.com/p/e65c246beac1 在jQuery大量使用的环境下,目前网上的众多jQuery插件也能基本满足要求,但是在项目具体需求下,有 ...

  5. Python框架----cookie和session

    一.cookie和session的介绍 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...

  6. python的类和对象

    一.面向对象和面向过程 1.1面向过程的特点 优点是:极大的降低了写程序的复杂度,只需要顺着要执行的步骤,堆叠代码即可. 缺点是:一套流水线或者流程就是用来解决一个问题,代码牵一发而动全身. 1.2面 ...

  7. python的一些遗漏用法

    一. 补充基础数据类型的相关知识点 1. str. join() 把列表变成字符串 li = ["李嘉诚", "麻花藤", "⻩海峰", & ...

  8. 【js】手机浏览器端唤起app,没有app就去下载app 的方法

    这种功能的作用: 1.一般公司有自己的app,而app是需要不断有新用户涌入才能持续运营,达到不错的收入.就需要使用这种方式进行引入新的用户. 2.一些内容在网页端体验不好,或者一些功能需要app内才 ...

  9. webVR框架A-frame

    A-frame:https://blog.csdn.net/sun124608666/article/details/77869570 three.js学习文档:http://www.hewebgl. ...

  10. The logback manual #01# Introduction

    依赖包如下pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...