牛客 - 云(扫描线)

题目链接:点击查看

题目大意:在第一个象限内有数个矩形,在第三象限内也有数个矩形,现在第一象限内的矩形向下移动,第三象限内的矩形向右移动,所有矩形的移动速度相同,现在问有多少对矩形可以在第四象限内相交

题目分析:如果直接去求解会比较困难,因为所有的矩形都在移动,但我们可以将模型抽取出来,每次移动的相对运动都比较直接,所有的矩形都沿着 y = x 这条直线相对运动,这样一来我们就可以将所有的矩形投影到 y = - x 这条直线上去,然后用扫描线O(n)计算出贡献了

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
     
typedef long long LL;
    
typedef unsigned long long ull;
     
const int inf=0x3f3f3f3f;
     
const int N=1e5+100;
 
struct Node
{
    int x,type,state;
    Node(int x,int state,int type):x(x),state(state),type(type){}
    bool operator<(const Node& a)const
    {
        if(x!=a.x)
            return x<a.x;
        if(state!=a.state)
            return state>a.state;
    }
};
 
vector<Node>node;
 
int cnt[2];
 
int main()
{
//#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
//#endif
//  ios::sync_with_stdio(false);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        node.push_back(Node(a-b,1,0));
        node.push_back(Node(c-d,-1,0));
    }
    for(int i=1;i<=m;i++)
    {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        node.push_back(Node(a-b,1,1));
        node.push_back(Node(c-d,-1,1));
    }
    sort(node.begin(),node.end());
    LL ans=0;
    for(auto it:node)
    {
        cnt[it.type]+=it.state;
        if(it.state==1)
            ans+=cnt[it.type^1];
    }
    printf("%lld\n",ans);
      
        
        
        
        
        
        
        
    return 0;
}
发布了646 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104337952