HDU 4638 Group(莫队)题解
题意: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(莫队)题解的更多相关文章
- hdu 4638 Group 莫队算法
题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...
- HDU 5145 分块 莫队
给定n个数,q个询问[l,r]区间,每次询问该区间的全排列多少种. 数值都是30000规模 首先考虑计算全排列,由于有同种元素存在,相当于每次在len=r-l+1长度的空格随意放入某种元素即$\bin ...
- HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】
根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...
- HDU 5213 Lucky 莫队+容斥
Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...
- HDU 4638Group (莫队)
Group Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is ...
- Lucky HDU - 5213 (莫队,容斥)
WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...
- hdu 4638 Group
http://acm.hdu.edu.cn/showproblem.php?pid=4638 问题其实就是求[L,R]中有多少个连续的段 若每一个人都是一个段 那么[L,R]中每一个朋友关系就会减少一 ...
- HDU 4638 Group(分组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:给出一个数列,若干询问.每次询问区间[L,R]的最少有多少段?每一段是连续的一段且这段内的 ...
- HDU 4638 Group (线段树 | 树状数组 + 离线处理)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
随机推荐
- C# WPF Halcon HDevEngine混合编程
1. WPF+Halcon 引用halcondotnet.dll和hdevenginedotnet.dll XAML中导入命名空间xmlns:halcon=”clr-namespace:HalconD ...
- 凯撒密码、GDP格式化输出、99乘法表
1.恺撒密码的编码 s=input('明文:') print('密文:',end='') for i in s: print(chr(ord(i)+3),end='') 附加: print('字符串的 ...
- 【Linux学习八】脚本编程
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.多层bash#.和source都是当前bash [root@nod ...
- python range函数
这个函数很简单,就不写例子了,看看语法,拿来即用 python range() 函数可创建一个整数列表,一般用在 for 循环中. 函数语法 range(start, stop[, step]) 参数 ...
- mac电脑设置USB键盘按键方法,设置多显示屏镜像显示器的方法
mac电脑设置USB键盘按键方法,设置多显示屏镜像显示器的方法 设置多显示屏镜像显示器的方法 ==================== mac电脑复制粘贴使用command+c command+v - ...
- Django 创建项目流程
django 项目创建流程 1 创建项目 cmd django-admin startproject 项目名称 pycharm file -- new project -- Django -- 项目名 ...
- AtCoder Regular Contest 077 C - pushpush
题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_a Time limit : 2sec / Memory limit : 256MB Score ...
- FAQ About WOYO PDR007 Dent Removal Heat Induction System
WOYO PDR 007 is a dent repair tool for auto maintence. WOYO PDR007 Auto Body Paintless Dent Repair K ...
- Django框架----中间件
我们已经会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装饰器,这样是不是稍微有点 ...
- 自写Jquery插件 Tab
原创文章,转载请注明出处,谢谢!https://www.cnblogs.com/GaoAnLee/p/9067017.html 每每看到别人写的Jquery插件,自己也试着学习尝试,终有结果,废话不多 ...