状态

题目描述

There are n wooden rods vertically placed over a horizontal line. The rods are numbered 1 through n from left to right. Each rod i (1 ⩽ i ⩽ n) is placed at position xi and has a height hi .

A termite wants to eat all the rods one by one. It starts eating from an arbitrary rod s (1 ⩽ s ⩽ n). Then, after eating a rod i, the termite selects the next rod to eat based on the following method. Among the remaining rods j, the one with maximum hj - |xi-xj| is selected. If there are ties, the one with minimum |xi-xj| is selected. 
If there are still ties, the left-most rod is selected.
Your task is to calculate the total (horizontal) distance traveled by the termite to eat all the rods.

输入

The first line of the input contains two space-separated integers n, the number of rods, and s, the starting rod number (1 ⩽ s ⩽ n ⩽ 100 000). The rods are described in the next n lines. On the line 1 + i (1 ⩽ i ⩽ n), the i-th rod is specified with two space-separated integers xi (|xi| ⩽ 109 ) and hi (1 ⩽ hi ⩽ 109 ). Additionally, for each i (1 ⩽ i ⩽ n-1), xi < xi+1 .

输出

You should print a single integer denoting the total distance traveled by the termite.

样例输入 Copy

5 3
1 3
4 8
8 2
10 4
11 1

样例输出 Copy

17

题目大意:

给出一个起始位置,从起始位置开始,每次会选择一个位置,该位置满足题目中给的条件,问按照要求跳完所有点后,横坐标移动的距离是多少。

题目思路:

首先遇到绝对值就先去掉绝对值

对于一个位置i来说,对于任意的j有:

j>i : q[j].h - q[j].x + q[i].x

j<i: q[j].h + q[j].x - q[i].x

所以只需要维护一下两边的最大值即可确定到选哪个点

题目又要求,相同时选择离i最近的,也就是说左边选靠右的,右边选靠左的

此时可以直接用线段树push函数

对于每个点跳完之后的标记,可以将这个点权值设为-无穷

所以就会有 单点修改 + 区间查询

Code:

/*** keep hungry and calm CoolGuang!***/
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pp;
const ll INF=1e17;
const int Maxn=2e7+10;
const int maxn =5e5+10;
const int mod=1e9+9;
const int Mod = 1e9+7;
///const double eps=1e-10;
inline bool read(ll &num)
{char in;bool IsN=false;in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
ll ma[maxn],mb[maxn];///a - > 左 ,b -> 右
int posa[maxn],posb[maxn];
void push(int k){if(ma[k<<1] <= ma[k<<1|1]){ma[k] = ma[k<<1|1];posa[k] = posa[k<<1|1];}else{ma[k] = ma[k<<1];posa[k] = posa[k<<1];}if(mb[k<<1] >= mb[k<<1|1]){mb[k] = mb[k<<1];posb[k] = posb[k<<1];}else{mb[k] = mb[k<<1|1];posb[k] = posb[k<<1|1];}
}
void build(int k,int l,int r){ma[k] = mb[k] = -INF;posa[k] = posb[k] = -1;if(l == r) return ;int mid = (l+r)/2;build(k<<1,l,mid);build(k<<1|1,mid+1,r);push(k);
}
void Modify(int k,int l,int r,int pos,ll w1,ll w2){if(l == r){ma[k] = w1;mb[k] = w2;if(w1 == -INF) posa[k] = posb[k] = -1;else posa[k] = posb[k] = l;return;}int mid = (l+r)/2;if(pos <= mid) Modify(k<<1,l,mid,pos,w1,w2);else Modify(k<<1|1,mid+1,r,pos,w1,w2);push(k);
}
void Query_a(int k,int x,int y,int l,int r,ll &ans,int &pos){if(x<=l&&y>=r){if(ma[k] == -INF) return;if(ans<ma[k]){ans = ma[k];pos = posa[k];}else if(ans == ma[k]) pos = max(pos,posa[k]);return;}int mid = (l+r)/2;if(x<=mid) Query_a(k<<1,x,y,l,mid,ans,pos);if(y>mid) Query_a(k<<1|1,x,y,mid+1,r,ans,pos);push(k);
}
void Query_b(int k,int x,int y,int l,int r,ll &ans,int &pos){if(x<=l&&y>=r){if(mb[k] == -INF) return;if(ans<mb[k]){ans = mb[k];pos = posb[k];}else if(ans == mb[k])if(~posb[k])pos = min(pos,posb[k]);return;}int mid = (l+r)/2;if(x<=mid) Query_b(k<<1,x,y,l,mid,ans,pos);if(y>mid) Query_b(k<<1|1,x,y,mid+1,r,ans,pos);push(k);
}
struct node{ll x,y;
}q[maxn];
int main()
{read(n);read(m);build(1,1,n);for(int i=1;i<=n;i++){read(q[i].x);read(q[i].y);Modify(1,1,n,i,q[i].x+q[i].y,q[i].y-q[i].x);}ll ans = 0, s = 1;while(1){ll tempm = m;Modify(1,1,n,m,-INF,-INF);ll ansa = -INF,ansb = -INF;int apos = -1,bpos = -1;if(m>1)Query_a(1,1,m-1,1,n,ansa,apos);if(m<n)Query_b(1,m+1,n,1,n,ansb,bpos);if(apos == -1 && bpos == -1) break;else if(apos == -1 && ~bpos) m = bpos;else if(~apos && bpos == -1) m = apos;else{if(ansa-q[m].x>ansb+q[m].x) m = apos;else if(ansa-q[m].x<ansb+q[m].x) m = bpos;else{if(q[m].x-q[apos].x <= q[bpos].x - q[m].x) m = apos;else m = bpos;}}ans += abs(q[m].x-q[tempm].x);}printf("%lld\n",ans);return 0;
}
/**
4 3
1 3
2 10
3 11
4 10
**/

【upc】2020年秋季组队训练赛第十二场J Greedy Termite | 删点线段树相关推荐

  1. 石油大--2020年秋季组队训练赛第十二场----J、Greedy Termite(线段树)

    题面: 题意: 给定正整数 nnn 和起始位置 sss. nnn 表示有 nnn 个杆子,每个杆子由属性 (xi,hi)(x_i,h_i)(xi​,hi​) 构成,表示在 xix_ixi​ 处有一根高 ...

  2. UPC 2020年秋季组队训练赛第十四场

    问题 A: Too Expensive to Buy a House 时间限制: 1 Sec 内存限制: 128 MB 题目描述 WNJXYK and DIDIDI are good friends ...

  3. 石油大--2020年秋季组队训练赛第十二场---- L、The Assembly Code(模拟)

    题面: 题意: 就是题目有点长,写起来直接写就好啦. 有一个长度为 232,0≤i<2322^{32},0\le i <2^{32}232,0≤i<232 数组 MMM , MiM_ ...

  4. 【DFS反向建图记忆化搜索】UPC Contest2592 - 2020年秋季组队训练赛第十四场 问题 D: Mysterious Treasure

    问题 D: Mysterious Treasure 时间限制: 1 Sec 内存限制: 128 MB 题目描述 WNJXYK and DIDIDI is playing a game. DIDIDI ...

  5. 【upc】2020年秋季组队训练赛第十四场 Get Strong | 折半搜索、二分

    题目描述 WNJXYK and DIDIDI are friends. They are thinking about getting strong all the time. They are pl ...

  6. 石油大 2019年第二阶段我要变强个人训练赛第十八场 Problem N 扶桑号战列舰(线段树+区间更新+区间查询)

    链接:http://icpc.upc.edu.cn/problem.php?cid=1803&pid=13 题意:给出一个n,接下来一行给出n个数.才开始所有数为0,每次操作可以选一个区间[l ...

  7. 石油大--2020年秋季组队训练赛第十三场----B、Bouldering(最短路)

    题面: 题意: 给定一个 h∗wh*wh∗w 点阵,其中某一些点是可以走的. 这些点都有一个权值,表示如果经过当前点,则会花费的力气. 给定一个 rrr,你只能从当前点到达与你欧几里得距离不超过 rr ...

  8. 石油大--Contest2022 - 2020年秋季组队训练赛第二场--17100 Problem D、Find String in a Grid (AC自动机)

    题面: 题意: 给定一个 R∗CR*CR∗C 的字符矩阵,由大写字母构成. 有 qqq 个询问,每次给定一个由大写字母构成的字符串,问这个字符串在这个字符矩阵中出现了多少次. 某个字符串在矩阵中出现定 ...

  9. 石油大--2020年秋季组队训练赛第十三场---- C、Colourful Chameleons(思维)

    题面: 题意: 有 nnn 种颜色的变色龙,其中第 iii 种颜色的变色龙有 ai,(ai>=n−1)a_i ,(a_i>=n-1)ai​,(ai​>=n−1) 只. 我每次可以选择 ...

最新文章

  1. 突发灵感,看到某网站的搞笑图片挺多,做了一个小java,扫描抠了一些
  2. CS231n:卷积神经网络
  3. 新建Silverlight文件的方法
  4. CenOS 配置C/C++语言
  5. C#------如何判断输入的是否为纯数字
  6. .Net Core 1.1打包发布到Linux
  7. mysql 5.7 严格模式_mysql 5.7中严格模式的问题
  8. C# COM Object for Use In JavaScript / HTML, Including Event Handling(转载)
  9. linux环境下redis安装
  10. SpringBoot如何实现自动配置
  11. (转)为什么用ls和du显示出来的文件大小有差别?
  12. Quartz——CronTrigger触发器
  13. 竞品分析文档撰写总结
  14. linux yml文件格式,YML 文件扩展名: 它是什么以及如何打开它?
  15. 看3D打印技术如何影响未来
  16. 对接百度api之银行卡识别
  17. C、C++、JAVA
  18. HDFS的常用操作--hdfs下的文件操作常用命令总结
  19. 医疗汇报医学演示PPT模板
  20. 个人简介 - HTML/CSS简单页面实例

热门文章

  1. 软件工程会议、期刊、学术组织等
  2. iOS学习笔记 --微博登录 redirect_uri_mismatch 错误
  3. java当前月份减一个月_Java对日期Date类进行加减运算、年份加减月份加减、时间差等等...
  4. linux库--静态库、动态库
  5. python图像分类示例_python +keras实现图像分类(入门级例子讲解)
  6. 数据结构上机作业4. n阶魔方(n为奇数)
  7. 集成聚类之EAC方法
  8. 新威电池测试仪软件打不开,新威电池测试仪软件使用教程
  9. flex+FluorineFx ASP.NET 视频拍照
  10. 做知识付费怎么推广课程?