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\),在判断新加入的点与现在有的点是否都 ...
随机推荐
- SecureCRT-登录unix/linux服务器主机的软件
百度百科说辞: SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. SecureCRT支持SSH,同时支持Te ...
- 图片懒加载--lazyload.js的用法
这几天公司的项目已经完成的差不多了,只剩下各种优化问题.今天着重于图片加载的优化.当一个页面需要下拉很长而且又有过多的图片要加载时,就会发生很多http请求,就会拉慢网页加载速度,用户体验不友好.怎么 ...
- shoshana-技术文集
20190422 全球最厉害的 14 位程序员,请收下我的膝 20190423 观察者模式(Observer)和发布(Publish/订阅模式(Subscribe) 2019042 ...
- Centos7 yum方式安装MySQL
1.下载安装源 wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 2.yum方式安装 yu ...
- 长乐国庆集训Day1
T1 统计数字 题目 [题目描述] 设 S(N ) 表示 N 的各位数字之和,如 S(484) = 4+8+4 = 16, S(22) = 2+2 = 4. 如果一个正整数满足 S(x*x) = S( ...
- Python的正则表达式和爬虫
1.常用元字符 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ 匹配字符串的结束 2.常用限 ...
- Go 基本数据类型
Go基础语法 package main import "fmt" func main(){ fmt.Println("Hello world") } 注意点: ...
- Linux环境下进程的CPU占用率
阿里云服务器网站:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=qqwovx6h 文字来源:http://www.s ...
- Calico网络模型
由于两台物理机的容器网段不同,我们完全可以将两台物理机配置成为路由器,并按照容器的网段配置路由表. 在物理机A中,我们可以这样配置:要想访问网段172.17.9.0/24,下一跳是192.168.10 ...
- 单点logi,n
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...