Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并
Summer Wars
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88994#problem/J
Description
The mafia is finally resting after a year of hard work and not so much money. Mafianca, the little mascot of the gang wanted to go to the beach.
Little did they know that the rival gang had prepared a trap to the little girl and her rubber duck. They know that Mafianca, despite loving the beach, hates water. So they set up some water sprayers to ruin her day. The beach can be seen as a plane. The sprayers are located at different (x, y) points along the shore. Each sprayer is so strong that it creates an infinite line of water, reaching every point with x-coordinate equal to the sprayer.
Mafianca's bodyguards figured this plan out. They didn't have the time to destroy the sprayers, so they decided to do something else. Lying around on the beach was a piece of modern art: a bunch of walls, represented by horizontal line segments, painted by some futuristic anonymous artist. Those walls can be moved, but they can only be moved together and horizontally.
The bodyguards decided that they would try to move this piece of art in order to block the most sprayers they can. A sprayer is blocked if there's at least one wall in both of its sides.
How many sprayers can they block?
Input
Input begins with the number of sprayers, n (1 ≤ n ≤ 1000) and the number of walls in the modern art, m (1 ≤ m ≤ 1000).
Follows n lines, each with two intergers, y and x (0 ≤ y, x ≤ 106), indicating that there's a sprayer at position x, y.
Then follows m lines, each with three integers: y, xbegin and xend (0 ≤ y ≤ 106) and (0 ≤ xbegin < xend ≤ 106) denoting a wall in the modern art has y-coordinate y, begins at xbegin and ends at xend. No wall will have a y-coordinate shared with a sprayer and the walls with same y will not intersect.
Output
Print the number of sprayers that can be blocked.
Sample Input
1 1
2 2
3 1 3
Sample Output
0
HINT
题意
给你一些点,和一些平行于x轴的线段,然后你可以移动线段,但是你只能水平移动,并且必须一起移动所有线段
你需要挡住最多的点数
挡住点,要求这个点的上面至少有一个线段,下面也要有一个线段
题解:
我的做法比较繁琐……
先O(nm)求出每个线段要覆盖这个点需要平移多少,然后我们可以得到2m个区间,然后我们上下区间再求交集,然后再跑一遍线段树就好了……
想法比较简单,但是写起来感觉还是比较麻烦吧= =
我后面防止负数的区间,我还离散化了一发
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* */
//**************************************************************************************
int n,q,a[];
struct data{
int l,r;
long long mx;
int tag;
}tr[];
void build(int k,int s,int t)
{
tr[k].l=s;tr[k].r=t;
if(s==t){tr[k].mx=a[s];return;}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
}
void pushdown(int k)
{
tr[k<<].tag+=tr[k].tag;
tr[k<<|].tag+=tr[k].tag;
tr[k<<].mx+=tr[k].tag;
tr[k<<|].mx+=tr[k].tag;
tr[k].tag=;
}
void update(int k,int a,int b,int x)
{
int l=tr[k].l,r=tr[k].r;
if(a==l&&r==b)
{
tr[k].tag+=x;
tr[k].mx+=x;
return;
}
if(tr[k].tag)pushdown(k);
int mid=(l+r)>>;
if(b<=mid)update(k<<,a,b,x);
else if(a>mid)update(k<<|,a,b,x);
else
{
update(k<<,a,mid,x);
update(k<<|,mid+,b,x);
}
tr[k].mx=max(tr[k<<].mx,tr[k<<|].mx);
}
long long ask(int k,int a,int b)
{
int l=tr[k].l,r=tr[k].r;
if(a==l&&b==r){return tr[k].mx;}
if(tr[k].tag)pushdown(k);
int mid=(l+r)>>;
if(b<=mid)return ask(k<<,a,b);
else if(a>mid)return ask(k<<|,a,b);
else return max(ask(k<<,a,mid),ask(k<<|,mid+,b));
} struct node
{
int x,y;
};
node aa[];
struct line
{
int y,x1,x2;
};
bool cmp(node aaa,node bbb)
{
if(aaa.x==bbb.x)
return aaa.y<bbb.y;
return aaa.x<bbb.x;
}
line bb[];
vector<node> Q1[];
vector<node> Q2[];
vector<node> T;
map<int,int> H;
vector<int> kkkk;
vector<node> TT;
int main()
{
int N,M;
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++)
scanf("%d%d",&aa[i].y,&aa[i].x);
for(int i=;i<=M;i++)
scanf("%d%d%d",&bb[i].y,&bb[i].x1,&bb[i].x2);
for(int i=;i<=N;i++)
{
for(int j=;j<=M;j++)
{
if(aa[i].y>bb[j].y)
Q1[i].push_back((node){aa[i].x-bb[j].x2,aa[i].x-bb[j].x1});
else
Q2[i].push_back((node){aa[i].x-bb[j].x2,aa[i].x-bb[j].x1});
}
}
for(int i=;i<=N;i++)
sort(Q1[i].begin(),Q1[i].end(),cmp),sort(Q2[i].begin(),Q2[i].end(),cmp); for(int i=;i<=N;i++)
{
int k=;
int tmp=-inf;
TT.clear();
if(Q1[i].size()==||Q2[i].size()==)
continue;
int j=;
while(j<Q1[i].size())
{
if(k>=Q2[i].size())
break;
tmp = max(tmp,Q1[i][j].x);
if(tmp>Q1[i][j].y)
{
j++;
continue;
}
while(k<Q2[i].size()&&Q2[i][k].y<tmp)
k++;
if(k>=Q2[i].size())
break;
int l = max(tmp,Q2[i][k].x);
int r = min(Q1[i][j].y,Q2[i][k].y);
if(Q1[i][j].y>Q2[i][k].y)
k++;
else
j++;
if(r<l)
continue;
tmp = max(l,r);
TT.push_back((node){l,r});
}
if(TT.size()==)
continue;
sort(TT.begin(),TT.end(),cmp);
int l=TT[].x,r=TT[].y;
for(int j=;j<TT.size();j++)
{
if(TT[j].x==r)
r=TT[j].y;
else
{
T.push_back((node){l,r});
l = TT[j].x;
r = TT[j].y;
}
}
T.push_back((node){l,r});
} for(int i=;i<T.size();i++)
kkkk.push_back(T[i].x),kkkk.push_back(T[i].y);
sort(kkkk.begin(),kkkk.end());
kkkk.erase(unique(kkkk.begin(),kkkk.end()),kkkk.end());
for(int i=;i<kkkk.size();i++)
H[kkkk[i]]=i+; build(,,kkkk.size()+);
for(int i=;i<T.size();i++)
update(,H[T[i].x],H[T[i].y],);
printf("%d\n",ask(,,kkkk.size()+));
}
Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并的更多相关文章
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- codeforces 482B. Interesting Array【线段树区间更新】
题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 643G - Choosing Ads(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(p>50\) 的时候怎么处理,也就是求一个区间的绝对众数.我们知道众数这个东西是不能用线段树直接维护的,因为对于区间 ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
随机推荐
- Android 开发框架介绍
一.概述 现android开发有很多开发框架使用,做App不一定用到框架,但好框架的思想也是值得学习.选择合适的开发框架可提供实用功能,简化项目开发提升效率. 二.Afinal框架 简介 Afinal ...
- 《C++ primer》--第12章
习题12.7 什么是封装?为什么封装是有用的? 解答: 封装是一种将低层次的元素组合起来形成新的.高层次实体的技术.例如,函数是封装的一种形式:函数所执行的细节行为被封装在函数本身这个更大的实体中:类 ...
- IBM笔试题(_与equals的区别)
http://topic.csdn.net/t/20050325/08/3879427.html 题目:Integer i = new Integer(42) Long l ...
- A Spy in the Metro
题意: n个车站,已知到达相邻车站的时间,有m1辆车从1站出发已知发车时间,有m2辆车从n站出发已知发车时间,求从1到达n所需等车的总时间最小. 分析: 有三种情况,在原地等,乘左到右的车,乘右到左的 ...
- Period(KMP,循环节问题)
题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...
- HDU5779 Tower Defence (BestCoder Round #85 D) 计数dp
分析(官方题解): 一点感想:(这个题是看题解并不是特别会转移,当然写完之后看起来题解说得很清晰,主要是人太弱 这个题是参考faebdc神的代码写的,说句题外话,很荣幸高中和faebdc巨一个省,虽然 ...
- 【LR】录制测试脚本中的基本菜单
学习来源: MBoo,小强老师性能测试及Loadrunner培训 ——录制测试脚本: 1.Vuser -> run-time settings ->General Run Logic : ...
- ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决
一般情况在使用ASP.NET开发使用JavaScript有中文汉字时不会出现乱码情况,比如:alert('您看到我了吗?');这样直接输入中文汉字的代码中是不会出现乱码的,如果出现了,一是检查Web. ...
- [CODEVS2603]公路修建
题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公路.修建公路的任务由各城市共同完成. 修建工程分若干轮完成.在每一轮中,每个城市选 ...
- CentOS 7 安装 PyCharm for python
下载链接:http://www.jetbrains.com/pycharm/ 如果只是为了开发python,这个免费版的应该已经够了. 今天讲的是怎么在CentOS7下面安装 pycharm: 下载完 ...