传送门

这个题的方法好像很多啊

1.莫队暴力

2.线段树 + 离线处理

先预处理出sg[i]表示前i个数的sg值,next[i]表示i的下一位置在哪里,如果后面再没有i,那么next[i] = n + 1

然后把线段树的每个叶子节点放上sg[i]。

把询问按照左端点由小到大排序,我们考虑如何从 l ~ r 转移到 l + 1 ~ r,

会发现,当把a[l]这个数去掉之后,如果后面没有a[l]那么答案就可能会更新,

那么我们可以更新 l + 1 ~ next[l] - 1这个区间,也就是用线段树操作

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 200001
#define INF ~(1 << 31)
#define root 1, 1, n
#define ls now << 1, l, mid
#define rs now << 1 | 1, mid + 1, r
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, q;
int a[N], next[N], vis[N], ans[N], mx[N << 2], sg[N]; struct node
{
int x, y, id;
}p[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline bool cmp(node x, node y)
{
return x.x < y.x;
} inline void build(int now, int l, int r)
{
if(l == r)
{
mx[now] = sg[l];
return;
}
mx[now] = INF;
int mid = (l + r) >> 1;
build(ls);
build(rs);
} inline void push_down(int now)
{
if(mx[now] != INF)
{
mx[now << 1] = min(mx[now << 1], mx[now]);
mx[now << 1 | 1] = min(mx[now << 1 | 1], mx[now]);
mx[now] = INF;
}
} inline void update(int now, int l, int r, int x, int y, int d)
{
if(x <= l && r <= y)
{
mx[now] = min(mx[now], d);
return;
}
push_down(now);
int mid = (l + r) >> 1;
if(x <= mid) update(ls, x, y, d);
if(mid < y) update(rs, x, y, d);
} inline int query(int now, int l, int r, int x)
{
if(l == r) return mx[now];
push_down(now);
int mid = (l + r) >> 1;
if(x <= mid) return query(ls, x);
else return query(rs, x);
} int main()
{
int i, j = 0, now = 1, nxt;
n = read();
q = read();
for(i = 1; i <= n; i++) a[i] = read();
for(i = 1; i <= n; i++)
{
vis[a[i]] = 1;
while(vis[j]) j++;
sg[i] = j;
}
build(root);
for(i = 0; i <= n; i++) vis[i] = n + 1;
for(i = n; i >= 1; i--) next[i] = vis[a[i]], vis[a[i]] = i;
for(i = 1; i <= q; i++)
{
p[i].id = i;
p[i].x = read();
p[i].y = read();
}
std::sort(p + 1, p + q + 1, cmp);
for(i = 1; i <= q; i++)
{
while(now < p[i].x)
{
if(now + 1 < next[now])
update(root, now + 1, next[now] - 1, a[now]);
now++;
}
ans[p[i].id] = query(root, p[i].y);
}
for(i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}

3.主席树

。。不会

[BZOJ3339] Rmq Problem(线段树)的更多相关文章

  1. Codeforces 803G Periodic RMQ Problem 线段树

    Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...

  2. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  3. [bzoj3339]Rmq Problem||[bzoj3585]mex_线段树

    Rmq Problem bzoj-3339||mex bzoj-3585 题目大意:给定一个长度为n的数列a,多次讯问区间l,r中最小的不属于集合{$A_l,A_{l+1}...A_r$}的非负整数. ...

  4. bzoj 3489 A simple rmq problem——主席树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...

  5. BZOJ3339 Rmq Problem

    [bzoj3339]Rmq Problem Description Input Output Sample Input 7 5 0 2 1 0 1 3 2 1 3 2 3 1 4 3 6 2 7 Sa ...

  6. bzoj 3489: A simple rmq problem k-d树思想大暴力

    3489: A simple rmq problem Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 551  Solved: 170[Submit][ ...

  7. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  8. 【题解】BZOJ3489 A Hard RMQ problem(主席树套主席树)

    [题解]A simple RMQ problem 占坑,免得咕咕咕了,争取在2h内写出代码 upd:由于博主太菜而且硬是要用指针写两个主席树,所以延后2hQAQ upd:由于博主太菜而且太懒所以他决定 ...

  9. Uva 12299 带循环移动的RMQ(线段树)

    题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...

随机推荐

  1. [windows]设置开机取消登录窗口选项直接进入桌面

    步骤: 菜单--〉运行--〉输入:control passwords2或rundll32 netplwizdll,UsersRunDll--〉用户账户-用户-取消勾选“要使用本机,用户必须输入用户名和 ...

  2. SnowKiting

    原文 Let's go fly a kite...in the snow Reach into your closet,find that dusty kite and clean it off - ...

  3. hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)

    题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...

  4. asp.net core mvc 异步表单(Ajax.BeginForm)

    .net core中已经没有beginform扩展函数了. 通过Bower引入jquery-ajax-unobtrusive: <script src="~/lib/jquery-aj ...

  5. lua_to_luac

    #!/bin/sh `rm -rf allLua.zip` `mkdir ./tempScripts` `mkdir ./tempScripts/scripts` `cp -a ./scripts/ ...

  6. The Django Book - 第四章 模板2

    模板(相应)使用的几种方式: 1.使用HttpResponse返回字符串HTML from django.http import HttpResponse def current_datetime(r ...

  7. RSA不对称加密和公钥 私钥

    理论上只要有加密的规则 基本都是可以解密的 但是如果解密需要消耗的时间过长 比如1000年 解密过后已经没什么意义了 此时可认为这种算法不能被破解 也就是说此加密可信 MD5 是一种单向操作 加密后不 ...

  8. VC-基础:VC中得到当前系统的时间和日期

    得到当前时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒).主要方法如下: 1)使用CRT函数 C++代码   ]; time_t nowtim ...

  9. 实验十一 团队作业7:团队项目设计完善&编码

    实验十一 团队作业7:团队项目设计完善&编码 实验时间 2019-6-6 Deadline: 2019-6-12 10:00,以团队随笔博文提交至班级博客的时间为准. 评分标准: 按时交 – ...

  10. WINDOWS下使用Mysql 中碰到的问题记录

    问题:在cmd中输入net stop mysql反馈“服务名无效” win+R打开运行窗口,输入 services.msc 查看其中mysql的服务名,比如我的是叫做MySQL80 让我们继续回到最开 ...