LightOJ 1093 - Ghajini 线段树
http://www.lightoj.com/volume_showproblem.php?problem=1093
题意:给定序列,问长度为d的区间最大值和最小值得差最大是多少。
思路:可以使用线段树做,由于固定区间长度,还可以使用单调队列。
/** @Date : 2016-12-06-18.39
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/ #include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct yuu
{
int l, r;
int ma;
int mi;
}tt[N << 2]; int a[N]; void pushup(int p)
{
tt[p].ma = max(tt[p << 1].ma, tt[p << 1 | 1].ma);
tt[p].mi = min(tt[p << 1].mi, tt[p << 1 | 1].mi);
} void build(int l, int r, int p)
{
tt[p].l = l;
tt[p].r = r;
tt[p].ma = 0;
tt[p].mi = INF;
if(l == r)
{
tt[p].ma = tt[p].mi = a[l];
return ;
}
int mid = (l + r) >> 1;
build(l , mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
} void updata(int l, int r, int v, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
{
return ;
}
int mid = (tt[p].l + tt[p].r) >> 1;
if(l <= mid)
updata(l, r, v, p << 1);
if(r > mid)
updata(l, r, v, p << 1 | 1);
pushup(p);
} PII query(int l, int r, int p)//直接返回pair会超时
{
//cout <<tt[p].ma <<"~" <<tt[p].mi << endl;
if(l <= tt[p].l && r >= tt[p].r)
return MP(tt[p].ma, tt[p].mi);
int mid = (tt[p].l + tt[p]. r) >> 1;
PII ans;
ans.fi = 0;
ans.se = INF;
if(l <= mid)
{
ans.fi = max(ans.fi, query(l, r, p << 1).fi);
ans.se = min(ans.se, query(l, r, p << 1).se);
}
if(r > mid)
{
ans.fi = max(ans.fi, query(l, r, p << 1 | 1).fi);
ans.se = min(ans.se, query(l, r, p << 1 | 1).se);
}
return ans;
} int queryma(int l, int r, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
return tt[p].ma;
int mid = (tt[p].l + tt[p].r) >> 1;
int ma = 0;
if(l <= mid)
ma = max(ma, queryma(l, r, p << 1));
if(r > mid)
ma = max(ma, queryma(l, r, p << 1 | 1));
return ma;
} int querymi(int l, int r, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
return tt[p].mi;
int mid = (tt[p].l + tt[p].r) >> 1;
int mi = INF;
if(l <= mid)
mi = min(mi, querymi(l, r, p << 1));
if(r > mid)
mi = min(mi, querymi(l, r, p << 1 | 1));
return mi;
}
int main()
{
int T;
int cnt = 0;
cin >> T;
while(T--)
{
int n, d;
scanf("%d%d", &n, &d);
for(int i = 1; i <= n; i++)
{
scanf("%d", a + i);
}
build(1, n, 1);
//cout << query(2, 3, 1) << endl;
int ma = 0;
for(int i = 1; i + d - 1 <= n; i++)
{
//PII x = query(i, i + d - 1, 1);
ma = max(queryma(i, i + d - 1, 1) - querymi(i, i + d - 1, 1), ma);
//cout << ma <<endl;
}
printf("Case %d: %d\n", ++cnt, ma);
}
return 0;
}
LightOJ 1093 - Ghajini 线段树的更多相关文章
- lightoj 1179(线段树)
传送门:Josephus Problem 题意:经典约瑟夫问题,有n个人,每次数到第k个人出列,求剩下的最后一人. 分析:用线段树模拟约瑟夫问题,记录区间的减少情况,然后根据每次数到的人在区间排第几位 ...
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- LightOJ 1097 - Lucky Number 线段树
http://www.lightoj.com/volume_showproblem.php?problem=1097 题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3 ...
- lightoj 1084 - Winter(dp+二分+线段树or其他数据结构)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1084 题解:不妨设dp[i] 表示考虑到第i个点时最少有几组那么 if a[i ...
- LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树
分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...
- LightOJ 1135(线段树)
题解引自:http://www.cnblogs.com/wuyiqi/archive/2012/05/27/2520642.html 题意: 有n个数,刚开始都为0 add i , j 给i,j区间内 ...
- LightOJ 1085(树状数组+离散化+DP,线段树)
All Possible Increasing Subsequences Time Limit:3000MS Memory Limit:65536KB 64bit IO Format: ...
- LightOJ 1348 (树链剖分 + 线段树(树状数组))
题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
随机推荐
- Java抽象与接口的区别
Java抽象与接口的区别 答案方式一.简单来说,1.接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的, 2.另外,实现接口的一定要实现接口里定义的所有 ...
- Spring Security 快速了解
在Spring Security之前 我曾经使用 Interceptor 实现了一个简单网站Demo的登录拦截和Session处理工作,虽然能够实现相应的功能,但是无疑Spring Security提 ...
- A4
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 很胖,刚学,照猫画虎做了登录与注册界面. 展示GitHub ...
- lintcode-189-丢失的第一个正整数
189-丢失的第一个正整数 给出一个无序的正数数组,找出其中没有出现的最小正整数. 样例 如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2 挑战 只允许 ...
- iOS开发UIApplication用法
1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplication对象,而且是单例的,如果 ...
- Jenkins系列-Jenkins通过Publish over SSH插件实现远程部署
配置ssh免秘钥登录 安装Publish over SSH插件 插件使用官网:https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plug ...
- C#窗口文件双击打开时出错
出错原因: 1. 修改了该窗口文件的.Designer.cs文件中:#region Windows 窗体设计器生成的代码这里面的代码,导致运行不正常. 为了传递数据,我在构造函数中增加了传递的值. 需 ...
- centos7编译安装redis遇坑
编译redis时:make cc Command not found 原因分析:没有安装gcc,执行: yum install gcc 编译redis时:error: jemalloc/jemallo ...
- Bootstrap排版类
类 描述 实例 .lead 使段落突出显示 尝试一下 .small 设定小文本 (设置为父文本的 85% 大小) 尝试一下 .text-left 设定文本左对齐 尝试一下 .text-center 设 ...
- 2011 Multi-University Training Contest 8 - Host by HUST
Rank:56/147. 开场看B,是个线段树区间合并,花了2hour敲完代码...再花了30min查错..发现push_down有问题.改了就AC了. 然后发现A过了很多人.推了个公式,发现是个分段 ...