POJ2482 Stars in Your Window 和 test20180919 区间最大值
Stars in Your Window
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15036 | Accepted: 4061 |
Description
These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently.
Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert.
Farewell, my princess!
If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.
Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.
Input
There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.
Output
Sample Input
3 5 4
1 2 3
2 3 2
6 3 1
3 5 4
1 2 3
2 3 2
5 3 1
Sample Output
5
6
Source
分析
不想吐槽出题人了。
考虑该矩形右上角的坐标,每个星星相当于给一个矩形的权值加了c,那么就是求最大的区域的点权和。
由于边界上的星星不算,所以把每个星星像左边和下面各移动0.5,那么覆盖这个星星的矩形坐标范围为(x,y)到(x+w-1,y+h-1)这个矩形。
时间复杂度\(O(n \log n)\)
#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=1e4+1;
int n;
struct P{
unsigned int x,y,z;
int c;
bool operator<(co P&w)co{
return x<w.x||(x==w.x&&c<w.c);
}
}a[N*2];
unsigned w,h,b[N*2];
struct T{
int l,r,ans,add;
}t[N*8];
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
t[p].ans=t[p].add=0;
if(l==r) return;
int mid=l+r>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
void spread(int p){
t[p<<1].add+=t[p].add,t[p<<1].ans+=t[p].add;
t[p<<1|1].add+=t[p].add,t[p<<1|1].ans+=t[p].add;
t[p].add=0;
}
void change(int p,int l,int r,int c){
if(l<=t[p].l&&t[p].r<=r) return t[p].add+=c,t[p].ans+=c,void();
if(t[p].add) spread(p);
int mid=t[p].l+t[p].r>>1;
if(l<=mid) change(p<<1,l,r,c);
if(r>mid) change(p<<1|1,l,r,c);
t[p].ans=max(t[p<<1].ans,t[p<<1|1].ans);
}
void Stars_in_Your_Window(){
for(int i=1;i<=n;++i){
int k=i<<1;
read(a[k-1].x),read(a[k-1].y),read(a[k-1].c);
a[k].x=a[k-1].x+w;
b[k-1]=a[k].y=a[k-1].y;
b[k]=a[k].z=a[k-1].z=a[k].y+h-1;
a[k].c=-a[k-1].c;
}
n<<=1;
sort(b+1,b+n+1);
int m=unique(b+1,b+n+1)-(b+1);
for(int i=1;i<=n;++i){
a[i].y=lower_bound(b+1,b+m+1,a[i].y)-b;
a[i].z=lower_bound(b+1,b+m+1,a[i].z)-b;
}
sort(a+1,a+n+1);
build(1,1,m);
int ans=0;
for(int i=1;i<=n;++i){
change(1,a[i].y,a[i].z,a[i].c);
ans=max(ans,t[1].ans);
}
printf("%d\n",ans);
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
while(~scanf("%d%d%d",&n,&w,&h)) Stars_in_Your_Window();
return 0;
}
区间最大值
分析
我们将所有修改操作的左右端点都拿出来混合着排序。
然后扫描线一样扫描每个端点,维护一个堆储存当前最大值,然后就可以把这些修改操作分成O(m) 个不相交的区间,各自贡献独立。
复杂度为\(O(m \log m)\)。
浅谈扫描线
把每个区间的l,r+1的左闭右开区间端点混合排序后,其实在扫描左闭右开的区间过程中主要是以下4种情况:
- 前一个是l1,这一个是l2,那么其实要加的是[l1,l2-1]这个区间,而l2-1-l1+1=l2-l1
- 前一个是l1,这一个是r2+1,那么要加的是[l1,r2]这个区间,而r2-l1+1=r2+1-l1
- 前一个是r1+1,这一个是l2,那么要加的是[r1+1,l2-1]这个区间,而l2-1-r1-1+1=l2-r1-1
- 前一个是r1+1,这一个是r2+1,那么要加的是[r1+1,r2]这个区间,而r2+1-r1-1=r2-r1-1+1
所以可以直接对区间端点执行减操作,统计答案。
左闭右开区间真好用。
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
typedef pair<ll,ll> pii;
const int INF=0x7fffffff;
const int MAXN=1e5+7,mod=998244353;
ll maxv[MAXN];
pii A[MAXN<<1];
int sz;
multiset<ll>S;
ll ans;
int pow2(ll x)
{
x%=mod;
return (ll)x*x%mod;
}
void addedge(ll x,ll y)
{
if(S.empty())
return;
(ans += (ll)( y-x ) % mod * pow2( *S.rbegin() ) % mod ) %= mod;
}
int main()
{
freopen("segment.in","r",stdin);
freopen("segment.out","w",stdout);
ll n;
int m;
read(n);read(m);
for(int i=1;i<=m;++i)
{
ll l,r;
read(l);read(r);read(maxv[i]);
A[++sz]=pii(l,i);
A[++sz]=pii(r+1,-i);
}
sort(A+1,A+sz+1);
for(int i=1;i<=sz;++i)
{
if(i > 1 && A[i-1].first < A[i].first)
addedge(A[i-1].first,A[i].first);
if(A[i].second > 0)
S.insert(maxv[A[i].second]);
else
S.erase(maxv[-A[i].second]);
}
printf("%lld",ans);
// fclose(stdin);
// fclose(stdout);
return 0;
}
POJ2482 Stars in Your Window 和 test20180919 区间最大值的更多相关文章
- POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...
- Poj2482 Stars in Your Window(扫描线)
题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...
- POJ2482 Stars in Your Window 题解
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- poj2482 Stars in Your Window
此题可用线段树或静态二叉树来做. 考虑用线段树: 很容易想到先限定矩形横轴范围再考虑在此纵轴上矩形内物品总价值的最大值. 那么枚举矩形横轴的复杂度是O(n)的,考虑如何快速获取纵轴上的最大值. 我们不 ...
- test20180919 区间最大值
题意 分析 我们将所有修改操作的左右端点都拿出来混合着排序. 然后扫描线一样扫描每个端点,维护一个堆储存当前最大值,然后就可以把这些修改操作分成O(m) 个不相交的区间,各自贡献独立. 复杂度为\(O ...
- 【POJ2482】Stars in Your Window
[POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- 【POJ2482】【线段树】Stars in Your Window
Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...
随机推荐
- PHP比较两个版本的方法
PHP比较两个版本的方法 写app接口经常要用到 PHP接口的版本和app里面的版本做比较 如果app版本低于接口版本 那么就要提示他升级 <pre><?php/** * 版本处理类 ...
- Git使用总结(三):协同开发常见冲突
1.不同人修改了不同的文件 a.账户A,账户B分别从远端拉取了相同分支 b.账户A修改了main.cpp文件后提交到远端,账户B修改fun.cpp文件提交远端时会报如下错误 ...
- 面向对象分析与设计—OOA部分
第二部分 面向对象分析 2.1 面向对象分析(OOA)的定义? OOA——面向对象的分析,就是运用面向对象方法进行系统分析,对问题域(问题所涉及的范围)和系统责任(所开发的系统应具备的职能)进行分析与 ...
- 关于python的一次性能调优过程
问题 这两天在公司帮老大写一个程序功能,要求抓取从elasticsearch和kibana服务器上返回的数据,统计所有hits的数据字段ret_code为0的hit,并计算其占有率等一些功能. 功能倒 ...
- 04 Python的继承、方法重写、super()类、父类私密属性的调用
继承 A类继承B类,A即可获得B类的全部公共属性和方法(包括内置属性和方法).格式如:class A(B): class Animal: def sleep(self): print("zZ ...
- PAT甲级题分类汇编——树
本文为PAT甲级分类汇编系列文章. AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想 ...
- 在被调用函数中获取资源及C++中的引用
#include <iostream> using namespace std; struct Teacher{ char name[64]; int age; }; //在被调用函数 获 ...
- python检测当前端口是否使用
基于python检测端口是否在使用 - 缘起花渊 - 博客园https://www.cnblogs.com/yqmcu/p/9804002.html def net_is_used(port,ip=' ...
- ubuntu配置fastdfs+Nginx
全程参考主线来自:https://blog.csdn.net/xiaoxie762457/article/details/80690841(文中图片失效不造成影响) Nginx安装主要参考:https ...
- 怎样获取当前页面框架的数量(即iframe和frame的数量)
需要使用window.length, 或者window.frames.length; 如果页面中不包含frame和iframe元素, 则返回0; window.length === window.fr ...