CF # 296 C Glass Carving (并查集 或者 multiset)
2 seconds
256 megabytes
standard input
standard output
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm × h mm
sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.
In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made
glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.
After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.
Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?
The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000).
Next n lines contain the descriptions of the cuts. Each description has the form H y or V x.
In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1)
from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1)
millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.
After each cut print on a single line the area of the maximum available glass fragment in mm2.
4 3 4
H 2
V 2
V 3
V 1
8
4
4
2
7 6 5
H 4
V 3
V 5
H 2
V 1
28
16
12
6
4
Picture for the first sample test:

Picture for the second sample test:

题意:切玻璃,每切一刀求当前的最大快的面积
思路: 最重要到额一点是找横的最大和竖着的最大
假设用 set,每次把新的长宽插入。然后erase()被破坏的长或者宽
假设用并查集,先把没有切的看成一个总体,然后重后往前依次去掉一条切得线后能够得到新的一个长或者宽
multiset 代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8
typedef __int64 ll; #define fre(i,a,b) for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define ssf(n) scanf("%s", n)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define bug pf("Hi\n") using namespace std; #define INF 0x3f3f3f3f
#define N 10005 multiset<ll>x,y;
multiset<ll>hx,hy;
multiset<ll>::iterator it; char ch[10]; ll w,h;
ll xx;
int n; int main()
{
int i,j;
scanf("%I64d%I64d%d",&w,&h,&n); x.insert(0);
x.insert(w); y.insert(0);
y.insert(h); hx.insert(w);
hy.insert(h); while(n--)
{
scanf("%s%d",ch,&xx);
if(ch[0]=='V')
{
x.insert(xx); it=x.find(xx);
it++;
ll ri=*it;
it--;
it--;
ll le=*it;
it=hx.find(ri-le); //这一行和下一行不能用hx.erase(ri-le)取代,由于这样是删除全部的
hx.erase(it); //插入一条线,破坏了一个长度
hx.insert(ri-xx); //添加了两个长度
hx.insert(xx-le);
}
else
{
y.insert(xx); it=y.find(xx);
it++;
ll ri=*it;
it--;
it--;
ll le=*it; it=hy.find(ri-le);
hy.erase(it);
hy.insert(ri-xx);
hy.insert(xx-le);
} it=hx.end();
it--;
ll le=*it; it=hy.end(); //长宽都取最大值
it--;
ll ri=*it; pf("%I64d\n",le*ri);
}
return 0;
}
并查集代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8
typedef __int64 ll; #define fre(i,a,b) for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define ssf(n) scanf("%s", n)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define bug pf("Hi\n") using namespace std; #define INF 0x3f3f3f3f
#define N 200005 int lenh,lenw; //长的最大值与宽的最大值
int fah[N],faw[N];
int numh[N],numw[N];
int vish[N],visw[N];
int w,h,n;
int op[N];
ll ans[N];
char ch[N][3]; int chah(int x)
{
if(fah[x]!=x)
fah[x]=chah(fah[x]);
return fah[x];
} void unith(int x,int y)
{
x=chah(x);
y=chah(y); if(x==y) return ; fah[y]=x;
numh[x]+=numh[y];
lenh=max(numh[x],lenh);
} int chaw(int x)
{
if(faw[x]!=x)
faw[x]=chaw(faw[x]);
return faw[x];
} void unitw(int x,int y)
{
x=chaw(x);
y=chaw(y); if(x==y) return ; faw[y]=x;
numw[x]+=numw[y]; lenw=max(numw[x],lenw);
} int main()
{
int i,j;
sfff(w,h,n); mem(visw,0);
mem(vish,0);
int t=max(h,w); fre(i,0,t+1)
{
fah[i]=i;
numh[i]=1;
faw[i]=i;
numw[i]=1;
} numh[0]=numw[0]=0; int x; fre(i,0,n)
{
scanf("%s%d",ch[i],&op[i]); if(ch[i][0]=='H')
vish[op[i]]=1;
else
visw[op[i]]=1; } lenh=1;
lenw=1; fre(i,1,h)
if(!vish[i])
unith(i,i+1); fre(i,1,w)
if(!visw[i])
unitw(i,i+1); // fre(i,1,h)
// if(fah[i]==i)
// {
// pf("%d %d\n",i,numh[i]);
//
// } // fre(i,1,w)
// if(faw[i]==i)
// {
// pf("%d %d\n",i,numw[i]);
//
// } free(i,n-1,0)
{
//pf("****%d %d\n",lenw,lenh); ans[i]=(ll)(lenh)*(ll)(lenw); if(ch[i][0]=='H')
unith(op[i],op[i]+1);
else
unitw(op[i],op[i]+1);
} fre(i,0,n)
{
pf("%I64d\n",ans[i]);
} return 0;
}
CF # 296 C Glass Carving (并查集 或者 multiset)的更多相关文章
- cf 之lis+贪心+思维+并查集
https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...
- C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 && 并查集方法)
C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF #296 (Div. 1) A. Glass Carving 线段树
A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF 115 A 【求树最大深度/DFS/并查集】
CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- CF 500 B. New Year Permutation 并查集
User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permut ...
- Codeforces Round #296 (Div. 1) A. Glass Carving Set的妙用
A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #296 (Div. 2) C. Glass Carving [ set+multiset ]
传送门 C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 【转】Linux 之 数据流重定向
转自:http://www.linuxidc.com/Linux/2012-09/69764.htm linux在你登入时,便将默认的标准输入.标准输出.标准错误输出安排成你的终端.I/O重定向就是你 ...
- Ch03 React/JSX/Component 簡介
Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...
- 4th 循环结构概述和for语句的格式及其使用
04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for,while,do...while B:循环结构for语句的格式: for(初始化表达式;条件表达式; ...
- 关于java 关键字enum不识别的解决办法
从别人那儿拷贝过来的myeclipse java工程,打开一看标红了一大片,仔细一看,原来是不识别enum关键字,这就有点尴尬了. 我自己重新建了一个java工程,测试了下,假如我在新建工程的时候选择 ...
- pythonGUI编程——Qt库(1)
1.简单示例实现一个小窗口. PyQt5是一种高级的语言,下面只有几行代码就能显示一个小窗口.底层已经实现了窗口的基本功能. #!/usr/bin/python#coding:utf-8# ...
- 人工智能时代,你为什么一定要学Python?
Python岗位年薪至少在10-20w之间,而且除了北.上.广.深外,杭州和合肥等二.三线城市的待遇正在与一线城市持平.未来,无论你身处何地,都能享受到人工智能.Python“带来的“市场红利”. P ...
- C# 通知机制 IObserver<T> 和 IObservable<T>
class Program { public static void Main() { // Define a provider and two observers. LocationTracker ...
- [luogu2591 ZJOI2009] 函数
传送门 Solution 画图找规律.. Code //By Menteur_Hxy #include <cstdio> #define min(a,b) ((a)>(b)?(b): ...
- 【郑轻邀请赛 F】 Tmk吃汤饭
[题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2132 [题意] [题解] 很容易想到用队列来模拟; 这个队列维护的是正在煮的4个人煮 ...
- Linux系统自带服务罗列
/ect/services 文件列出了系统详细的服务 红色字体为常用服务 acpid ACPI(全称 Advanced Configuration and Power Interface)服务是电源管 ...