POJ 1765 November Rain

November Rain
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2097 Accepted: 448
Case Time Limit: 2000MS

Description

Contemporary buildings can have very complicated roofs. If we take a vertical section of such a roof it results in a number of sloping segments. When it is raining the drops are falling down on the roof straight from the sky above. Some segments are completely exposed to the rain but there may be some segments partially or even completely shielded by other segments. All the water falling onto a segment as a stream straight down from the lower end of the segment on the ground or possibly onto some other segment. In particular, if a stream of water is falling on an end of a segment then we consider it to be collected by this segment. 

For the purpose of designing a piping system it is desired to compute how much water is down from each segment of the roof. To be prepared for a heavy November rain you should count one liter of rain water falling on a meter of the horizontal plane during one second. 

Task 
Write a program that: 

reads the description of a roof, 
computes the amount of water down in one second from each segment of the roof, 
writes the results. 

Input

The first line of the input contains one integer n (1 <= n < = 40000) being the number of segments of the roof. Each of the next n lines describes one segment of the roof and contains four integers x1, y1, x2, y2 (0 <= x1, y1, x2, y2 < = 1000000, x1 < x2, y1<>y2) separated by single spaces. Integers x1, y1 are respectively the horizontal position and the height of the left end of the segment. Integers x2, y2 are respectively the horizontal position and the height of the right end of the segment. The segments don't have common points and there are no horizontal segments. You can also assume that there are at most 25 segments placed above any point on the ground level. 

Output

The output consists of n lines. The i-th line should contain the amount of water (in liters) down from the i-th segment of the roof in one second.

Sample Input

6
13 7 15 6
3 8 7 7
1 7 5 6
5 5 9 3
6 3 8 2
9 6 12 8

Sample Output

2
4
2
11
0
3

Source


  线段树好题,花了很长的时间的构思,首先这道题离散化是肯定的。建立了两棵线段树,一是段更新,来维护遮蔽的区间。二是点更新,来维护从某一点下来的水量。
查了很长时间的代码,没想到却死在了sort上,在排序的时候数据超出int了导致溢出了,小遗憾啊
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
#define N 41000
using namespace std;
struct num
{
    int l,r,tl,tr;
    __int64 sum;
}a[8*N],b[8*N];
struct Num
{
    __int64 x1,y1,x2,y2;
    int pos;
}c[N];
int x[2*N];
__int64 ans[N];
map<int,int>ma,ma1;
int w = 0;
bool cmp(Num p1,Num p2)
{
    if(p2.x1>=p1.x1&&p2.x1<=p1.x2)
    {
        double y = (double)((p1.y2-p1.y1)*(p2.x1-p1.x1))/(p1.x2-p1.x1) + (double)p1.y1;
        return y>(double)p2.y1;
    }
    if(p2.x2>=p1.x1&&p2.x2<=p1.x2)
    {
        double y = (double)((p1.y2-p1.y1)*(p2.x2-p1.x1))/(p1.x2-p1.x1) + (double)p1.y1;
        return y>(double)p2.y2;
    }
    int k1 = min(p1.y1,p1.y2);
    int k2 = min(p2.y1,p2.y2);
    if(k1!=k2)
    {
        return k1>k2;
    }else
    {
        return p1.x2<p2.x1;
    }
}
int main()
{
    //freopen("data.txt","r",stdin);
    void build1(int k,int l,int r);
    void build2(int k,int l,int r);
    __int64 find1(int k,int l,int r);
    __int64 find2(int k,int l,int r);
    void update2(int k,int l,int val);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d %I64d %I64d %I64d",&c[i].x1,&c[i].y1,&c[i].x2,&c[i].y2);
            c[i].pos = i;
            x[2*i-1] = c[i].x1;
            x[2*i] = c[i].x2;
        }
        sort(c+1,c+n+1,cmp);
        sort(x+1,x+2*n+1);
        int Top = 0;
        ma.clear();
        ma1.clear();
        for(int i=1;i<=2*n;i++)
        {
            int k = x[i];
            if(ma[k]==0)
            {
                ma[k]=++Top;
                ma1[Top] = k;
            }
        }
        build1(1,1,Top);
        build2(1,1,Top);
        for(int i=1;i<=n;i++)
        {
            __int64 res1 = find1(1,c[i].x1,c[i].x2);
            res1 = c[i].x2-c[i].x1-res1;
            __int64 res2 =find2(1,c[i].x1,c[i].x2);
            res1 = res1+res2;
            if(c[i].y1>c[i].y2)
            {
                update2(1,c[i].x2,res1);
            }else
            {
                update2(1,c[i].x1,res1);
            }
            ans[c[i].pos] = res1;
        }
        for(int i=1;i<=n;i++)
        {
            printf("%I64d\n",ans[i]);
        }
    }
    return 0;
}
void build1(int k,int l,int r)
{
    a[k].l = l; a[k].r = r;
    a[k].tl = ma1[l]; a[k].tr = ma1[r];
    a[k].sum = 0;
    if(l+1==r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build1(k<<1,l,mid);
    build1(k<<1|1,mid,r);
}
void pushup1(int k)
{
    a[k].sum = a[k<<1].sum+a[k<<1|1].sum;
}
__int64 find1(int k,int l,int r)
{
    if(a[k].tl==l&&a[k].tr==r)
    {
        __int64 val = a[k].sum;
        a[k].sum = r-l;
        return val;
    }
    if(a[k].tl<=l&&a[k].tr>=r&&(a[k].sum==a[k].tr-a[k].tl))
    {
        return r-l;
    }
    __int64 sum;
    if(a[k<<1].tr>=r)
    {
        sum = find1(k<<1,l,r);
    }else if(a[k<<1|1].tl<=l)
    {
        sum = find1(k<<1|1,l,r);
    }else
    {
        sum = find1(k<<1,l,a[k<<1].tr);
        sum += find1(k<<1|1,a[k<<1|1].tl,r);
    }
    pushup1(k);
    return sum;
}
void build2(int k,int l,int r)
{
    b[k].l = l; b[k].r = r;
    b[k].tl = ma1[l]; b[k].tr = ma1[r];
    b[k].sum = 0;
    if(l==r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build2(k<<1,l,mid);
    build2(k<<1|1,mid+1,r);
}
void pushup2(int k)
{
    b[k].sum = b[k<<1].sum+b[k<<1|1].sum;
}
__int64 find2(int k,int l,int r)
{
    if(b[k].tl==l&&b[k].tr==r)
    {
        __int64 val = b[k].sum;
        b[k].sum = 0;
        return val;
    }
    __int64 sum;
    if(b[k].sum==0)
    {
        b[k<<1].sum = 0;
        b[k<<1|1].sum = 0;
    }
    if(b[k<<1].tr>=r)
    {
        sum = find2(k<<1,l,r);
    }else if(b[k<<1|1].tl<=l)
    {
        sum = find2(k<<1|1,l,r);
    }else
    {
        sum = find2(k<<1,l,b[k<<1].tr);
        sum +=find2(k<<1|1,b[k<<1|1].tl,r);
    }
    pushup2(k);
    return sum;
}
void update2(int k,int l,int val)
{
    if(b[k].tl ==l&&b[k].tr==l)
    {
        b[k].sum+=val;
        return;
    }
    if(b[k].sum==0)
    {
        b[k<<1].sum = 0;
        b[k<<1|1].sum = 0;
    }
    if(b[k<<1].tr>=l)
    {
       update2(k<<1,l,val);
    }else if(b[k<<1|1].tl<=l)
    {
       update2(k<<1|1,l,val);
    }
    pushup2(k);
}


内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值