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 ...
随机推荐
- php核心纪要 整理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 仿豆瓣网(电脑版网页)HTML+CSS实现
步骤一:将豆瓣电脑版网页以图片形式保存下来: 利用了chrome里面的插件: 步骤二:将图片放置到PS中,研究布局: 我将其分为header部分,banner部分,section部分,footer部分 ...
- 基于TCP/IP协议的socket通讯server
思路: socket必须要随项目启动时启动,所以需用Spring自带的监听器,需要保持长连接,要用死循环,所以必须另外起线程,不能阻碍主线程运行 1.在项目的web.xml中配置listener &l ...
- cvc-complex-type.3.2.2: 元素 'constructor-arg' 中不允许出现属性 'name'
将版本号改成 3.0 以上的即可.
- weblogic 乱码
1.找到weblogic安装目录,当前项目配置的domain 2.找到bin下的setDomainEnv.cmd文件 3.打开文件,从文件最后搜索第一个set JAVA_OPTIONS=%JAVA_O ...
- Kylin, Mondrian, Saiku系统的整合
本文主要介绍有赞数据团队为了满足在不同维度查看.分析重点指标的需求而搭建的OLAP分析工具.这个工具对Kylin.Mondrian以及Saiku做了一个整合,主要工作包括一些定制化的修改以及环境的配置 ...
- FCoin优势
FCoin优势 顶级技术金融级别的速度和稳定,交易高效有保障:国际顶尖团队自主研发撮合系统,能够每秒处理200万笔交易:证券级先进算法,支持GTT.GTC.FOK.IOC等多种专业交易指令,为交易者提 ...
- CentOS6.4下邮件服务器搭建
CentOS6.4下邮件服务器搭建 linux下邮件服务器的搭建大致分为三个步骤 准备工作(真实的生产环境下需要) 发送服务器安装及配置 (Postfix) 接收服务器安装及配置(dovecot) ...
- 使用Holer外网SSH访问内网(局域网)Linux系统
1. Holer工具简介 Holer exposes local servers behind NATs and firewalls to the public internet over secur ...
- linux下nginx整合php
在nginx中药使用php可不像apache那样,因为apache是把php当做自己的一个模块来启动的, 而我们的nginx是把http请求转发给php程序,也就是说,php和nginx是相互独立的的 ...