日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ACM-ICPC 2018 徐州赛区网络预赛G (单调队列)

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ACM-ICPC 2018 徐州赛区网络预赛G (单调队列) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傳送門

題面:

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave (?xx?,?yy?) means the wave is a rectangle whose vertexes are (?00?,?00?), (?xx?,?00?), (?00?,?yy?), (?xx?,?yy?). Every time the wave will wash out the trace of former wave in its range and remain its own trace of (?xx?,?00?) -> (?xx?,?yy?) and (?00?,?yy?) -> (?xx?,?yy?). Now the toad on the coast wants to know the total length of trace on the coast after n waves. It's guaranteed that a wave will not cover the other completely.

Input

The first line is the number of waves?n(n \le 50000)n(n≤50000).

The next?nn?lines,each contains two numbers?xx?yy?,(?0 < x0<x?,?y \le 10000000y≤10000000?),the?ii-th line means the?ii-th second there comes a wave of (?xx?,?yy?), it's guaranteed that when?1 \le i1≤i?,?j \le nj≤n?,x_i \le x_jxi?≤xj??and?y_i \le y_jyi?≤yj??don't set up at the same time.

Output

An Integer stands for the answer.

Hint:

As for the sample input, the answer is?3+3+1+1+1+1=103+3+1+1+1+1=10

樣例輸入復制

3 1 4 4 1 3 3

樣例輸出復制

10

題目來源

ACM-ICPC 2018 徐州賽區網絡預賽

題目描述:

? ??有nn次漲潮和退潮,每次的范圍是個x×yx×y的矩形,求n次漲退潮后,潮水痕跡的長度。?
?不存在此i,j∈[1,n],i≠j,xi≤xj且yi≤yji,j∈[1,n],i≠j,xi≤xj且yi≤yj?

題目分析:

? ? 題目中有一個很重要的條件,就是形成的兩個矩陣必定不能兩兩包含,這個條件就可以省去我們討論很多情況。

? ? 首先,我們將所有點根據x坐標排序,并根據加入的時間分別給他們打上時間標記,可以得到如下圖:

? ? 因為題目中的條件,倘若根據x進行排序,對應的也等價于對y坐標進行排序。我們根據x的大小遍歷所有的點,我們可以發現,當前的點i的x坐標對答案的貢獻為第1個點到第i個點中標記單調遞增的個數size*(xi-xi-1)。y同理。

? ? 因此我們可以考慮用單調隊列去維護每一個點對答案答案的貢獻。而因為正著維護單調隊列比較麻煩,因此我們可以考慮倒著維護單調隊列,最后統計答案即可。時間復雜度O(n)。

代碼:

#include <bits/stdc++.h> #define maxn 50005 using namespace std; typedef long long ll; struct Node{ll x,y;int id,a,b;bool operator<(const Node &b){return x<b.x;} }q[maxn]; deque<int>que; int main() {int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%lld%lld",&q[i].x,&q[i].y);q[i].id=i;}sort(q+1,q+1+n);for(int i=1;i<=n;i++){//因為y的答案是從n開始統計的故此要從1開始維護答案while(!que.empty()&&q[i].id>que.back()) que.pop_back();que.push_back(q[i].id);q[i].a=que.size();}que=deque<int>();for(int i=n;i>=1;i--){//倒著統計x的答案while(!que.empty()&&q[i].id>que.back()) que.pop_back();que.push_back(q[i].id);q[i].b=que.size();}ll res=0,xx=0,yy=0;for(int i=1;i<=n;i++){//統計x的答案res+=(q[i].x-xx)*q[i].b;xx=q[i].x;}for(int i=n;i>=1;i--){//統計y的答案res+=(q[i].y-yy)*q[i].a;yy=q[i].y;}printf("%lld\n",res);return 0; }

?

轉載于:https://www.cnblogs.com/Chen-Jr/p/11007196.html

總結

以上是生活随笔為你收集整理的ACM-ICPC 2018 徐州赛区网络预赛G (单调队列)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。