[HDOJ4022]Bombing(离散化+stl)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4022
一个图上有n个点,之后m个操作,每次操作一行或者一列。使得这一行或者这一列的点全部消除。每次操作输出每次消除的点的个数。
思路:
因为数据范围很大,刚开始想的是离散化后维护各行各列的点数,但是发现这样离线的做法只能维护当前状态,更新成了一个难题。后来在思考有没有一个方法,可以在不超过数据范围的情况下,在O(lgn)的限制内维护所有线上的坐标。想来想去,一开始想用map<int, vector<int>>的,但是vector扫描是O(n)的。那就要考虑一个非线性结构,最后想到了红黑树。还是保存重复元素的那个——std::multiset<int>。
所以这道题就变成了一道水题了。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; #define fr first
#define sc second
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, n) for(int i = 0; i < (n); i++)
#define For(i, a, n) for(int i = (a); i < (n); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a)) const int maxn = ;
int n, m;
int x[maxn], y[maxn];
int hx[maxn], hxcnt;
int hy[maxn], hycnt;
int sx[maxn], sy[maxn];
map<int, multiset<int> > xx;
map<int, multiset<int> > yy;
multiset<int>::iterator it; inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int getid(int* h, int hcnt, int x) {
return lower_bound(h, h+hcnt, x) - h;
} int main() {
// FRead();
int c, d;
while(~scanf("%d%d", &n, &m) && n + m) {
Cls(sx); Cls(sy); xx.clear(), yy.clear();
Rep(i, n) {
scan_d(x[i]); scan_d(y[i]);
hx[i] = x[i]; hy[i] = y[i];
xx[x[i]].insert(y[i]);
yy[y[i]].insert(x[i]);
}
sort(hx, hx+n); sort(hy, hy+n);
hxcnt = unique(hx, hx+n) - hx;
hycnt = unique(hy, hy+n) - hy;
Rep(i, n) {
sx[getid(hx, hxcnt, x[i])]++;
sy[getid(hy, hycnt, y[i])]++;
}
Rep(i, m) {
scan_d(c); scan_d(d);
if(c == ) {
printf("%d\n", xx[d].size());
for(it = xx[d].begin();
it != xx[d].end(); it++) {
yy[*it].erase(d);
}
xx[d].clear();
sx[getid(hx, hxcnt, d)] = ;
}
else {
printf("%d\n", yy[d].size());
for(it = yy[d].begin();
it != yy[d].end(); it++) {
xx[*it].erase(d);
}
sy[getid(hy, hycnt, d)] = ;
yy[d].clear();
}
}
printf("\n"); }
return ;
}
[HDOJ4022]Bombing(离散化+stl)的更多相关文章
- HDU 4022 Bombing(stl,map,multiset,iterater遍历)
题目 参考了 1 2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- 洛谷 [P1995] 程序自动分析
并查集+ 离散化 首先本题的数据范围很大,需要离散化, STL离散化代码: //dat是原数据,id是编号,sub是数据的副本 sort(sub + 1, sub + tot + 1); size = ...
- 数据离散化 ( 以及 stl 中的 unique( ) 的用法 )+ bzoj3289:Mato的文件管理
http://blog.csdn.net/gokou_ruri/article/details/7723378 ↑惯例Mark大神的博客 bzoj3289:Mato的文件管理 线段树求逆序对+莫队 ...
- HDU4022 Bombing STL
Bombing Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Su ...
- 离散化——化不可能为可能(STL)
所谓离散,就是化连续为不连续,使得我们某种枚举的方法得以实现. 当然,离散还能够帮助我们将某些数据范围很大达到2^16,但是这些数据并不多(例如才1000+),我们可以把数据进行离散,保持他们之间的相 ...
- 使用STL离散化
把原来的数组a复制一份拷贝b 用sort先把数组a排序 用unique消除a里面重复的元素 对于b中的每一个元素,用lower_bound找到它在a中的位置,也就是离散化之后的编号. 没了. #inc ...
- UVa 221 (STL 离散化) Urban Elevations
题意: 作图为n个建筑物的俯视图,右图为从南向北看的正视图,按从左往右的顺序输出可见建筑物的标号. 分析: 题中已经说了,要么x相同,要么x相差足够大,不会出现精度问题. 给这n个建筑物从左往右排序, ...
- CDOJ 1059 秋实大哥与小朋友 STL(set)+离散化+BIT区间更新单点查询
链接: A - 秋实大哥与小朋友 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Subm ...
随机推荐
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- easyui页面布局
html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- Jenkins入门-转
reference : http://www.cnblogs.com/itech/archive/2011/11/23/2260009.html 在网上貌似没有找到Jenkins的中文的太多的文档,有 ...
- request 路径随笔
1. 路劲可分为 绝对路径 和 相对路径 2. 绝对路径 (开头带"/") 前端: http://localhost:8080/myWebApp/user/login.jsp /m ...
- Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...
- Unix无缓冲文件操作函数、文件信息查询
问题描述: Unix无缓冲文件操作函数.文件信息查询 问题解决: struct stat 结构体信息: 具体代码: 具体源文件:
- 【POJ】【3308】Paratroopers
网络流/二分图最小点权覆盖 sigh……这题……TLE&RE了好几发 建一个二分图,左边的每个结点代表行,右边的代表列,如果在(i,j)这个位置有一个外星人,那么我们就连一条边 (左 i -& ...
- 剑指offer--面试题6
题目:由前序.中序遍历序列重建二叉树 虽然思路能想到,但是实际写却无从下手...下面重现作者代码,多多实践... #include<exception> //首先定义二叉树节点 struc ...
- 【入门篇】Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布
http://blog.csdn.net/allenlinrui/article/details/19419721 1.介绍 Nginx - 高性能web server,这个不用多说了,大家都 ...
- VirtualBox 给虚拟机绑定IP
在VirtualBox中,有时候打开虚拟机,会出现Waiting for 60 seconds more for network configuration这种情况,从而使得开机变得很慢. 通过以下操 ...