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个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
随机推荐
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- jQuery File Upload文件上传插件简单使用
前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...
- Python中的global和nonlocal
在Python中,一个变量的scope范围从小到大分成4部分:Local Scope(也可以看成是当前函数形成的scope),Enclosing Scope(简单来说,就是外层函数形成的scope), ...
- c#事件实质
c#的事件实际上是对windows消息的封装: windows消息系统分为3部分:消息队列,消息循环,窗口过程(wndproc函数)
- 刷ROM必備的clockworkmod recovery
Desire HD 手機早早就 Root,前陣子也S-OFF 變成工程版的 HBOOT(ENG S-OFF),想要刷機的朋友一定常常聽人提起 clockworkmod recovery ,接下來就是安 ...
- eg_1
1. 编写一个程序,输出一个字符串中的大写英文字母个数,小写英文字母个数以及非英文字母个数. 第一种方法: public class Test { public static void main(St ...
- 3dContactPointAnnotationTool开发日志(十五)
有时候拖动一个窗口的时候可能直接拖出去了那就再也拖不回来只能reset重新来过: 于是开了个类成员变量在start里记录了一下panel的位置: var lp = panel.GetCompo ...
- python 爬虫 伪装
#coding=utf-8 import requests def requests_view(response): import webbrowser requests_url = response ...
- [C/C++] 友元函数和友元类
A---友元函数: class Data{ public: ... friend int f(int &m);//友元函数 ... } 友元函数是可以直接访问类的私有成员的非成员函数.它是定义 ...
- 关键系统的JVM参数推荐
1. 性能篇 1.1 建议的性能参数 1. 取消偏向锁: -XX:-UseBiasedLocking JDK1.6开始默认打开的偏向锁,会尝试把锁赋给第一个访问它的线程,取消同步块上的synchron ...