矩形面积交

例题:hdu1255
题意:裸题,不过是要交两次以上的面积
基本上都和面积并差不多

#include<bits/stdc++.h>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1

const int maxn=2005;
struct node//和面积并一样,存放横向的线(把一个矩形拆成上下两根线)
{
    double x1,x2,h;
    int sta;
    node(){}
    node(double x1,double x2,double h,int sta):x1(x1),x2(x2),h(h),sta(sta){}
    bool operator < (const node &a)const//这些横向线按y升序排序
    {
        if(h==a.h)return x1<a.x1;
        return h<a.h;
    }
}p[maxn<<1];

double x[maxn];//存放所有出现过的x值,用来离散化
double tree1[maxn<<2],tree2[maxn<<2];//分别维护只有一次和两次的x区间长度
int lazy[maxn<<2];//对x区间的标记

void pushup(int rt,int l,int r)
{
    if(lazy[rt]>=2)tree1[rt]=tree2[rt]=x[r+1]-x[l];//如果当前这个区间已经有两次了,一次和二次直接加上这段长度
    else if(lazy[rt]==1)//如果当前这个区间只有一次,一次的要更新
    {
        tree1[rt]=x[r+1]-x[l];
        if(l==r)tree2[rt]=0;
        else tree2[rt]=tree1[lson]+tree1[rson];
    }
    else
    {
        if(l==r)tree1[rt]=tree2[rt]=0;
        else tree1[rt]=tree1[lson]+tree1[rson],tree2[rt]=tree2[lson]+tree2[rson];
    }
}

void updata(int rt,int l,int r,int x,int y,int v)
{
    if(l==r)
    {
        lazy[rt]+=v;
        pushup(rt,l,r);
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid)updata(lson,l,mid,x,y,v);
    if(y>=mid+1)updata(rson,mid+1,r,x,y,v);
    pushup(rt,l,r);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        double x1,x2,y1,y2;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            p[i]=node(x1,x2,y1,1);
            p[n+i]=node(x1,x2,y2,-1);
            x[i]=x1,x[i+n]=x2;
        }
        n<<=1;
        sort(p+1,p+1+n);
        sort(x+1,x+1+n);
        memset(tree1,0,sizeof(tree1));
        memset(tree2,0,sizeof(tree2));
        memset(lazy,0,sizeof(lazy));
        double ans=0;
        int sz=unique(x+1,x+1+n)-x-1;
        for(int i=1;i<n;i++)
        {
            int l=lower_bound(x+1,x+1+sz,p[i].x1)-x;
            int r=lower_bound(x+1,x+1+sz,p[i].x2)-x;
            updata(1,1,sz,l,r-1,p[i].sta);
            ans+=tree2[1]*(p[i+1].h-p[i].h);
        }
        printf("%.2f\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,这是一份 C 语言代码,用于计算两个矩形面积。 ``` #include <stdio.h> struct Rectangle { int x1, y1, x2, y2; }; int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } int overlap(struct Rectangle a, struct Rectangle b) { int x_overlap = max(0, min(a.x2, b.x2) - max(a.x1, b.x1)); int y_overlap = max(0, min(a.y2, b.y2) - max(a.y1, b.y1)); return x_overlap * y_overlap; } int main() { struct Rectangle a, b; printf("请输入第一个矩形的坐标(x1 y1 x2 y2):"); scanf("%d%d%d%d", &a.x1, &a.y1, &a.x2, &a.y2); printf("请输入第二个矩形的坐标(x1 y1 x2 y2):"); scanf("%d%d%d%d", &b.x1, &b.y1, &b.x2, &b.y2); printf("两个矩形面积为:%d\n", overlap(a, b)); return 0; } ``` ### 回答2: 下面是一个用C语言实现计算矩形面积的代码: ```c #include <stdio.h> int main() { // 输入两个矩形的左上角和右下角坐标 int x1, y1, x2, y2; int x3, y3, x4, y4; printf("请输入第一个矩形的左上角坐标(x1, y1): "); scanf("%d%d", &x1, &y1); printf("请输入第一个矩形的右下角坐标(x2, y2): "); scanf("%d%d", &x2, &y2); printf("请输入第二个矩形的左上角坐标(x3, y3): "); scanf("%d%d", &x3, &y3); printf("请输入第二个矩形的右下角坐标(x4, y4): "); scanf("%d%d", &x4, &y4); // 计算矩形的宽度和高度 int width = 0, height = 0; if (x1 <= x4) { if (x2 <= x3) { printf("矩形面积为0\n"); return 0; } else { width = x2 - x3; } } else { if (x4 <= x1) { printf("矩形面积为0\n"); return 0; } else { width = x4 - x1; } } if (y1 <= y4) { if (y2 <= y3) { printf("矩形面积为0\n"); return 0; } else { height = y2 - y3; } } else { if (y4 <= y1) { printf("矩形面积为0\n"); return 0; } else { height = y4 - y1; } } // 计算矩形面积 int area = width * height; printf("矩形面积为:%d\n", area); return 0; } ``` 运行程序后,会提示输入两个矩形的左上角和右下角坐标。根据输入的坐标,程序会计算矩形的宽度和高度,并判断是否有面积。如果有面积,程序会计算并输出矩形面积;如果没有面积,则输出0。 ### 回答3: 下面是一个使用C语言实现矩形面积的代码示例: ```c #include<stdio.h> // 定义矩形结构体 typedef struct { int x; // 左下角的x坐标 int y; // 左下角的y坐标 int width; // 矩形的宽度 int height; // 矩形的高度 } Rectangle; // 计算矩形面积的函数 int getIntersectionArea(Rectangle rect1, Rectangle rect2) { // 判断两个矩形是否有重叠部分 if (rect1.x + rect1.width <= rect2.x || rect2.x + rect2.width <= rect1.x || rect1.y + rect1.height <= rect2.y || rect2.y + rect2.height <= rect1.y) { return 0; // 没有重叠部分,面积为0 } // 计算重叠部分的左下角坐标 int x = rect1.x > rect2.x ? rect1.x : rect2.x; int y = rect1.y > rect2.y ? rect1.y : rect2.y; // 计算重叠部分的右上角坐标 int right = (rect1.x + rect1.width) < (rect2.x + rect2.width) ? (rect1.x + rect1.width) : (rect2.x + rect2.width); int top = (rect1.y + rect1.height) < (rect2.y + rect2.height) ? (rect1.y + rect1.height) : (rect2.y + rect2.height); // 计算重叠部分的宽度和高度 int width = right - x; int height = top - y; return width * height; // 返回面积 } int main() { Rectangle rect1 = {1, 1, 4, 4}; // 第一个矩形 Rectangle rect2 = {3, 3, 4, 4}; // 第二个矩形 int intersectionArea = getIntersectionArea(rect1, rect2); // 计算矩形面积 printf("矩形面积为: %d\n", intersectionArea); return 0; } ``` 上述代码定义了一个Rectangle结构体用来表示矩形的左下角坐标、宽度和高度,并实现了一个getIntersectionArea函数来计算两个矩形面积。在main函数中,我们创建了两个矩形,并调用getIntersectionArea函数来计算它们的面积,并将结果打印输出。 请注意,上述代码中的矩形坐标只是示例,您可以根据实际需求修改矩形的坐标来计算不同的矩形面积

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值