题目链接: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. HDU 1028Ignatius and the Princess III(母函数简单题)

     Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  2. String、StringBuilder

    public class testString{ public static void main(String[] args) { String a="cool"; String ...

  3. POJ 1222

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6196   Accepted: 40 ...

  4. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. 使用git了解代码编写过程

    在看教程时,有的老师会将代码放到github,如果不想跟着视频一步一步来,那就直接clone整个代码,但整个看着又有点蒙,那就使用版本切换的功能了. 首先 git clone 下载下来 git log ...

  6. 社交APP经典死法18种,听野路子产品菜狗怎么说

    点这里 社交APP经典死法18种,听野路子产品菜狗怎么说 时间 2015-04-06 11:24:53  虎嗅网相似文章 (4)原文  http://www.huxiu.com/article/112 ...

  7. POJ 2000

    #include<iostream> #include<cstdio> #define MAXN 10009 using namespace std; ]; int main( ...

  8. C# WINFORM 捕获全局异常

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Thr ...

  9. struct 理解 (需要经常理解)

    2014.3.11 分析offviewer时,有一些问题,很基础的,但是忘记了,发现问题那就快点搞定它 以下内容参考自百度百科: (2)struct 结构体有点忘记了,要复习一下  定义一个结构的一般 ...

  10. Java:内部类

    1.内部类的定义: 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. 2.内部类的分类: Java中的内部类共分为四种: 成员内部类member inner clas ...