码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 780G Andryusha and Nervous Barriers 线段树套set || 线段树套单调栈

时间:2019-06-15 10:00:53      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:while   tps   with   name   cto   void   return   需要   add   

Andryusha and Nervous Barriers

问题本质我们只需要找出每个线段两端下面第一个碰到的是哪个线段就好啦。

按照 h 排序我们就能用线段树套set 不管维护什么都能维护出这个东西,但是 如果set里维护 u + s的话,就能优化成单调栈, 好优秀啊。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int h, w, n;
int toL[N], toR[N];
int to[N];
int hs[N];

set<int> start;
set<PII> seg;

int dp[N];

struct Node {
    int u, l, r, s;
    bool operator < (const Node& rhs) const {
        return u < rhs.u;
    }
    void read() {
        scanf("%d%d%d%d", &u, &l, &r, &s);
    }
} a[N];

vector<PII> vc;
struct segmentTree {
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
    set<PII> a[N << 2];
    void Insert(int p, PII v, int l, int r, int rt) {
        a[rt].insert(v);
        if(l == r) return;
        int mid = l + r >> 1;
        if(p <= mid) Insert(p, v, lson);
        else Insert(p, v, rson);
    }
    void Delete(int p, PII v, int l, int r, int rt) {
        a[rt].erase(v);
        if(l == r) return;
        int mid = l + r >> 1;
        if(p <= mid) Delete(p, v, lson);
        else Delete(p, v, rson);
    }
    void query(int L, int R, int x, int y, int l, int r, int rt) {
        if(R < l || r < L || R < L) return;
        if(L <= l && r <= R) {
            auto it = a[rt].lower_bound(mk(x, -inf));
            while(it != a[rt].end() && it->fi <= y) {
                vc.push_back(*it);
                ++it;
            }
            return;
        }
        int mid = l + r >> 1;
        query(L, R, x, y, lson);
        query(L, R, x, y, rson);
    }
} Tree;

int main() {
    scanf("%d%d%d", &h, &w, &n);
    for(int i = 1; i <= w; i++) start.insert(i);
    for(int i = 1; i <= n; i++) a[i].read();
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++) hs[i] = a[i].u;
    for(int i = n; i >= 1; i--) {
        if(a[i].u + a[i].s < h + 1) continue;
        while(1) {
            auto it = start.lower_bound(a[i].l);
            if(it == start.end() || *it > a[i].r) break;
            to[*it] = i;
            start.erase(it);
        }
    }
    for(int i = n; i >= 1; i--) {
        int p = upper_bound(hs + 1, hs + 1 + n, a[i].u + a[i].s) - hs - 1;
        Tree.query(i + 1, p, a[i].l, a[i].r, 1, n, 1);
        for(auto &t : vc) {
            if(t.se < 0) toL[-t.se] = i;
            else toR[t.se] = i;
            Tree.Delete(abs(t.se), t, 1, n, 1);
        }
        if(a[i].l - 1 > 0) Tree.Insert(i, mk(a[i].l - 1, -i), 1, n, 1);
        if(a[i].r + 1 <= w) Tree.Insert(i, mk(a[i].r + 1, i), 1, n, 1);
        vc.clear();
    }
    for(int i = 1; i <= n; i++) {
        if(a[i].l == 1) toL[i] = toR[i];
        if(a[i].r == w) toR[i] = toL[i];
    }
    dp[0] = 1;
    for(int i = 1; i <= n; i++) {
        dp[i] = (dp[toL[i]] + dp[toR[i]]) % mod;
    }
    int ans = 0;
    for(int i = 1; i <= w; i++) add(ans, dp[to[i]]);
    printf("%d\n", ans);
    return 0;
}

/*
*/

 

Codeforces 780G Andryusha and Nervous Barriers 线段树套set || 线段树套单调栈

标签:while   tps   with   name   cto   void   return   需要   add   

原文地址:https://www.cnblogs.com/CJLHY/p/11026021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!