题目链接: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. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

  2. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  3. android解析XML总结(SAX、Pull、Dom三种方式) <转载>

    android解析XML总结(SAX.Pull.Dom三种方式) http://www.cnblogs.com/JerryWang1991/archive/2012/02/24/2365507.htm ...

  4. ios 缓存相关信息收集

    链接:http://www.cnblogs.com/pengyingh/category/353093.html 使用NSURLCache让本地数据来代替远程UIWebView请求 摘要: 原文作者: ...

  5. 服务器NPC的ID如何分配的

    服务器ID分配包括NPC,Monster,Pet的ID分配都是调用allocateUID然后自动保存的ID加一,pet说是通过玩家的ID移位获得的,调试一下发现还是调用allocateUID,如果通过 ...

  6. 驱动笔记 - file_operations

    #include <linux/fs.h> struct file_operations { struct module *owner; loff_t (*llseek) (struct ...

  7. 能够将 HTML 表格转换成图表的jQuery插件:Chartinator

    点这里 一个jQuery 插件能够将HTML 表格转换成图表,使用 Google Charts 实现. Chartinator当前支持以下特性: Creation of the following c ...

  8. Action Bar详解

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  9. 牛 JQuery视频笔记

    QX3GLL 包装集 next() nextAll() nextAll("div"); prev();prevAll() end();andSlf(); eq(2);gt(1);l ...

  10. 广播接收者BroadcastReceiver

    BroadcastReceiver与activity,service有完整的生命周期不同,BroadcastReceiver本质上是一系统级别的监听器,专门负责监听各程序发出的broadcast.与程 ...