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 ...
随机推荐
- 混合型log,info按大小分,error按日期
1.配置文件 <?xml version="1.0" encoding="utf-8"?> <configuration> <!- ...
- armv8 memory system
在armv8中,由于processor的预取,流水线, 以及多线程并行的执行方式,而且armv8-a中,使用的是一种weakly-ordered memory model, 不保证program or ...
- HighCharts插件学习(二)
HighCharts属性 credits: {版权信息} data: {数据功能模块} drilldown: {钻取} exporting: {导出} labels: {标签} legend: ...
- word2vector 资料
http://blog.csdn.net/garfielder007/article/details/51345201 https://cs224d.stanford.edu/lecture_note ...
- modelsim仿真正确FPGA运行不正确的可能原因 - cm4写寄存器错
困住整整一周了,工作进行不下去,中午偶遇导师,指导意见是有两种可能: 1. FPGA编译器优化代码,可以考虑把综合过程中所有的warning排查一下 2. verilog里有不可综合的语句. 又及 ...
- grouping sets 的使用
grouping sets 可以用于在计算分组聚合函数值的同时计算合计值 -- 查询结果group_id为NULL的那一行即为合计值 select group_id , count(distinct ...
- Linux服务器安装部署redis
参考地址: redis教程:http://www.runoob.com/redis/redis-tutorial.html redis百度百科:https://baike.baidu.com/item ...
- eclipse 依赖别的 工程 断点进不去
maven Debug 发现进不了断点. 点击右键-->Properties-->Java Compiler-->Classfile Generation, 勾选上Add line ...
- 报文、http、https的理解
一.何为报文? 报文是网络中交换与传输的数据单位,即站点一次性要发送的数据块.报文包含了将要发送的完整的数据信息,其长短不一致,长度不限且可变. 二.报文的作用 报文多是多个系统之间需 ...
- 深入理解HashMap+ConcurrentHashMap的扩容策略
前言 理解HashMap和ConcurrentHashMap的重点在于: (1)理解HashMap的数据结构的设计和实现思路 (2)在(1)的基础上,理解ConcurrentHashMap的并发安全的 ...