1. 程式人生 > >ACM-ICPC 2018 徐州賽區網路預賽G (單調佇列)

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

題目來源

題目描述:

    有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;
}

相關推薦

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 rectan

ACM-ICPC 2018 徐州賽區網路預賽 G. Trace (線段樹維護)

題意 你在海灘上有浪花,所有的浪花都是矩形的,當一個浪花來了之後,就會覆蓋他的前一個浪花,問你最後浪花的周長和 思路 我們從後往前來,這樣的話,我們處理的都是會留下來的,怎樣的會留下來呢? 首先我們先看1號點和2號點,由圖我們可以很明顯的看出1號點會被淹沒,而1`和1“他們不會被淹沒

ACM-ICPC 2018 徐州賽區網路預賽 G. Trace】 離散化+權值線段樹

題目連結 https://nanti.jisuanke.com/t/31459 題意 題意就是按順序給出一些左下角在原點的與坐標軸平行的矩形,題意就是按順序給出一些左下角在原點的與座標軸平行的矩形,題意就是按順序給出一些左下角在原點的與坐標軸平行的矩形, 後來的

ACM-ICPC 2018 徐州賽區網路預賽 Bdp

傳送門 題面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named "Sena" are playing a video game. The game

ACM-ICPC 2018 瀋陽賽區網路預賽Dk短路

傳送門 題面: One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci the father somehow knows it

ACM-ICPC 2018 南京賽區網路預賽 題解未完

目錄 A.An Olympian Math Problem【簽到題】 傳送門 題意: 給你一個n 求S模n的值 題解:推規律 AC_code: #include<bits/stdc++.h> #define ll long long

ACM-ICPC 2018 徐州賽區網路預賽 H. Ryuji doesn't want to study 線段樹維護字首和的字首和

題意 很簡答就是每次查詢 (L,R)的時候 ,查詢 a[L]∗(R−L+1)+a[L+1]∗(R−L−1)+a[L+2]∗(R−L−2)....a[R]∗1 a [

ACM-ICPC 2018 徐州賽區網路預賽 B. BE, GE or NE (記憶化搜尋)

題意 兩個人在玩遊戲,有一個初始的分數,每次輪流玩遊戲有三種操作,當前數字加上A,當前數字減去B,當前數字乘上-1,當最終分數>h 的時候就是good ending,小於l的時候就是bad ending ,其他的都是 Normal Ending。 思路 從第一次操作開始記憶化搜,

ACM-ICPC 2018 徐州賽區網路預賽 Feature Track

Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat movement from a cat video. To do this, he extracts c

[ACM-ICPC 2018 徐州賽區網路預賽] Ryuji doesn't want to study [線段樹]

Problem Describe Ryuji is not a good student, and he doesn’t want to study. But there are n books he should learn, each book has its knowledge

ACM-ICPC 2018 徐州賽區網路預賽 I

Characters with Hash Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encrypted value. For instance, he

ACM-ICPC 2018 徐州賽區網路預賽 K. Morgana Net (矩陣快速冪)

題意 給你一個n*n的矩陣A,和一個m*m的矩陣B(m%2==1) B是卷積核,讓你用B對A做t次卷積運算,並且對於A中的每一個元素計算出來的值要模2,所以A最後會是一個01矩陣。 問你經過t此後,A中有多少個元素=1 1<=t<=1e9,1<=n<=8,

ACM-ICPC 2018 徐州賽區網路預賽 J - Maze Designer

題目連結:https://nanti.jisuanke.com/t/31462 題意: 在一個N*M的空地上,建牆造一個迷宮,使得迷宮的耗費最小,且迷宮中的任意兩點之間只有一條路,題目保證每組資料的迷宮唯一。 輸入迷宮中兩個點的座標,輸出兩點間的距離 思路:任意兩點間只有一條

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

題意: 每一輪有三種操作, 加上a 減去b 或者 取負 當且僅當 a, b, c 不為0時,對應的操作有效; 給出一個上界和一個下界 大於等於上界就是 Good Ending 小於等於下界 就是 Bad Ending 否則就是 Normal Ending 兩個人輪流操作

ACM-ICPC 2018 徐州賽區網路預賽 F. Features Track

Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat movement from a cat video. To do this, he ex

ACM-ICPC 2018 徐州賽區網路預賽 J-Maze Designer

Maze Designer After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle.

ACM-ICPC 2018 徐州賽區網路預賽 C

題目連結:點選這裡 解題思路: 對於這麼一個3*3的格子一共就是有八種選擇,那麼題目要求在這八種的選擇的的值的期望中輸出最大的那個. 那麼我們首先就去固定'*'號我們不知道而M知道的點,將他變為已知的值,然後再去暴力dfs列舉'#'號的值,求出這次所有‘*’號固定的

ACM-ICPC 2018 徐州賽區網路預賽 J

題意: 在一個N*M的空地上,建牆造一個迷宮,使得迷宮的耗費最小,且迷宮中的任意兩點之間只有一條路,題目保證每組資料的迷宮唯一。 輸入迷宮中兩個點的座標,輸出兩點間的距離 思路:任意兩點間只有一條路,顯然是一棵樹。在地圖上建最大生成樹,就可以使得牆的耗

ACM-ICPC 2018 徐州賽區網路預賽 H. Ryuji doesn't want to study 線段樹維護字首和的字首和

題意 很簡答就是每次查詢 (L,R)的時候 ,查詢 a[L]∗(R−L+1)+a[L+1]∗(R−L−1)+a[L+2]∗(R−L−2)....a[R]∗1a[L]∗(R−L+1)+a[L+1]∗(R−L−1)+a[L+2]∗(R−L−2)....a[R]∗1

ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies

題解 題目大意 按照順序分糖果 每個最少分1個 問有多少種情況 根據題意找到規律 答案為2的n-1次方 使用尤拉降冪和快速冪計算 AC程式碼 #include <stdio.h> #include <bits/stdc++.h>