题目链接:http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2167

Bobo has n × m points arranged into a matrix with n rows and m columns. The points in the intersection of the i-th row and the j-th column is labeled with (i, j). He is going to perform q operations of the following 2 kinds.

Given parameters a, b, add edges between points (i, j) and (i, j + 1) for all a ≤ i ≤ b and 1 ≤ j < m.
Given parameters a, b, add edges between points (i, j) and (i + 1, j) for all 1 ≤ i < n and a ≤ j ≤ b.
Bobo would like to know the number of connected components after each operation.

1 ≤ n, m ≤ 109
1 ≤ q ≤ 105
ti ∈ {1, 2}
If ti = 1, 1 ≤ ai ≤ bi ≤ n. If ti = 2, 1 ≤ ai ≤ bi ≤ m.
The sum of q does not exceed 5 × 105.
Input
The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains three integers n, m and q.

The i-th of the following q lines contains three integers ti, ai and bi where ti is the kind of the operation i and ai, bi are corresponding parameters.

Output
For each test case, print q integers which denote the number of connected components after each operation.

Sample Input
2 3 3
2 1 1
2 3 3
1 1 1
1000000000 1000000000 1
1 1 2
Sample Output
5
4
2
999999998000000002
Hint
Source
2018湖南省第14届大学生计算机程序设计竞赛

思路:在纸上画一下图,可以发现答案跟a,b所跨水平和垂直区间有关,如图,假设nn为水平方向所占用的区间,mm为垂直方向所占的区间,且共有n行m列,则它是把mm×m+nn×n-nn×mm个点连通成一个点,所以答案ans=n×m-(mm×m+nn×n-nn×mm)+1=n×m-mm×m-nn×n+nn×mm+1,但注意当水平方向占的区间为0时,是将mm行的点连通成mm个点,即mm×m个点连通成mm个点,此时ans=n×m-mm×m+mm,当垂直方向区间为0时也可想而知;

湖南省第十四届程序设计大赛E:Grid
AC代码:

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x,y;
    node*l,*r;
}*t1,*t2;
int Insert(int x,int y,node *t)
{
    if(t->x==0){t->x=x;t->y=y;return y-x+1;}
    if(x>=t->x&&y<=t->y) return 0;
    if(y<t->x)
    {
        if(t->l==NULL)
        {
            node *s=(node*)malloc(sizeof(node));
            s->l=s->r=NULL;
            s->x=0;
            t->l=s;
        }
        return Insert(x,y,t->l);
    }
    if(x>t->y)
    {
        if(t->r==NULL)
        {
            node *s=(node*)malloc(sizeof(node));
            s->l=s->r=NULL;
            s->x=0;
            t->r=s;
        }
        return Insert(x,y,t->r);
    }
    int ans=0;
    if(x<t->x)
    {
         if(t->l==NULL)
        {
            node *s=(node*)malloc(sizeof(node));
            s->l=s->r=NULL;
            s->x=0;
            t->l=s;
        }
        ans+=Insert(x,t->x-1,t->l);
    }
    if(y>t->y)
    {
        if(t->r==NULL)
        {
            node *s=(node*)malloc(sizeof(node));
            s->l=s->r=NULL;
            s->x=0;
            t->r=s;
        }
        ans+=Insert(t->y+1,y,t->r);
    }
    return ans;
}
int main()
{
    int n,m,q;
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        long long ans=(long long)n*m;
        int opt,x,y;
        long long nn=0,mm=0;
        t1=(node*)malloc(sizeof(node));
        t1->x=0;t1->l=t1->r=NULL;
        t2=(node*)malloc(sizeof(node));
        t2->x=0;t2->l=t2->r=NULL;
        while(q--)
        {
            int opt,x,y;
            scanf("%d%d%d",&opt,&x,&y);
            if(opt==1)
            {
                mm+=Insert(x,y,t1);
                if(nn==0)
                    printf("%lld\n",ans-mm*m+mm);
                else printf("%lld\n",ans-mm*m-nn*n+nn*mm+1);
            }
            else
            {
                nn+=Insert(x,y,t2);
                if(mm==0)
                    printf("%lld\n",ans-nn*n+nn);
                else printf("%lld\n",ans-mm*m-nn*n+nn*mm+1);
            }
        }
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-19
  • 2022-02-14
猜你喜欢
  • 2022-12-23
  • 2021-11-10
  • 2021-10-16
  • 2021-12-16
  • 2022-12-23
  • 2021-12-08
相关资源
相似解决方案