链接:

https://www.patest.cn/contests/gplt/L3-017

题意:

给出直线上的N个顶点,(N-1)条边的限制值(每对相邻的顶点之间都有一条边),以及Q个区间(给出起始顶点编号以及终止顶点编号)。
每个区间都可以为该区间的所有边加上一个附加值,所有区间在某条边上所累加的附加值不能超过这条边的限制值。
问:所有区间的附加值总和最大是多少?

分析:

先按终点编号(将原来起点与终点编号较大的作为终点编号)从小到大排序,然后贪心选择:
顺序考虑每一个区间,用线段树快速找到该区间的最小值,然后更新该区间。
最后所有区间能找到的最小值总和就是答案。
至于为什么要按终点编号从小到大排序,而不是按起点编号从小到大排序,或按区间长度从小到大排序,看一下下面的测试数据就明白了。
不过这道题目说每个限制值是不超过2的31次方的非负整数,为什么偏要 long long 才能过呢?

代码:

 #include <cstdio>
#include <algorithm>
using namespace std; typedef long long int LLI;
const LLI INF = 0x3f3f3f3f3f3f3f3f;
const int UP = 1e5 + ; struct SEGMENT_TREE_NODE {
LLI v, m; //值,标记
} st[UP<<]; struct REGION {
int L, R;
bool operator < (const REGION& that) const {
return R < that.R;
}
} reg[UP]; void build(int root, int L, int R){
st[root].m = ;
if(L + == R){
scanf("%lld", &st[root].v);
return;
}
int M = L + (R - L) / ;
build(root*+, L, M);
build(root*+, M, R);
st[root].v = min(st[root*+].v, st[root*+].v);
} void push_down(int root){
if(!st[root].m) return;
st[root*+].v += st[root].m;
st[root*+].v += st[root].m;
st[root*+].m += st[root].m;
st[root*+].m += st[root].m;
st[root].m = ;
} LLI query(int root, int L, int R, int AL, int AR){
if(AR <= L || AL >= R) return INF;
if(AL <= L && R <= AR) return st[root].v;
push_down(root);
int M = L + (R - L) / ;
return min(query(root*+, L, M, AL, AR), query(root*+, M, R, AL, AR));
} void update(int root, int L, int R, int AL, int AR, LLI v){
if(AR <= L || AL >= R) return;
if(AL <= L && R <= AR){
st[root].v += v;
st[root].m += v;
return;
}
push_down(root);
int M = L + (R - L) / ;
update(root*+, L, M, AL, AR, v);
update(root*+, M, R, AL, AR, v);
st[root].v = min(st[root*+].v, st[root*+].v);
} int main(){
int n, q;
scanf("%d%d", &n, &q);
build(, , --n); for(int i = ; i < q; i++){
scanf("%d%d", &reg[i].L, &reg[i].R);
if(reg[i].L > reg[i].R) swap(reg[i].L, reg[i].R);
}
sort(reg, reg + q); LLI ans = ;
for(int i = ; i < q; i++){
LLI v = query(, , n, reg[i].L, reg[i].R);
ans += v;
if(v) update(, , n, reg[i].L, reg[i].R, -v);
}
printf("%lld\n", ans);
return ;
}

测试数据:

input1:

5 3
5 3 3 1
4 1
1 3
3 4

output1:

4

input2:

5 3
3 9 9 1
3 1
2 0
2 4

output2:

10

PAT-GPLT L3-017 森森快递(贪心 + 线段树)的更多相关文章

  1. BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库

    正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...

  2. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

  3. L3-2 森森快递 (30 分)(贪心+线段树/分块)

    题目链接:https://pintia.cn/problem-sets/1108203702759940096/problems/1108204121661857798 题目大意: 森森开了一家快递公 ...

  4. Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)

    题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...

  5. PAT天梯赛练习题 L3-002. 堆栈(线段树查询第K大值或主席树)

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...

  6. poj 2010 Moo University - Financial Aid (贪心+线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...

  7. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

  8. UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化

    题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...

  9. BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树

    题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...

随机推荐

  1. Hadoop实战之四~hadoop作业调度详解(2)

    这篇文章将接着上一篇wordcount的例子,抽象出最简单的过程,一探MapReduce的运算过程中,其系统调度到底是如何运作的. 情况一:数据和运算分开的情况 wordcount这个例子的是hado ...

  2. 流畅的python和cookbook学习笔记(九)

    1.减少可调用对象的参数个数,使用functools.partial冻结参数 使用functools.partial(),可以固定一个或者多个值,减少调用参数. >>> def sp ...

  3. JDK自带工具keytool生成ssl证书 和 HTTPS双向认证

    创建证书(第一步) keytool -genkey -alias "baidu" -keypass "123456" -keystore "D:/ba ...

  4. [翻译]Review——The Inner Workings Of Virtual DOM

    The Inner Workings Of Virtual DOM 虚拟DOM的内部工作机制 原文地址:https://medium.com/@rajaraodv/the-inner-workings ...

  5. mysql 查询近几天的结果 按每天的粒度查询

    ),DATE_FORMAT(FROM_UNIXTIME(createtime), '%Y-%m-%d') as time from bskuser group by time

  6. SQL Server UDF to pad a string

    http://www.mssqltips.com/sqlservertip/1738/sql-server-udf-to-pad-a-string/ declare @l varchar(50) se ...

  7. org.springframework.beans.factory.NoSuchBeanDefinitionException

    1. 问题描述 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxx ...

  8. 归并排序算法Matlab实现

    Matlab一段时间不用发现有些生疏了,就用归并排序来练手吧.代码没啥说的,百度有很多.写篇博客,主要是记下matlab语法,以后备查.   测试代码 srcData = [1,3,2,4,6,5,8 ...

  9. Windows核心编程(第5版)----关闭内核对象

    无论怎样创建内核对象,都要向系统指明将通过调用 CloseHandle 来结束对该对象的操作: BOOL CloseHandle(HANDLE hobj); 该函数首先检查调用进程的句柄表,以确保传递 ...

  10. Python爬虫教程-31-创建 Scrapy 爬虫框架项目

    本篇是介绍在 Anaconda 环境下,创建 Scrapy 爬虫框架项目的步骤,且介绍比较详细 Python爬虫教程-31-创建 Scrapy 爬虫框架项目 首先说一下,本篇是在 Anaconda 环 ...