CF # 296 C Glass Carving (并查集 或者 multiset)

来源:互联网 发布:网络教育全国统考难吗 编辑:程序博客网 时间:2024/06/05 13:38

C. 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 0001 ≤ 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 4H 2V 2V 3V 1
output
8442
input
7 6 5H 4V 3V 5H 2V 1
output
28161264

Note

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-8typedef __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 10005multiset<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-8typedef __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 200005int 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);elseunitw(op[i],op[i]+1);    }   fre(i,0,n)    {    pf("%I64d\n",ans[i]);    }  return 0;}








1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 毕业证封皮丢了怎么办 留服认证不了怎么办 无公害认证书怎么办 假学历认证报告怎么办 留学要求寄原件怎么办 干部小三怀孕怎么办? 小三的孩子怎么办 把小三打住院了怎么办 小月子没人伺候怎么办 寝室室友有狐臭怎么办 室友在寝室养猫怎么办 和直接领导不合怎么办 房产共有人去世怎么办 发现邪教宣传内容怎么办 说课时两个课时怎么办 投稿文章被拒绝怎么办 中立性细胞偏低怎么办? 孩子爱告状老师怎么办 高中学不好数学怎么办 想做老师应该怎么办 教师职称换学校怎么办 大四差选修学分怎么办 尔雅通识课学分没修满怎么办 大学全英文授课怎么办 小孩脑部有囊肿怎么办 防震期间门应该怎么办 在家待着没意思怎么办 人户分离上学怎么办 小孩上学没人接送怎么办 宝宝上学没人接送怎么办 上海浦东新区酒类许可证怎么办 金税盘里报税处理打不开怎么办 惠民卡到期了怎么办 遇到不拴狗链的主人怎么办 平安福没钱续保怎么办 提前很久到机场怎么办 机场来早了怎么办 机场去早了怎么办 只有高中学历该怎么办 没钱没学历该怎么办 中招分数压线怎么办