牛客 - 云(扫描线)
生活随笔
收集整理的這篇文章主要介紹了
牛客 - 云(扫描线)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:在第一個象限內有數個矩形,在第三象限內也有數個矩形,現在第一象限內的矩形向下移動,第三象限內的矩形向右移動,所有矩形的移動速度相同,現在問有多少對矩形可以在第四象限內相交
題目分析:如果直接去求解會比較困難,因為所有的矩形都在移動,但我們可以將模型抽取出來,每次移動的相對運動都比較直接,所有的矩形都沿著 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; }?
總結
以上是生活随笔為你收集整理的牛客 - 云(扫描线)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客 - 汉诺塔(思维+dp)
- 下一篇: 2019ICPC(上海) - Color