Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
题目链接:
http://codeforces.com/problemset/problem/622/C
题意:
给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置。
分析:
最初没看样例直接钦定输出每个不等于t的元素位置,结果怎么想都是n2复杂度的,后来看了样例才发现是输出任意一个。。
对于一个区间,如果区间最大值和最小值相等,那么该区间元素值全部相同,那么我们维护区间的最大最小值,然后判断是否均等于t,若不等,输出最大值或最小值的位置即可,若相等, 则该区间所有元素值均等于t。
区间最大最小值用线段树维护,最初使用map来保存最大最小值所在的位置,结果TLE,改成数组就过了,就是内存难看了一点。。
感觉自己姿势怪怪的上网搜了一发标程:设dp[i]表示不等于a[i]的最大的元素下标
这样每次看t是否等于区间最右端的值,若是,则判断dp[t]是否在区间内,若不是,则最右端的值即为答案。非常巧妙。
代码:
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define sa(n) scanf("%d", &(n))
const int maxn = 6e5 + 5, maxm = 1e6 + 5, oo = 0x3f3f3f3f;
struct Node{int l;int r;int a; int b;int pa;int pb;}Tree[maxn];
int a[maxn];
int ans[maxm];
void build(int i, int l, int r)
{
Tree[i].l = l;
Tree[i].r = r;
Tree[i].a = 0;
Tree[i].b = oo;
if(l == r) {
Tree[i].pa = Tree[i].pb = l;
return;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build((i << 1) | 1, mid + 1, r);
}
void push_up(int i)
{
if(Tree[i<<1].a > Tree[(i << 1)| 1].a){
Tree[i].a = Tree[i<<1].a ;
Tree[i].pa = Tree[i << 1].pa;
}else{
Tree[i].a = Tree[(i<<1) | 1].a ;
Tree[i].pa = Tree[(i << 1) | 1].pa;
}
if(Tree[i<<1].b < Tree[(i << 1)| 1].b){
Tree[i].b = Tree[i<<1].b ;
Tree[i].pb = Tree[i << 1].pb;
}else{
Tree[i].b = Tree[(i<<1) | 1].b ;
Tree[i].pb = Tree[(i << 1) | 1].pb;
}
}
int querymax(int i, int l, int r)
{
if(Tree[i].l == l && Tree[i].r == r){
ans[Tree[i].a] = Tree[i].pa;
return Tree[i].a;
}
int mid = Tree[i].l + Tree[i].r >> 1;
if(r <= mid) return querymax(i<<1, l, r);
else if(l > mid) return querymax((i << 1)|1, l, r);
else return max(querymax(i << 1, l, mid), querymax((i << 1)|1, mid + 1, r));
}
int querymin(int i, int l, int r)
{
if(Tree[i].l == l && Tree[i].r == r){
ans[Tree[i].b] = Tree[i].pb;
return Tree[i].b;
}
int mid = Tree[i].l + Tree[i].r >> 1;
if(r <= mid) return querymin(i<<1, l, r);
else if(l > mid) return querymin((i << 1)|1, l, r);
else return min(querymin(i << 1, l, mid), querymin((i << 1)|1, mid + 1, r));
}
void update(int i, int k, int x)
{
if(Tree[i].l == k && Tree[i].r == k){
Tree[i].a = Tree[i].b = x;
return;
}
int mid = Tree[i].l + Tree[i].r >> 1;
if(k <= mid) update(i << 1, k, x);
else update((i << 1) | 1, k, x);
push_up(i);
}
int main (void)
{
int n, m;sa(n);sa(m);
build(1, 0, n - 1);
for(int i = 0; i < n; i++){
sa(a[i]);
update(1, i, a[i]);
}
int l, r, x;
int a, b;
for(int i = 0; i < m; i++){
sa(l);sa(r);sa(x);
a = querymax(1, l - 1, r - 1);
b = querymin(1, l - 1, r - 1);
if(a == b && a == x) puts("-1");
else if(a == x) printf("%d\n", ans[b] + 1);
else printf("%d\n", ans[a] + 1);
}
return 0;
}
DP方法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define sa(n) scanf("%d", &(n))
const int maxn = 6e5 + 5, maxm = 1e6 + 5, oo = 0x3f3f3f3f;
int dp[maxn];
int a[maxn];
int main (void)
{
int n, m;sa(n);sa(m);
memset(dp, -1, sizeof(dp));
for(int i = 1; i <= n; i++){
sa(a[i]);
if(a[i] == a[i - 1]) dp[i] = dp[i - 1];
else dp[i] = i - 1;
}
int l, r, t;
for(int i = 0; i < m; i++){
sa(l);sa(r);sa(t);
if(a[r] == t){
if(dp[r] < l) cout<<-1<<endl;
else cout<<dp[r]<<endl;
}else cout<<r<<endl;
}
return 0;
}
Codeforces 622C Not Equal on a Segment 【线段树 Or DP】的更多相关文章
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- CodeForces 622C Not Equal on a Segment
预处理p[i],p[i]表示:[p[i],i]这段闭区间上所有数字都是a[i] 询问的时候,如果xi==a[ri]并且p[ri]<=li,一定无解 剩下的情况都是有解的,如果xi!=a[ri], ...
- Codeforces 629D Babaei and Birthday Cake(线段树优化dp)
题意: n个蛋糕编号从小到大编号,j号蛋糕可以放在i号上面,当且仅当j的体积严格大于i且i<j,问最终可得的最大蛋糕体积. 分析: 实质为求最长上升子序列问题,设dp[i]从头开始到第i位的最长 ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
随机推荐
- 解析Vue.js中的computed工作原理
我们通过实现一个简单版的和Vue中computed具有相同功能的函数来了解computed是如何工作的.写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指 ...
- asp发送短信验证码 pst方式
<script language="jscript" runat="server"> Array.prototype.get = function ...
- Linux学习-X Server 配置文件解析与设定
X server 的配置 文件都是预设放置在 /etc/X11 目录下,而相关的显示模块或上面提到的总总模块,则主要放置在/usr/lib64/xorg/modules . 比较重要的是字型文件与芯片 ...
- python 模块相互import
模块A中import B,而在模块B中import A.这时会怎么样呢?这个在Python列表中由RobertChen给出了详细解释,抄录如下: [A.py] from B import D clas ...
- day05 模块以及内置常用模块用法
内置常用模块详解: 1 time 2 datetime 3 random 4 os 5 sys 6 shutil 7 shelve 8 xml 9 configparser 10 hashlib ...
- FCKeditor自定义编辑区CSS样式
在网站后台使用FCKeditor编辑器的时候,见到的效果可能并不完全是”所见即所得”的,因为如果在FCKeditor编辑区中使用了前台样式表中的样式,在编辑区中并不能把这些样式显示出来.解决这个问题的 ...
- Jmeter(四)_逻辑控制器详解
循环控制器: 指定其子节点运行的次数,可以使用具体的数值,也可以设置为变量 1:勾选永远:表示一直循环下去 2:如果同时设置了线程组的循环次数和循环控制器的循环次数,那循环控制器的子节点运行的次数为两 ...
- centos7下nginx添加到自定义系统服务中提示Access denied
安装好nginx后,添加到系统服务中 在/usr/lib/systemd/system下创建nginx.service文件内容如下: [Unit] Description=nginx - high p ...
- Spring 4.3.11.RELEASE文档阅读(二):Core Technologies_AOP
虽然并不是每个问题都有答案,但我想了很多问题.so, just write it down , maybe one day...... AOP: 1,AOP是啥 2,AOP思想是怎么产生的 3,AOP ...
- iOS学习笔记27-摄像头
一.摄像头 在iOS中,手机摄像头的使用有以下两种方法: UIImagePickerController拍照和视频录制 优点:使用方便,功能强大 缺点:高度封装性,无法实现一些自定义工作 AVFoun ...