2019年牛客多校第一场 I题Points Division 线段树+DP
题目链接
题意
给你\(n\)个点,每个点的坐标为\((x_i,y_i)\),有两个权值\(a_i,b_i\)。
现在要你将它分成\(\mathbb{A},\mathbb{B}\)两部分,使得在满足“\(\mathbb{A}\)的点不能落在在\(\mathbb{B}\)的点的右下方”的条件下\(\sum\limits_{i\in\mathbb{A}}a_i+\sum\limits_{j\in\mathbb{B}}b_j\)最大。
思路
这篇博客讲得很详细,大家可以看这位大佬的昂~
代码实现如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int n;
vector<int> vec;
struct Point {
int x, y, a, b;
bool operator < (const Point& pp) const {
return x == pp.x ? y > pp.y : x < pp.x;
}
}point[maxn];
struct node {
int l, r;
LL mx, lazy;
}segtree[maxn<<2];
void push_up(int rt) {
segtree[rt].mx = max(segtree[lson].mx, segtree[rson].mx);
}
void push_down(int rt) {
LL x = segtree[rt].lazy;
segtree[rt].lazy = 0;
segtree[lson].lazy += x;
segtree[rson].lazy += x;
segtree[lson].mx += x;
segtree[rson].mx += x;
}
void build(int rt, int l, int r) {
segtree[rt].l = l, segtree[rt].r = r;
segtree[rt].mx = segtree[rt].lazy = 0;
if(l == r) return;
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
}
void update1(int rt, int pos, LL val) {
if(segtree[rt].l == segtree[rt].r) {
segtree[rt].mx = val;
return;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(pos <= mid) update1(lson, pos, val);
else update1(rson, pos, val);
push_up(rt);
}
void update2(int rt, int l, int r, LL val) {
if(segtree[rt].l == l && segtree[rt].r == r) {
segtree[rt].mx += val;
segtree[rt].lazy += val;
return;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) update2(lson, l, r, val);
else if(l > mid) update2(rson, l, r, val);
else {
update2(lson, l, mid, val);
update2(rson, mid + 1, r, val);
}
push_up(rt);
}
LL query(int rt, int l, int r) {
if(segtree[rt].l == l && segtree[rt].r == r) {
return segtree[rt].mx;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) return query(lson, l, r);
else if(l > mid) return query(rson, l, r);
else return max(query(lson, l, mid), query(rson, mid + 1, r));
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while(~scanf("%d", &n)) {
vec.clear();
for(int i = 1; i <= n; ++i) {
scanf("%d%d%d%d", &point[i].x, &point[i].y, &point[i].a, &point[i].b);
vec.push_back(point[i].y);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
sort(point + 1, point + n + 1);
for(int i = 1; i <= n; ++i) {
point[i].y = lower_bound(vec.begin(), vec.end(), point[i].y) - vec.begin() + 1;
}
int sz = vec.size();
build(1, 0, sz + 1);
for(int i = 1; i <= n; ++i) {
LL num = query(1, 0, point[i].y);
update1(1, point[i].y, num + point[i].b);
update2(1, 0, point[i].y - 1, point[i].a);
update2(1, point[i].y + 1, sz + 1, point[i].b);
}
printf("%lld\n", segtree[1].mx);
}
return 0;
}
2019年牛客多校第一场 I题Points Division 线段树+DP的更多相关文章
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 2019年牛客多校第一场 H题XOR 线性基
题目链接 传送门 题意 求\(n\)个数中子集内所有数异或为\(0\)的子集大小之和. 思路 对于子集大小我们不好维护,因此我们可以转换思路变成求每个数的贡献. 首先我们将所有数的线性基的基底\(b\ ...
- 2019年牛客多校第一场 B题 Integration 数学
题目链接 传送门 思路 首先我们对\(\int_{0}^{\infty}\frac{1}{\prod\limits_{i=1}^{n}(a_i^2+x^2)}dx\)进行裂项相消: \[ \begin ...
- 2019年牛客多校第一场 C题Euclidean Distance 暴力+数学
题目链接 传送门 题意 给你\(n\)个数\(a_i\),要你在满足下面条件下使得\(\sum\limits_{i=1}^{n}(a_i-p_i)^2\)最小(题目给的\(m\)只是为了将\(a_i\ ...
- 2019年牛客多校第一场 E题 ABBA DP
题目链接 传送门 思路 首先我们知道\('A'\)在放了\(n\)个位置里面是没有约束的,\('B'\)在放了\(m\)个位置里面也是没有约束的,其他情况见下面情况讨论. \(dp[i][j]\)表示 ...
- Cutting Bamboos(2019年牛客多校第九场H题+二分+主席树)
题目链接 传送门 题意 有\(n\)棵竹子,然后有\(q\)次操作,每次操作给你\(l,r,x,y\),表示对\([l,r]\)区间的竹子砍\(y\)次,每次砍伐的长度和相等(自己定砍伐的高度\(le ...
- 2019年牛客多校第二场 F题Partition problem 爆搜
题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...
- MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)
题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...
- Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 找第\(k\)小团. 思路 用\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都 ...
随机推荐
- DevExpress XtraReport - 动态加载报表布局模板
XtraReport的报表模板文件是.repx,下面的代码演示动态加载报表布局模板. XtraReport mReport = new XtraReport(); mReport.LoadLayout ...
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [源码]Python调用C# DLL例子(Python与.Net交互)
K8Cscan C# DLL例子代码 namespace CscanDLL { public class scan { public static string run(string ip) { if ...
- QQ联合登录(基于Oauth2.0协议)
1. 获取授权码Authorization Code https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id= ...
- java知识精要(一)
一.java数组 (疯狂java讲义 第4.5 ~ 4.6章节) 1) 声明形式: type[] arrayName; 推荐方式 type arrayName[]; 2) 初始化: 方式一: type ...
- java注解注意点
注意:以后工作中代码中 不允许出现警告 自定义注解 1:自定义注解并没有发挥它的作用,而Eclipse自带的注解通过反射另外有一套代码,可以发挥它的作用,例如:跟踪代码...... 2:如果自定义的代 ...
- .net Dapper 实践系列(3) ---数据显示(Layui+Ajax+Dapper+MySQL)
目录 写在前面 产生问题 解决方案 写在前面 上一小节,我们使用Dapper 里事务实现了一对多关系的添加.这一小节,主要记录如何使用Dapper 实现多表的查询显示. 产生问题 在mvc控制器中查询 ...
- C# vb .net实现淡色效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的淡色效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...
- hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法
1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...
- DIV中的文字垂直并且水平居中的CSS
.MsgPopup { height: 100px; line-height: 100px; text-align: center;}