CodeForces - 527C

博客详细介绍了CodeForces上的Glass Carving问题,描述了如何通过水平和垂直切割玻璃来练习切割技巧,并讨论了解题思路,包括使用STL中的Set和Multiset容器来存储切割信息,动态更新最大玻璃碎片的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Glass Carving

Description:

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.

Example

Input
4 3 4
H 2
V 2
V 3
V 1
Output
8
4
4
2

Input
7 6 5
H 4
V 3
V 5
H 2
V 1
Output
28
16
12
6
4

Note:

Picture for the first sample test:
这里写图片描述
Picture for the second sample test:
这里写图片描述


题目大意:
有一块长w宽h的玻璃,每次水平或垂直的切一刀。每做一次切割以后,输出当前的最大的玻璃面积。

解题思路:
1.由于都是水平或者垂直的切割,所有的玻璃都是长方形的,只要知道两条边的长度就可以知道面积了。
2.由于切割点不会重复,可以用set容器存储各个端点,并且set容器是自动有序。另外由于线段长度是会重复的,因此需要用Multiset容器存储各个线段长。
3.每做一次切割,就在相应方向的set中找到边界点,pos1-pos2可以得到线段长度,再回到相应的Multiset中找出边长删去,并将两个新的边长放回。

            it = h.find(num);
            it++;
            pos1 = *it;       //切割点 右边的端点
            it--;  it--;
            pos2 = *it;       //切割点 左边的端点

4.水平方向和竖直方向分开存储,每次分别取出最大的边长度相乘就得到了最大的玻璃面积。


源代码

#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<set>
#include<map>
using namespace std;

set<int> v;
set<int> h;
multiset<int> dv;
multiset<int> dh;
set<int>::iterator it;

 int main() {
    int Ver, Hor, times;
    char cmd[2];
    int num, i;
    int pos1, pos2;
    scanf("%d%d%d", &Ver, &Hor, &times);
    v.insert(0);
    h.insert(0);
    v.insert(Ver);
    h.insert(Hor);
    dv.insert(Ver);
    dh.insert(Hor);
    for (i = 0; i < times; i++) {
        scanf("%s%d", cmd, &num);
        if (cmd[0] == 'H') {
            h.insert(num);
            it = h.find(num);
            it++;
            pos1 = *it;
            it--;  it--;
            pos2 = *it;
            it = dh.find(pos1 - pos2);
            dh.erase(it);
            dh.insert(pos1-num);
            dh.insert(num-pos2);
        }
        else {
            v.insert(num);
            it = v.find(num);
            it++;
            pos1 = *it;
            it--;   it--;
            pos2 = *it;
            it = dv.find(pos1 - pos2);
            dv.erase(it);
            dv.insert(pos1-num);
            dv.insert(num-pos2);

        }
        it = dv.end();
        it--;
        pos1 = *it;
        it = dh.end(); 
        it--;
        pos2 =*it;
        printf("%lld\n", (long long)pos1 * pos2);
    }
    return 0;
}
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值