A Sequence Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 712    Accepted Submission(s): 114

Problem Description
One day, WNJXYK found a very hard problem on an Online Judge. This problem is so hard that he had been thinking about the solutions for a couple of days. And then he had a surprise that he misunderstood that problem and easily figured out a solution using segment tree. Now he still wonders that solution for the misread problem.
There is a sequence with N positive integers A1,A2,...,An and M queries. Each query will give you an interval [L,R] and require an answer with YES/NO indicates that whether the numbers in this interval are continuous in its integer range. 
Let us assume that the maximal number in an interval is mx and the minimal number is mi. The numbers in this interval are continuous in its integer range means that each number from mi to mx appears at least once in this interval.
 
Input
The input starts with one line contains exactly one positive integer T which is the number of test cases. And then there are T cases follow.
The first line contains two positive integers n,m which has been explained above.The second line contains positive integers A1,A2,...,An.
Then there will be m lines followed. Each line contains to positive numbers Li,Ri indicating that the ith query’s interval is [Li,Ri].
 
Output
For each test case, output m line.
Each of following m lines contains a single string “YES”/ “NO” which is the answer you have got.
 
Sample Input
2
3 3
3 1 2
2 3
1 3
1 2
5 3
1 2 2 4 5
1 5
1 3
3 3
 
Sample Output
YES
YES
NO
NO
YES
YES

Hint

T=5
1<=n<=100000
1<=Ai<=10^9
1<=m<=100000
The input file is very large, so you are recommend to use scanf() and printf() for IO.

题解:线段树/ST 求区间最大值和最小值,用莫队解决q次查询 复杂度4 *n*logn+nsqrt(q)。要注意莫队本身复杂度nsqrt(q)就很高了,所以尽量通过预处理使里面的基本操作复杂度变低,而且尽量采用离散化的方式来避免使用map和unordered_map,前者复杂度O(logn),后者虽然是O(1),但常数很大,都不适合放在莫队里面
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<"["<<#x<<"]"<<" "<<x<<endl;
const int maxn=1e5+;
int a[maxn],b[maxn],c[maxn],in[maxn];
struct node{
int maxx;
int minn;
int l;
int r;
}Node[maxn<<];
struct Qa{
int l;
int r;
int id;
int bloc;
}q[maxn];
int ANS[maxn];
void pushup(int rt){
Node[rt].maxx=max(Node[rt<<].maxx,Node[(rt<<)|].maxx);
Node[rt].minn=min(Node[rt<<].minn,Node[(rt<<)|].minn);
}
void build(int L,int R,int rt){
Node[rt].l=L;
Node[rt].r=R;
if(L==R){
Node[rt].maxx=Node[rt].minn=a[L];
return;
}
int mid=(L+R)/;
build(L,mid,rt<<);
build(mid+,R,(rt<<)|);
pushup(rt);
}
bool cmp(struct Qa aa,struct Qa bb){
if(aa.bloc==bb.bloc)return aa.r<bb.r;
return aa.bloc<bb.bloc;
}
void query(int L,int R,int rt,int L1,int R1,int &m1,int &m2){
//debug(L);
if(L1<=L&&R1>=R){
m1=max(m1,Node[rt].maxx);
m2=min(m2,Node[rt].minn);
return;
}
int mid=(L+R)/;
if(mid>=L1)query(L,mid,rt<<,L1,R1,m1,m2);
if(mid<R1)query(mid+,R,(rt<<)|,L1,R1,m1,m2);
}
int main()
{
//cout<<100000ll*sqrt(100000)+400000ll*log(100000)<<endl;
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
//unordered_map<int,int>mp;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
in[i]=;
}
build(,n,);
int len=sqrt(n);
for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
q[i].bloc=q[i].l/len;
ANS[i]=;
}
sort(q+,q++m,cmp);
sort(b+,b++n);
int L=;
int R=;
int tot=;
int kk=unique(b+,b++n)-b-;
for(int i=;i<=n;i++){
int pos=lower_bound(b+,b++kk,a[i])-b;
c[i]=pos;
}
in[c[]]++;
for(int i=;i<=m;i++){
int m1=a[q[i].l];
int m2=a[q[i].r];
query(,n,,q[i].l,q[i].r,m1,m2);
while(L<q[i].l){
in[c[L]]--;
if(in[c[L]]==){
tot--;
}
L++;
}
while(L>q[i].l){
L--;
in[c[L]]++;
if(in[c[L]]==){
tot++;
}
}
while(R>q[i].r){
in[c[R]]--;
if(in[c[R]]==){
tot--;
}
R--;
}
while(R<q[i].r){
R++;
in[c[R]]++;
if(in[c[R]]==){
tot++;
}
}
if(tot==m1-m2+){
ANS[q[i].id]=;
}
}
for(int i=;i<=m;i++){
if(ANS[i]){
printf("YES\n");
}
else{
printf("NO\n");
}
}
}
return ;
}

[hdoj6483][莫队+线段树/ST]的更多相关文章

  1. Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树

    E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...

  2. 洛谷P3246 序列 [HNOI2016] 莫队/线段树+扫描线

    正解:莫队/线段树+扫描线 解题报告: 传送门! 似乎是有两种方法的,,,所以分别港下好了QAQ 第一种,莫队 看到这种询问很多区间之类的就会自然而然地想到莫队趴?然后仔细思考一下,发现复杂度似乎是欧 ...

  3. 【CF633H】Fibonacci-ish II 莫队+线段树

    [CF633H]Fibonacci-ish II 题意:给你一个长度为n的序列$a_i$.m个询问,每个询问形如l,r:将[l,r]中的所有$a_i$排序并去重,设得到的新数列为$b_i$,求$b_1 ...

  4. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  5. [bzoj4358]permu:莫队+线段树/回滚莫队

    这道题是几天前水过去的,现在快没印象了,水一发. 首先我们看到它让求解的是最长的值域 连续段长度,很好. 然后就想到了山海经,但但是我还没有做. 然后又想到了很久以前的一次考试的T3旅馆hotel(我 ...

  6. BZOJ 4129 树上带修莫队+线段树

    思路: 可以先做做BZOJ3585 是序列上的mex 考虑莫队的转移 如果当前数字出现过 线段树上把它置成1 对于询问 二分ans 线段树上查 0到ans的和 是不是ans+1 本题就是把它搞到了序列 ...

  7. bzoj 3289: Mato的文件管理 莫队+线段树

    题目链接 给一些询问,每个询问给出区间[L, R] , 求这段区间的逆序数. 先分块排序, 然后对于每次更改, 如果是更改L, 那么应该查询区间内比他小的数的个数, 如果更改R, 查区间内比他大的数的 ...

  8. BZOJ 4358 坑 莫队+线段树 死T

    这是一个坑 竟然卡nsqrt(n)logn T死 等更 //By SiriusRen #include <cmath> #include <cstdio> #include & ...

  9. CF633H Fibonacci-ish II(莫队+线段树)

    温馨提示:本题十分卡常数,我手动开O2才过的.而数据范围不伦不类的n<=30000,常数小的O(n2)居然比O(n√nlogn)跑得快…… 考虑插进去一个元素对答案产生的影响.原本数列为Σa[i ...

随机推荐

  1. 031 Android 异步任务(AsyncTask)

    1.介绍 AsyncTask(了解即可),重点掌握Handler+Thread 2.实现方法 3.执行步骤 4.java后台 package com.lucky.test36asynctask; im ...

  2. js判断是Android还是iOS

    var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf('Android') > - ...

  3. [转帖]Linux内核系统体系概述

    Linux内核系统体系概述 https://www.cnblogs.com/alantu2018/p/8447369.html Linux 内核主要由 5 个模块构成,它们分别是: 进程调度模块 用来 ...

  4. Python23之内置函数filter()和map()

    首先我们了解一个概念:迭代 迭代是访问集合元素的⼀种⽅式.迭代器是⼀个可以记住遍历的位置的对象.迭代器对象从集合的第⼀个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 我们已经知道 ...

  5. 剑指offer41:所有和为S的连续正数序列,例如,有多少种连续的正数序列的和为100

    1 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久 ...

  6. 【并发】8、借助redis 实现多线程生产消费阻塞队列

    顾名思义这个就是再消费的时候,不是之前的那哥用yield进行线程切换的操作,而是用线程等待阻塞的方式去执行,说实话我感觉效率不一定有之前那个好, 因为我对这种阻塞队列使用的时候,之前有发现阻塞队列,塞 ...

  7. vue的安装配置与项目创建

    1,安装vue必须先安装node.js.  --------去官网安装node.js 因为npm依赖node.js环境,使用npm的时候需要安装node.js.安装node.js的时候npm会默认安装 ...

  8. git 如何取消add操作

    可以直接使用命令    git reset HEAD 这个是整体回到上次一次操作 绿字变红字(撤销add) 如果是某个文件回滚到上一次操作:  git reset HEAD  文件名 红字变无 (撤销 ...

  9. sqlserver导入Excel数据 总是报错:错误 0xc020901c: 数据流任务 1: 输出“Excel 源输出”(55) 上的 输出列“T2”(64) 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项

    在网络上搜索解决办法,解决办法是把excel导入到access数据库中,再把access数据库导入到sqlsever中,公司机器上不让安装office工具,问了一个同事得到的回答是把数据中很长的那行数 ...

  10. MongoDB安装及环境配置

    一.什么是MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...