hdu5862树状数组 离散化

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <map>
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
struct node
{
    int type,x,y,y2;
} nod[maxn*2];
int a[maxn*2];
int Maxn;
bool cmp(node a,node b)
{
    if(a.x==b.x)return a.type<b.type;
    return a.x<b.x;
}
int c[maxn*2];
int lowbit(int i)
{
    return i&(-i);
}
void add(int i,int d)
{
    while(i<=Maxn)
    {
        c[i]+=d;
        i+=lowbit(i);
    }
}
int sum(int i)
{
    int ans=0;
    while(i>0)
    {
        ans+=c[i];
        i-=lowbit(i);
    }
    return ans;
}
map<int,int>mp;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        mp.clear();
        int n,tot=0;
        int all=1;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            int x1,x2,y1,y2;
            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
            if(x1==x2)//竖线
            {
                if(y1>y2)swap(y1,y2);
                nod[tot++]= {1,x1,y1,y2};
                a[all++]=y1;
                a[all++]=y2;
            }
            else
            {
                if(x1>x2)swap(x1,x2);
                nod[tot++]= {0,x1,y1,1};
                nod[tot++]= {0,x2+1,y2,-1};
                a[all++]=y1;
            }
        }
        sort(a+1,a+all);
        int cnt=0;
        for(int i=1; i<=all; i++)//y轴离散化
        {
            if(!mp[a[i]])mp[a[i]]=++cnt;
        }
        Maxn=cnt+1;
        sort(nod,nod+tot,cmp);
        ll ans=0;
        cle(c);
        for(int i=0; i<tot; i++)
        {
            if(nod[i].type==0)
            {
                int xpy=mp[nod[i].y];
                add(xpy,nod[i].y2);
            }
            else
            {
                int xpl=mp[nod[i].y];
                int xpr=mp[nod[i].y2];
                ans+=sum(xpr)-sum(xpl-1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值