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,问 ...
随机推荐
- 【android】签署应用采用相同证书的用处
在应用的预期生命周期内,您应使用相同证书签署所有 APK 应用升级:当系统安装应用的更新时,它会比较新版本和现有版本中的证书.如果证书匹配,则系统允许更新.如果您使用不同的证书签署新版本,则必须为应用 ...
- 【mysql】mysql 备份脚本
#! /bin/bash HOST=localhost USER=root PASSWORD=password DATE_STR=$(date '+%F--%T') ERROR_LOG=/usr/ ...
- Linux扩增卷组、逻辑卷以及缩减逻辑卷
今天我们将了解怎样来扩展卷组,扩展和缩减逻辑卷.在这里,我们可以缩减或者扩展逻辑卷管理(LVM)中的分区,LVM也可称之为弹性卷文件系统. 前置需求使用LVM创建弹性磁盘存储——第一部分 什么时候我们 ...
- Linux操作系统启动流程
一般来说,所有的操作系统的启动流程基本就是: 总的来说,linux系统启动流程可以简单总结为以下几步:1)开机BIOS自检,加载硬盘.2)读取MBR,进行MBR引导.3)grub引导菜单(Boot L ...
- Git for Windows 工具的使用(一)
如果你还不知道什么是Git,只知道GitHub,但是还不会用,我想这个教程会帮助你. 前言 鉴于网上目前的教材都太落后,GitHub for Windows已经更新了多个版本,好多界面都发生了变化,所 ...
- loj2254 「SNOI2017」一个简单的询问
ref #include <algorithm> #include <iostream> #include <cstdio> #include <cmath& ...
- iOS关于Xcode上的Other linker flags
Targets选项下有Other linker flags的设置,用来填写XCode的链接器参数,如:-ObjC -all_load -force_load等.还记得我们在学习C程序的时候,从C代码到 ...
- alidoing --使用JS实现多语言框架、喜欢的请进、、瓦特平台!
大家好! 多语言实现的案例:http://alidoing.com/或者http://www.alidoing.com/ 图:切换语言界面 JS代码实现: 1.首先新建一个对象langobj,当然对象 ...
- TPS限流
限流是高可用服务需要具备的能力之一 ,粗暴简单的就像我们之前做的并发数控制.好一点的有tps限流,可用令牌桶等算法实现.<亿级流量网站架构核心技术>一书P67限流详解也有讲.dubbo提供 ...
- Python全栈开发第二期课表
day01-python 全栈开发-基础篇 01 开课介绍 01:55:13 ★ 02 开课介绍02 01:28:31 ★ 03 开课介绍03 00:22:55 ...