(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 29295 Accepted: 7902

Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

Output

For each test case write one line on the standard output: 
Test case (case number): (number of crossings)

Sample Input

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5


看似是一道二分图图论的问题,其实经过一些简单的(大雾)变换之后,就可以转化为树状数组来求解
自昨天被辉大神教做人之后,我对树状数组的理解也有了一些加深:0

大体题意:日本有东和西两个岛,从北到南依次标号为1,2,3...然后给出k条边,求出k条边一共有多少交点(注意是两两相交,只要两条线有交点就算一个)。

经过分析之后可以发现如果两条边有交点,就必须满足x1-x2>0&&y1-y2<0或者x1-x2<0&&y1-y2>0;
有两个条件,所以我们可以通过将x排序来变为一个条件:即将所有边按x的大小从小到大排序,然后一边遍历,一边维护树状数组。对于边k,在边1到边k-1中寻找y>yk的边,有几条这样的边,那么对于边k,在1~k-1中就有几个与之相交的边。
一边维护树状数组,一边更新交点的值,遍历结束后就可求出所有交点的数量。

PS:在排序时,如果x1==x2,那么要按y1<y2的顺序来排,不然在计算与较小y值那条边相交边的数量时,会将x与之相等的边也算进去(因为树状数组只存储y的值)。

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#define LL long long
#define inf 0x3f3f3f3f

using namespace std;
int n,m,k;

struct de
{
    int x,y;
    bool operator<(const de &other)const
    {
        if(x==other.x)
            return y<other.y;
        return x<other.x;
    }
};

de qiao[500*1010];
LL run[10100]={0};

int lowbit(int i){return i&(-i);}

void update(int i)
{
    while(i<=m)
    {
        run[i]++;
        i+=lowbit(i);
    }
}

LL sum(int i)
{
    LL s=0;
    while(i>0)
    {
        s+=run[i];
        i-=lowbit(i);
    }
    return s;
}

int main()
{
    int t;
    scanf("%d",&t);
    for(int p=1;p<=t;p++)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(qiao,0,sizeof(qiao));
        memset(run,0,sizeof(run));
        for(int i=1;i<=k;i++)
            scanf("%d%d",&qiao[i].x,&qiao[i].y);
        sort(qiao+1,qiao+1+k);
        LL ans=0;
        for(int i=1;i<=k;i++)
        {
            ans+=sum(m)-sum(qiao[i].y);
            update(qiao[i].y);
        //cout << "ans=" << ans << endl;

        }
        printf("Test case %d: %I64d\n",p,ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/brotherHaiNo1/p/7309154.html

【电能质量扰动】基于ML和DWT的电能质量扰动分类方法研究(Matlab实现)内容概要:本文研究了一种基于机器学习(ML)和离散小波变换(DWT)的电能质量扰动分类方法,并提供了Matlab实现方案。首先利用DWT对电能质量信号进行多尺度分解,提取信号的时频域特征,有效捕捉电压暂降、暂升、中断、谐波、闪变等常见扰动的关键信息;随后结合机器学习分类器(如SVM、BP神经网络等)对提取的特征进行训练与分类,实现对不同类型扰动的自动识别与准确区分。该方法充分发挥DWT在信号去噪与特征提取方面的优势,结合ML强大的模式识别能力,提升了分类精度与鲁棒性,具有较强的实用价值。; 适合人群:电气工程、自动化、电力系统及其自动化等相关专业的研究生、科研人员及从事电能质量监测与分析的工程技术人员;具备一定的信号处理基础和Matlab编程能力者更佳。; 使用场景及目标:①应用于智能电网中的电能质量在线监测系统,实现扰动类型的自动识别;②作为高校或科研机构在信号处理、模式识别、电力系统分析等课程的教学案例或科研实验平台;③目标是提高电能质量扰动分类的准确性与效率,为后续的电能治理与设备保护提供决策依据。; 阅读建议:建议读者结合Matlab代码深入理解DWT的实现过程与特征提取步骤,重点关注小波基选择、分解层数设定及特征向量构造对分类性能的影响,并尝试对比不同机器学习模型的分类效果,以全面掌握该方法的核心技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值