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. nohup 后台运行脚本,且可以实时查看日志

    -u加在python上 python命令加上-u(unbuffered)参数后会强制其标准输出也同标准错误一样不通过缓存直接打印到屏幕. 这是因为python的缓存机制所决定的 如果是使用 nohup ...

  2. 【数论】小A进学校

    小A进学校 题目描述 近日,清华大学挖出来一个明清古墓.小A决定冒充考古系科研人员去盗墓.他遇到的第一个难关是来自校门口保安的质疑,因为小没有清华学生证,所以保安决定通过问问题的方式验证小A的身份. ...

  3. 用ASP.NET Web API技术开发HTTP接口(二)

    在第一部分,我们创建了一个基本的ASP.NET Web API项目,新建成功了数据表,然后添加了一些测试数据,最后创建了API控制器,用json格式把数据表里面的内容成功输出到浏览器上.接下来我们将继 ...

  4. Task 开始 停止

    注意点:需要将每个线程的 MemoryCacheManager 保存,这里我保存在缓存中,需要取消时根据缓存key值取出 MemoryCacheManager //开始Task1 private vo ...

  5. Java Web 深入分析(10) Spring 实践

    Spring helloworld [http://wiki.jikexueyuan.com/project/spring/hello-world-example.html] HelloWorld.j ...

  6. interface Part2(定义接口)

    一. 在 C# 语言中,类之间的继承关系仅支持单重继承,而接口是为了实现多重继承关系设计的. 二. 一个类能同时实现多个接口,还能在实现接口的同时再继承其他类,并且接口之间也可以继承. 三. 无论是表 ...

  7. iOS - error:unrecognized selector sent to class 导入第三方SDK .a后不识别,运行崩溃

    今天将app统计的.a静态库包含到一个app应用中,调试时报下面的错误: *** Terminating app due to uncaught exception 'NSInvalidArgumen ...

  8. nginx 反向代理配置(一)

    文章参考:https://blog.csdn.net/physicsdandan/article/details/45667357       什么是代理?       代理在普通生活中的意义就是本来 ...

  9. 2019年6月车型数据Access数据库+缩略图 更新于2019年6月5日.

    工作需要才来采集的, 数据来源某卡汽车网, 分享出来给需要的人吧, 本着分享的精神, 我就不猥琐的放到csdn下载了 本来是sql server的, 我导出到access了, 也方便大家查看. 顺手抓 ...

  10. Android状态栏和导航栏

    1.隐藏状态栏或导航栏 View decordView = getWindow().getDecorView(); /*SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI ...