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

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

/*
*/

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

时间: 2024-08-26 00:05:55

Codeforces 780G Andryusha and Nervous Barriers 线段树套set || 线段树套单调栈的相关文章

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,

Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)

#include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];int mx[4000007];int sum[4000007];int head[1000007],to[2000007],nex[2000007];int n,k;int a[10000077];int dfn;int tot;void pushup(int rt){    mx[rt]=max(mx[rt

D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】

Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every

[bzoj3932][CQOI2015]任务查询系统-题解[主席树][权值线段树]

Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei秒后结束(第Si秒和Ei秒任务也在运行 ),其优先级为Pi.同一时间可能有多个任务同时执行,它们的优先级可能相同,也可能不同.调度系统会经常向 查询系统询问,第Xi秒正在运行的任务中,优先级最小的Ki个任务(即将任务按照优先级从小到大排序后取前Ki个 )的优先级之和是多少.特别的,如

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)

题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 1 #include <cstdio> 2 #include

hdu 5412 CRB and Queries(线段树套笛卡尔树 - 动态区间第k大)

题目链接:hdu 5412 CRB and Queries 首先对所有出现过的值排序,建立线段树,每个线段树的节点是一棵笛卡尔树,笛卡尔树记录区间下标值. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; #define lson(x) (x<<1) #define rson(x) ((x<<

hdu5029 树链剖分 + 线段树

将树映射在线段上进行操作 然后每个 重链变成一个连续的区间 #include <iostream> #include <cstdio> #include <string.h> #include <vector> #include <algorithm> #pragma comment(linker,"/STACk:10240000,10240000") using namespace std; const int maxn=1

【bzoj3956】Count 单调栈+可持久化线段树

题目描述 输入 输出 样例输入 3 2 0 2 1 2 1 1 1 3 样例输出 0 3 题解 单调栈+可持久化线段树 本题是 bzoj4826 的弱化版(我为什么做题总喜欢先挑难的做QAQ) $k$对点对$(i,j)$有贡献,当且仅当$a_k=max(a_{i+1},a_{i+2},...,a_{r-1})$,且$a_k<a_i\&\&a_k<a_j$. 那么我们可以使用单调栈求出i左面第一个比它大的位置$lp[i]$,和右面第一个比它大的位置$rp[i]$,那么点对$(lp