题目链接:http://hihocoder.com/problemset/problem/1299

线段树,按照t为下标去更新v,更新的时候要保留最大的那个。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; //kirai²»ÊÇɳ²è£¬²»»áÍü¼ÇÐÞ¸Ämaxn
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
typedef struct Node {
int t, v;
}Node;
const int maxn = ;
int n, m;
int sum[maxn<<]; void pushUP(int rt) {
//modify
sum[rt] = max(sum[rt<<], sum[rt<<|]);
} void build(int l, int r, int rt) {
if(l == r) {
sum[rt] = -;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushUP(rt);
} void update(int p, int add, int l, int r, int rt) {
if(l == r) {
sum[rt] = max(sum[rt], add);
return;
}
int m = (l + r) >> ;
if(p <= m) {
update(p, add, lson);
}
else {
update(p, add, rson);
}
pushUP(rt);
} int query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return sum[rt];
}
int m = (l + r) >> ;
int ret = ;
if(L <= m) {
//modify
ret = max(ret, query(L, R, lson));
}
if(R > m) {
//modify
ret = max(ret, query(L, R, rson));
}
return ret;
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d%d", &n, &m)) {
int t, v;
build(, n, );
for(int i = ; i < n; i++) {
scanf("%d%d", &t, &v);
update(t, v, , n, );
}
int a, b;
while(m--) {
scanf("%d%d", &a, &b);
int ans = query(a, b, , n, );
if(ans <= ) printf("None\n");
else printf("%d\n", ans);
}
}
return ;
}
 #include <bits/stdc++.h>
using namespace std; #define lrt rt << 1
#define rrt rt << 1 | 1
const int maxn = ;
typedef struct Node {
int l, r;
int sum;
}Node;
Node T[maxn<<];
int n, m; void pushUP(int rt) {
T[rt].sum = max(T[lrt].sum, T[rrt].sum);
} void build(int rt, int l, int r) {
T[rt].l = l;
T[rt].r = r;
T[rt].sum = -;
if(l == r) return;
int mid = (l + r) >> ;
build(lrt, l, mid);
build(rrt, mid+, r);
pushUP(rt);
} void update(int rt, int pos, int val) {
if(T[rt].l > pos || T[rt].r < pos) return;
if(T[rt].l <= pos && T[rt].r >= pos) T[rt].sum = max(T[rt].sum, val);
if(T[rt].l == T[rt].r) return;
update(lrt, pos, val);
update(rrt, pos, val);
pushUP(rt);
} int query(int rt, int l, int r) {
int ret = -;
if(T[rt].l > r || T[rt].r < l) return ret;
if(T[rt].l >= l && T[rt].r <= r) return T[rt].sum;
if(T[rt].l == T[rt].r) return ret;
ret = max(ret, query(lrt, l, r));
ret = max(ret, query(rrt, l, r));
return ret;
} int main() {
// freopen("in", "r", stdin);
int a, b;
while(~scanf("%d%d",&n,&m)) {
build(, , n);
for(int i = ; i <= n; i++) {
scanf("%d %d", &a, &b);
update(, a, b);
}
while(m--) {
scanf("%d %d", &a, &b);
int ret = query(, a, b);
if(ret == -) puts("None");
else printf("%d\n", ret);
}
}
return ;
}

[HIHO1299]打折机票(线段树)的更多相关文章

  1. hihocoder #1299 : 打折机票 线段树

    #1299 : 打折机票 题目连接: http://hihocoder.com/problemset/problem/1299 Description 因为思念新宿的"小姐姐"们, ...

  2. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  3. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  4. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  5. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  6. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  7. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  8. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  9. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

随机推荐

  1. python实现web分页日志查看

    当我们维护一个网站时,无论前台还是后台,经常会出现各种个样的问题.有时候问题很难直观的发现,这个时候只能查看各种日志来跟踪问题.但是查看日志有各种个样的问题.首先,要用各种工具登陆到服务器,这个有时候 ...

  2. PE文件之资源讲解

    资源是PE文件中非常重要的部分,几乎所有的PE文件中都包含资源,与导入表与导出表相比,资源的组织方式要复杂得多,要了解资源的话,重点在于了解资源整体上的组织结构. 我们知道,PE文件资源中的内容包括: ...

  3. 《EnterLib PIAB深入剖析》系列博文汇总_转

    转: http://www.cnblogs.com/artech/archive/2008/08/08/1263418.html

  4. IOS-闪光灯操作

    AVCaptureDevice.h主要用来获取iphone一些关于相机设备的属性. 前置和后置摄像头 enum { AVCaptureDevicePositionBack = , AVCaptureD ...

  5. Ogre1.8.1源码编译

    本文的编译环境为Windows7_SP1 + VS2010_SP1 + CMake2.8.11   :) 资源下载 1. 下载Ogre1.8.1的源代码,下载链接地址:http://www.ogre3 ...

  6. C#中的可空类型

    public class Person { public DateTime birth; public DateTime? death; string name; public TimeSpan Ag ...

  7. 使用Visio进行UML建模

    http://www.qdgw.edu.cn/zhuantiweb/jpkc/2009/rjkf/xmwd/Visio_UmlModel.htm#_Toc80417837 内容提纲: 1.VISIO中 ...

  8. sematext

    https://sematext.atlassian.net/wiki/display/PUBLOGSENE/Syslog

  9. JMeter使用指南--转

    JMeter使用指南 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成 ...

  10. lintcode:打劫房屋 III

    题目 打劫房屋 III 在上次打劫完一条街道之后和一圈房屋之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子组成的区域比较奇怪,聪明的窃贼考察地形之后,发现这次的地形是一颗二叉树.与前两次偷窃 ...