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 线段树的更多相关文章

  1. lightoj 1179(线段树)

    传送门:Josephus Problem 题意:经典约瑟夫问题,有n个人,每次数到第k个人出列,求剩下的最后一人. 分析:用线段树模拟约瑟夫问题,记录区间的减少情况,然后根据每次数到的人在区间排第几位 ...

  2. LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化

    http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...

  3. LightOJ 1097 - Lucky Number 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1097 题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3 ...

  4. lightoj 1084 - Winter(dp+二分+线段树or其他数据结构)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1084 题解:不妨设dp[i] 表示考虑到第i个点时最少有几组那么 if a[i ...

  5. LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树

    分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> ...

  6. LightOJ 1135(线段树)

    题解引自:http://www.cnblogs.com/wuyiqi/archive/2012/05/27/2520642.html 题意: 有n个数,刚开始都为0 add i , j 给i,j区间内 ...

  7. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

  8. LightOJ 1348 (树链剖分 + 线段树(树状数组))

    题目 Link 分析 典型的树链剖分题, 树链剖分学习资料 Code #include <bits/stdc++.h> using namespace std; const int max ...

  9. Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))

    题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...

随机推荐

  1. 使用libpcab抓包&处理包

    #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h& ...

  2. 全排列 next_permutation() 函数的用法

    在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...

  3. <Effective C++>读书摘要--Implementations<一>

    1.For the most part, coming up with appropriate definitions for your classes (and class templates) a ...

  4. Debian 7 amd64--TP-LINK TL-WN725N 2.0源码驱动编译安装

    租房用的是无线网络,在新安装的Debian 7 amd64使用的无线网卡型号是TP-LINK TL-WN725N 2.0,发现驱动安装还是有些问题,折腾了很久,特意在此记录一下. TL-WN725N ...

  5. YaoLingJump开发者日志(九)V1.1.1版本完成

    跳跃吧瑶玲下载连接 百度网盘下载 介绍   Android版本升级到8.0,发现原来的图标不显示了:   百度了一下解决方案,用了该博客提供的第二种方法,修改roundIcon的内容.   可爱的图标 ...

  6. 【week4】课堂Scrum站立会议

    项目:连连看游戏 小组名称:天天向上(旁听) 小组成员:张政 张金生 李权 武致远 已完成任务 1.本项目采用c#. 2. 初步界面. 形成一个windows下的游戏界面,每个需要消除的方块是一个bu ...

  7. 第43天:事件对象event

    一.事件对象事件:onmouseover. onmouseout. onclickevent //事件的对象 兼容写法:var event = event || window.event; event ...

  8. Log-spectral distance

    Log-spectral distance对数频谱距离 log-spectral distance(LSD),也指 log-spectral distortion,是两个频谱之间的距离度量(用分贝表示 ...

  9. BZOJ 1509 逃学的小孩(树的直径)

    题意:从树上任找三点u,v,w.使得dis(u,v)+min(dis(u,w),dis(v,w))最大. 有一个结论u,v必是树上直径的两端点. 剩下的枚举w就行了. 具体不会证... # inclu ...

  10. 【poj2104】K-th Number 主席树

    题目描述 You are working for Macrohard company in data structures department. After failing your previou ...