zoukankan      html  css  js  c++  java
  • CF #296 (Div. 1) A. Glass Carving 线段树

    A. Glass Carving
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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?

    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.

    Sample test(s)
    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的矩形,然后用水平线和竖直线切割,每次切割后输出最大的子面积。
     
    思路:
    很明显最大子面积是横着最大的长度*竖着最大的长度,那么建两棵线段树,每个区间lx、rx、maxh分别为离该区间左端点最近的已经切割过的点、离右端点最近的已经切割过的点、最长的切割长度。维护这三个值就行了。代码略丑。
     
    代码:
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <iostream>
      5 #include <vector>
      6 #include <queue>
      7 #include <cmath>
      8 #include <set>
      9 using namespace std;
     10 
     11 #define N 200005
     12 #define ll root<<1
     13 #define rr root<<1|1
     14 #define mid1 (a[root].l+a[root].r)/2
     15 #define mid2 (b[root].l+b[root].r)/2
     16 
     17 int w, h, n;
     18 struct node{
     19     int l, r;
     20     int lx, rx;
     21     int maxh;
     22 }a[N*4], b[N*4];
     23 
     24 void build1(int l,int r,int root){
     25     a[root].l=l;
     26     a[root].r=r;
     27     if(l==r){
     28         if(l==0) a[root].lx=a[root].rx=0;
     29         else if(l==w) a[root].lx=a[root].rx=w;
     30         else a[root].lx=a[root].rx=-1;
     31         a[root].maxh=0;
     32         return;
     33     }
     34     build1(l,mid1,ll);
     35     build1(mid1+1,r,rr);
     36     a[root].lx=a[ll].lx;
     37     a[root].rx=a[rr].rx;
     38     a[root].maxh=a[root].r-a[root].l;
     39 }
     40 
     41 void build2(int l,int r,int root){
     42     b[root].l=l;
     43     b[root].r=r;
     44     if(l==r){
     45         if(l==0) b[root].lx=b[root].rx=0;
     46         else if(l==h) b[root].lx=b[root].rx=h;
     47         else b[root].lx=b[root].rx=-1;
     48         b[root].maxh=0;
     49         return;
     50     }
     51     build2(l,mid2,ll);
     52     build2(mid2+1,r,rr);
     53     b[root].lx=b[ll].lx;
     54     b[root].rx=b[rr].rx;
     55     b[root].maxh=b[root].r-b[root].l;
     56 }
     57 
     58 void update1(int p,int root){
     59     if(a[root].l==p&&a[root].r==p){
     60         a[root].lx=a[root].rx=p;return;
     61     }
     62     if(p<=a[ll].r) update1(p,ll);
     63     else update1(p,rr);
     64     int ln, rn;
     65     if(a[ll].lx!=-1) a[root].lx=a[ll].lx;
     66     else if(a[ll].rx!=-1) a[root].lx=a[ll].rx;
     67     else if(a[rr].lx!=-1) a[root].lx=a[rr].lx;
     68     else if(a[rr].rx!=-1) a[root].lx=a[rr].rx;
     69     else a[root].lx=-1;
     70     
     71     if(a[rr].rx!=-1) a[root].rx=a[rr].rx;
     72     else if(a[rr].lx!=-1) a[root].rx=a[rr].lx;
     73     else if(a[ll].rx!=-1) a[root].rx=a[ll].rx;
     74     else if(a[ll].lx!=-1) a[root].rx=a[ll].lx;
     75     else a[root].rx=-1;
     76     
     77     if(a[ll].rx!=-1) ln=a[ll].r-a[ll].rx;
     78     else ln=a[ll].r-a[ll].l;
     79     if(a[rr].lx!=-1) rn=a[rr].lx-a[rr].l;
     80     else rn=a[rr].r-a[rr].l;
     81     
     82     a[root].maxh=max(max(a[ll].maxh,a[rr].maxh),ln+rn+1);
     83 }
     84 
     85 void update2(int p,int root){
     86     if(b[root].l==p&&b[root].r==p){
     87         b[root].lx=b[root].rx=p;return;
     88     }
     89     if(p<=b[ll].r) update2(p,ll);
     90     else update2(p,rr);
     91     int ln, rn;
     92     if(b[ll].lx!=-1) b[root].lx=b[ll].lx;
     93     else if(b[ll].rx!=-1) b[root].lx=b[ll].rx;
     94     else if(b[rr].lx!=-1) b[root].lx=b[rr].lx;
     95     else if(b[rr].rx!=-1) b[root].lx=b[rr].rx;
     96     else b[root].lx=-1;
     97     
     98     if(b[rr].rx!=-1) b[root].rx=b[rr].rx;
     99     else if(b[rr].lx!=-1) b[root].rx=b[rr].lx;
    100     else if(b[ll].rx!=-1) b[root].rx=b[ll].rx;
    101     else if(b[ll].lx!=-1) b[root].rx=b[ll].lx;
    102     else b[root].rx=-1;
    103     if(b[ll].rx!=-1) ln=b[ll].r-b[ll].rx;
    104     else ln=b[ll].r-b[ll].l;
    105     if(b[rr].lx!=-1) rn=b[rr].lx-b[rr].l;
    106     else rn=b[rr].r-b[rr].l;
    107     b[root].maxh=max(max(b[ll].maxh,b[rr].maxh),ln+rn+1);
    108 }
    109 
    110 main()
    111 {
    112     int i, j, k;
    113     while(scanf("%d %d %d",&w,&h,&n)==3){
    114         char s[5];
    115         build1(0,w,1);
    116         build2(0,h,1);
    117     //    printf("%d %d
    ",a[1].maxh,b[1].maxh);
    118         while(n--){
    119             scanf("%s%d",s,&k);
    120             if(s[0]=='H'){
    121                 update2(k,1);
    122             }
    123             else{
    124                 update1(k,1);
    125             }
    126         //    printf("%d %d
    ",a[1].maxh,b[1].maxh);
    127             printf("%I64d
    ",(__int64)a[1].maxh*(__int64)b[1].maxh);
    128         }
    129     }
    130 }
  • 相关阅读:
    BUAA面向对象第一单元作业总结
    Kafka 总结学习
    Mybatis学习-GetMybatisInMyHead
    大数据实战-电信客服-重点记录
    基于有穷状态机思想的电梯系统
    Selective Search-目标检测“垫脚石”
    SparkSQL 实验
    Spark Core实验
    MapReduce实验
    NoSQL实验
  • 原文地址:https://www.cnblogs.com/qq1012662902/p/4805116.html
Copyright © 2011-2022 走看看