最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile
这题是杭电多校2019第六场的题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638
题意:给你平面上n个点,每个点都有权值(有负权),让你计算一个矩阵可能的最大覆盖权值和;
思路:用 连续最大子段-线段树 枚举上界,按行一行行更新线段树中的点,每插完一行就更新答案(类似枚举上下界),时间复杂度:O( n^2*log(n) ) ;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
#include <map>
//#include<unordered_map> http://acm.hdu.edu.cn/showproblem.php?pid=6638
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(arr,arr+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(arr+1,arr+1+n);//prev_permutation
#define fo(arr,b,c) for(register int arr=b;arr<=c;++arr)
#define fr(arr,b,c) for(register int arr=b;arr>=c;--arr)
#define mem(arr,b) memset(arr,b,sizeof(arr))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
void swapp(int &arr,int &b);
double fabss(double arr);
int maxx(int arr,int b);
int minn(int arr,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int arr);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int); int b[N];
struct node_
{
int x,y;
long long v;
friend bool operator<(node_ a,node_ b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y>b.y;
}
}arr[N];
struct vnode
{
int x;
long long v;
};
//=================================================离散化;
int LSx(int n)
{
int m=;
for(int i=;i<=n;++i)
b[++m]=arr[i].x;
sort(b+,b++m);
m=unique(b+,b++m)-b-;
for(int i=;i<=n;++i)
arr[i].x=lower_bound(b+,b++m,arr[i].x)-b;
return m;
}
int LSy(int n)
{
int m=;
for(int i=;i<=n;++i)
b[++m]=arr[i].y;
sort(b+,b++m);
m=unique(b+,b++m)-b-;
for(int i=;i<=n;++i)
arr[i].y=lower_bound(b+,b++m,arr[i].y)-b;
return m;
}
//===========================================连续子段线段树;
struct node
{
long long Sum,Lsum,Rsum,ans;
}tr[N<<]; void up(int rt)
{
tr[rt].Sum=tr[ls].Sum+tr[rs].Sum;
tr[rt].ans=max(max(tr[ls].ans,tr[rs].ans),tr[ls].Rsum+tr[rs].Lsum);
tr[rt].Lsum=max(tr[ls].Lsum,tr[ls].Sum+tr[rs].Lsum);
tr[rt].Rsum=max(tr[rs].Rsum,tr[rs].Sum+tr[ls].Rsum);
}
void Build(int l,int r,int rt)
{
if(l==r)
{
tr[rt].Sum=tr[rt].Lsum=tr[rt].Rsum=tr[rt].ans=;
return;
}
int mid=(l+r)>>;
Build(l,mid,ls);
Build(mid+,r,rs);
up(rt);
}
void update_dot(int pos,long long v,int l,int r,int rt)
{
if(l==r)
{
tr[rt].Sum+=v;
tr[rt].Lsum=tr[rt].Rsum=tr[rt].ans=tr[rt].Sum;
return;
}
int mid=(l+r)>>;
if(pos<=mid)
update_dot(pos,v,l,mid,ls);
else
update_dot(pos,v,mid+,r,rs);
up(rt);
}
vector<vector<vnode> >v(N); int main()
{
int T;
sc("%d",&T);
while(T--)
{
int n;
sc("%d",&n);
for(int i=;i<=n;++i)
sc("%d%d%lld",&arr[i].x,&arr[i].y,&arr[i].v);
int L=LSx(n);
int H=LSy(n);
fo(i,,H)v[i].clear();
for(int i=;i<=n;++i)
v[arr[i].y].push_back({arr[i].x,arr[i].v});
long long ans=;
for(int i=H;i>=;--i)
{
Build(,L,);
for(int j=i;j>=;--j)
{
int sz=v[j].size();
for(int k=;k<sz;++k)
{
vnode t=v[j][k];
update_dot(t.x,t.v,,L,);
}
ans=max(ans,tr[].ans);//每增加一层就更新答案;
}
}
pr("%lld\n",ans);
}
return ;
} /**************************************************************************************/ int maxx(int arr,int b)
{
return arr>b?arr:b;
} void swapp(int &arr,int &b)
{
arr^=b^=arr^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int arr)
{
return arr>?arr:-arr;
} double fabss(double arr)
{
return arr>?arr:-arr;
} int minn(int arr,int b)
{
return arr<b?arr:b;
}
最大矩阵覆盖权值--(静态连续最大子段 (线段树) )-HDU(6638)Snowy Smile的更多相关文章
- HDU 6464 权值线段树 && HDU 6468 思维题
免费送气球 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 区间第k大问题 权值线段树 hdu 5249
先说下权值线段树的概念吧 权值平均树 就是指区间维护值为这个区间内点出现次数和的线段树 用这个加权线段树 解决第k大问题就很方便了 int query(int l,int r,int rt,int k ...
- Codeforces 527C Glass Carving (最长连续0变形+线段树)
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glas ...
- HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...
- POJ 3264:Balanced Lineup(区间最值查询ST表&线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 75294 Accepted: 344 ...
- LA 3938 动态最大连续和(线段树)
https://vjudge.net/problem/UVALive-3938 题意:给出一个长度为n的整数序列D,你的任务是对m个询问作出回答.对于询问(a,b),需要找到两个下标x和y,使得a≤x ...
- codedecision P1112 区间连续段 题解 线段树
题目描述:https://www.cnblogs.com/problems/p/P1112.html 题目链接:http://codedecision.com/problem/1112 线段树区间操作 ...
- 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...
- 权值线段树&&可持久化线段树&&主席树
权值线段树 顾名思义,就是以权值为下标建立的线段树. 现在让我们来考虑考虑上面那句话的产生的三个小问题: 1. 如果说权值作为下标了,那这颗线段树里存什么呢? ----- 这颗线段树中, 记录每个值出 ...
随机推荐
- 关于int main(int argc,char* argv[])详解
平时在VS的环境下,主函数总会看到这两个参数,今天突然很想知道这两个参数的原理以及作用,因此查了下资料.真心受教了. 下面的博文是在百度空间看一位大神的,原文链接:http://hi.baidu.co ...
- hbase基于hue的查询语法
hbase基于hue的查询语法 登录地址 https://hue-ui.xiaoniangao.cn 界面操作说明 进入hue中的hbase 进入表的查询界面 界面说明 查询语句 ,表示结束查询,可以 ...
- Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)
https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...
- $\LaTeX$数学公式大全6
$6\ Binary\ Operation/Relation\ Symbols$$\ast$ \ast$\star$ \star$\cdot$ \cdot$\circ$ \circ$\bullet$ ...
- [JZOJ5400]:Repulsed(贪心+树形DP)
题目描述 小$w$心里的火焰就要被熄灭了. 简便起见,假设小$w$的内心是一棵$n-1$条边,$n$个节点的树. 现在你要在每个节点里放一些个灭火器,每个节点可以放任意多个. 接下来每个节点都要被分配 ...
- redis远程连接命令
redis-cli -h 172.17.0.85 -p 6379 输入密码 auth "1234
- laravel where orwhere的写法
orWhere如果不用闭包的形式写很容易写成分开的查询条件 要写成一组查询条件需要这样闭包写(就相当于把这两个条件放在一个小括号里,是一组查询条件“(xxx or xxx)”): if (!empty ...
- koa 基础(二十六)数据库 与 art-template 模板 联动 --- 编辑数据、删除数据
1.通过 ObjectID 获取 _id 根目录/module/db.js /** * DB库 */ var MongoDB = require('mongodb'); var MongoClient ...
- 1、WebSphere Application Server的下载以及安装
最近在做农行相关的项目,我们的后台需要发布到农行WebSphere Application Server上,因此学习一下: 一.WebSphere 是什么? WebSphere 为 SOA (面向服务 ...
- LC 672. Bulb Switcher II
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...