题目链接: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. JAVA 关于Icon,Image,ImageIcon的简单的对比参考 分类: Java Game 2014-08-14 17:08 210人阅读 评论(0) 收藏

    转自:http://blog.csdn.net/bcmissrain/article/details/7190886 其实就算是现在,我还是有不少地方概念模糊,但是下面的内容是是没有什么问题的.稍微介 ...

  2. apple开发者账号申请

    1.  登陆appleID. 2. 进入 Your Account 3. 在Account Summary 中的MemberShips中选择第一个(界面大概如下) 4.选择后进入下图. 5. yes ...

  3. CrowdFlower Winner's Interview: 1st place, Chenglong Chen

    CrowdFlower Winner's Interview: 1st place, Chenglong Chen The Crowdflower Search Results Relevance c ...

  4. silverlight的第一个程序

    摘要:silverlight是微软公司全力打造的一种跨平台.跨浏览器的RIA新技术,silverlight以XAML为界面呈现语言,支持2D矢量图形.动画.数据绑定.控件风格与模板.LINQ.WCF. ...

  5. centos msyql 安装与配置

    Mysql具有简单易学.体积小等优点,深受编程开发初学者的喜爱 工具/原料 接入Internet的Centos计算机 安装Mysql 1 Centos 6.6下安装Mysql很简单, yum list ...

  6. C# 模拟一个处理消息队列的线程类 Message Queue

    // 模拟一个处理消息队列的类 class MessageHandler { // 消息队列 private Queue<string> messageQue = new Queue< ...

  7. MYSQL注入天书之stacked injection

    第三部分/page-3 Stacked injection Background-8 stacked injection Stacked injections:堆叠注入.从名词的含义就可以看到应该是一 ...

  8. MapReduce数据流向分析

    MR数据流向示意图 步骤 1 输入文件从HDFS流向Mapper节点.在一般情况下,map所需要的数据就存在本节点,这就是数据本地化计算的优势,但是往往集群中数据分布不均衡(1000台节点,数据冗余度 ...

  9. JQuery的ajax方法

    1.使用方式: 由于是全局方法,所以调用简单:$.ajax(); 2.可输入参数: 最好是写成一个json形式,个人不建议用链式,那样看上去不太好. 参数名称 类型 描述 dataType strin ...

  10. HDU1542 Atlantis(矩形面积并)

    #pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...