Codeforces 527C Glass Carving (最长连续0变形+线段树)
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × 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?
Input
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.
Output
After each cut print on a single line the area of the maximum available glass fragment in mm2.
Examples
Input
Copy
4 3 4
H 2
V 2
V 3
V 1
Output
Copy
8
4
4
2
Input
Copy
7 6 5
H 4
V 3
V 5
H 2
V 1
Output
Copy
28
16
12
6
4
Note
Picture for the first sample test:
递归
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;typedef long long ll; const int maxn = 2 * 1e5 + 10;struct SegTree {ll ls, rs, max0;bool is_all0; }segTree[2][maxn<<2];void pushup(int root, int flag) {SegTree &cur = segTree[flag][root], &lc = segTree[flag][root<<1], &rc = segTree[flag][root<<1|1];cur.ls = lc.ls + (lc.is_all0 ? rc.ls : 0);cur.rs = rc.rs + (rc.is_all0 ? lc.rs : 0);cur.max0 = max(lc.rs + rc.ls, max(lc.max0, rc.max0));cur.is_all0 = lc.is_all0 && rc.is_all0; }void build(int L, int R, int root, int flag) {if (L == R) {segTree[flag][root].ls = segTree[flag][root].rs = segTree[flag][root].max0 = 1;segTree[flag][root].is_all0 = true;return;}int mid = (L + R)>>1;build(L, mid, root<<1, flag);build(mid + 1, R, root<<1|1, flag);pushup(root, flag); }void update_node(int L, int R, int root, int pos, int flag) {if (L == R) {segTree[flag][root].ls = segTree[flag][root].rs = segTree[flag][root].max0 = 0;segTree[flag][root].is_all0 = false;return;}int mid = (L + R)>>1;if (pos <= mid) {update_node(L, mid, root<<1, pos, flag);}else {update_node(mid + 1, R, root<<1|1, pos, flag);}pushup(root, flag); }ll query(int L, int R, int root, int qL, int qR, int flag) {if (qL <= L && R <= qR) {return segTree[flag][root].max0;}int mid = (L + R)>>1;ll temp = 0;if (qL <= mid) {temp = max(temp, query(L, mid, root<<1, qL, qR, flag));}if (qR > mid) {temp = max(temp, query(mid + 1, R, root<<1|1, qL, qR, flag));}return temp; }int main() {int W, H, q, x;char c[5];while (scanf("%d %d %d", &W, &H, &q) == 3) {build(1, W - 1, 1, 0);build(1, H - 1, 1, 1);while (q--) {scanf("%s %d", c, &x);if (c[0] == 'V') {update_node(1, W - 1, 1, x, 0);}else {update_node(1, H - 1, 1, x, 1);}printf("%I64d\n", (query(1, W - 1, 1, 1, W - 1, 0) + 1) * (query(1, H - 1, 1, 1, H - 1, 1) + 1));}} }
View Code
非递归
#include <iostream> #include <cstdio> #include <cmath> #define maxn 200001 using namespace std; int L[maxn<<2][2];//从左开始连续零个数 int R[maxn<<2][2];//从右 int Max[maxn<<2][2];//区间最大连续零 bool Pure[maxn<<2][2];//是否全零 int M[2]; void PushUp(int rt,int k){//更新rt节点的四个数据 Pure[rt][k]=Pure[rt<<1][k]&&Pure[rt<<1|1][k]; Max[rt][k]=max(R[rt<<1][k]+L[rt<<1|1][k],max(Max[rt<<1][k],Max[rt<<1|1][k]));L[rt][k]=Pure[rt<<1][k]?L[rt<<1][k]+L[rt<<1|1][k]:L[rt<<1][k];R[rt][k]=Pure[rt<<1|1][k]?R[rt<<1|1][k]+R[rt<<1][k]:R[rt<<1|1][k]; } void Build(int n,int k){//建树,赋初值for(int i=0;i<M[k];++i) L[M[k]+i][k]=R[M[k]+i][k]=Max[M[k]+i][k]=Pure[M[k]+i][k]=i<n;for(int i=M[k]-1;i>0;--i) PushUp(i,k); } void Change(int X,int k){//切割,更新 int s=M[k]+X-1;Pure[s][k]=Max[s][k]=R[s][k]=L[s][k]=0;for(s>>=1;s;s>>=1) PushUp(s,k); } int main(void) {int w,h,n;while(cin>>w>>h>>n){//以下3行,找出非递归线段树的第一个数的位置。 M[0]=M[1]=1;while(M[0]<h-1) M[0]<<=1;while(M[1]<w-1) M[1]<<=1;//建树 Build(h-1,0);Build(w-1,1);for(int i=0;i<n;++i){//读取数据 char x;int v;scanf(" %c%d",&x,&v);//切割 x=='H'?Change(v,0):Change(v,1);//输出 printf("%I64d\n",(long long)(Max[1][0]+1)*(Max[1][1]+1));}} return 0; }
View Code
其他解法
https://blog.csdn.net/zearot/article/details/44759437
转载于:https://www.cnblogs.com/shuaihui520/p/9835973.html
Codeforces 527C Glass Carving (最长连续0变形+线段树)相关推荐
- Codeforces 527C Glass Carving
vjudge 上题目链接:Glass Carving 题目大意: 一块 w * h 的玻璃,对其进行 n 次切割,每次切割都是垂直或者水平的,输出每次切割后最大单块玻璃的面积: 用两个 set 存储每 ...
- codeforces:E2. Array and Segments (Hard version)【线段树 + 区间修改】
分析 思路很简单 遍历每个作为最大值,然后区间不包含当前最大值的都可以减掉 easy version就可以这样暴力解决 然后求出最大差值 暴力解法 import sys input = sys.std ...
- Codeforces Round #742 (Div. 2) E. Non-Decreasing Dilemma (线段树维护区间连续问题)
题意: 操作1:把x位置的数字修改成y. 操作2:查询[l,r]之间不下降序列的个数. 题解: 线段树维护区间和问题 (这是套路,想不到只能说做题少别打我) . 用五个变量进行维护. sum区间总个数 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- codeforces 877E. Danil and a Part-time Job (DFS序列+线段树)
传送门:codeforces 877E 题目大意: 有一颗树,树的每个节点有一盏灯,状态为亮或灭.现在可以进行以下两种操作: 1.pow x,将以 x 为根节点的子树(包括根节点)的所有节点的灯的状态 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新
http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...
- Codeforces Round #737 (Div. 2) D. Ezzat and Grid 线段树动态开点
传送门 文章目录 题意: 思路: 题意: 思路: 比较套路的一个题,我们维护一个dp[i]dp[i]dp[i]表示到了第iii行能保留的区间最多是多少. 转移比较明显:dp[i]=max(dp[j]) ...
- YBTOJ:最短时间(长链剖分、线段树)
解析 不难得到最优策略:先尽可能的快的送死直到路径畅通无组,然后一口气冲到t点. 现在的难点就在于如何尽可能的快的送掉特定的次数. 不难发现,花费时间关于死亡次数的函数必然是一个下凸包. 设 fx,i ...
最新文章
- linux查询首字符不是T,linux – tload输出中的不同字符是什么意思?
- android:activity的生命周期及它们之间的传值
- 遇到的问题锦集及解决方案
- 网络编程 UDP套接字
- 相对URI以及base的设置
- php怎么读取mq的数据,php – 无法读取RabbitMQ的所有消息
- 服务器虚拟化的意思,服务器虚拟化存储的好处以及作用
- mysql在linux下诸多稀奇古怪的错误
- python判断用户名是否有效_Python校验用户名是否合法示例
- Java实现智能对话机器人自动聊天+语音秒回
- python之常用快捷键
- 彻底杀除“logo1_.exe”(威金病毒)病毒
- 【网络】PFC背景和原理 、文档(DCB=PFC + ETS,DCBX=DCB扩展)
- unity资源商店出现“抱歉,此链接不再有效”怎么办
- ZUI datagrid 数据表格重新渲染问题
- 供应链金融业务如何脱颖而出?
- Shapefile属性操作之增
- 放射技师计算机辅助诊断,基于CT影像的肺癌计算机辅助诊断关键技术研究
- 第二周:java异常和常用类 容器
- 机器学习数据划分笔记(train_test_split)
热门文章
- 【6.1】python中的变量是什么
- 技术人员转型是件痛苦的事情~
- vue的列表交错过渡
- android listview源码--第一章RecycleBin
- 设计过程中常见的 10 个小问题
- Spring Boot + Mybatis 实现动态数据源
- [转]软件保护之注册算法篇
- 错误 1093 You can't specify target table 'table name' for update in FROM clause
- [转] 初识Firebug(HTML查看和编辑、Javascript控制台、网络状况监视器)
- 技术驱动还是产品驱动