Codeforces 1132G Greedy Subsequences 线段树

Greedy Subsequences

我们先找到每个点的右边第一个比它大的, 然后从大的往它建边, 然后可以发现这是一棵树。

我们令d[ i ] 为 i 号点往上走最多能走几步, 我们能用线段树维护d 的值。

我们加入点 i 的时候, 我们把它的值设为 d[ fa ] + 1, 我们删除 i 的时候, 把 i 这棵子树中的d 都减 1,

每次询问最大值就好啦。

#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 = 1e6 + 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 n, k, to[N], a[N];
vector<int> G[N];

int in[N], ot[N], idx;
vector<int> ans;

struct SegmentTree {
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
    int mx[N << 2], lazy[N << 2];
    inline void pull(int rt) {
        mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
    }
    inline void push(int rt) {
        if(lazy[rt]) {
            mx[rt << 1] += lazy[rt];
            mx[rt << 1 | 1] += lazy[rt];
            lazy[rt << 1] += lazy[rt];
            lazy[rt << 1 | 1] += lazy[rt];
            lazy[rt] = 0;
        }
    }
    void update(int L, int R, int val, int l, int r, int rt) {
        if(R < l || r < L || R < L) return;
        if(L <= l && r <= R) {
            mx[rt] += val;
            lazy[rt] += val;
            return;
        }
        push(rt);
        int mid = l + r >> 1;
        update(L, R, val, lson);
        update(L, R, val, rson);
        pull(rt);
    }
    void setVal(int p, int val, int l, int r, int rt) {
        if(l == r) {
            mx[rt] = val;
            return;
        }
        push(rt);
        int mid = l + r >> 1;
        if(p <= mid) setVal(p, val, lson);
        else setVal(p, val, rson);
        pull(rt);
    }
    int query(int L, int R, int l, int r, int rt) {
        if(R < l || r < L || R < L) return 0;
        if(L <= l && r <= R) return mx[rt];
        push(rt);
        int mid = l + r >> 1;
        return max(query(L, R, lson), query(L, R, rson));
    }
} Tree;

void dfs(int u) {
    in[u] = ++idx;
    for(auto& v : G[u]) dfs(v);
    ot[u] = idx;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    stack<int> stk;
    for(int i = n; i >= 1; i--) {
        while(SZ(stk) && a[stk.top()] <= a[i]) stk.pop();
        to[i] = SZ(stk) ? stk.top() : n + 1;
        stk.push(i);
        G[to[i]].push_back(i);
    }
    dfs(n + 1);

    for(int i = n; i > n - k; i--) {
        Tree.setVal(in[i], Tree.query(in[to[i]], in[to[i]], 1, n + 1, 1) + 1, 1, n + 1, 1);
    }
    ans.push_back(Tree.mx[1]);
    for(int i = n - k; i >= 1; i--) {
        Tree.update(in[i + k], ot[i + k], -1, 1, n + 1, 1);
        Tree.setVal(in[i], Tree.query(in[to[i]], in[to[i]], 1, n + 1, 1) + 1, 1, n + 1, 1);
        ans.push_back(Tree.mx[1]);
    }
    reverse(ALL(ans));
    for(int i = 0; i < SZ(ans); i++)
        printf("%d%c", ans[i], " \n"[i == SZ(ans) - 1]);
    return 0;
}

/*
*/

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

时间: 2024-08-02 03:47:32

Codeforces 1132G Greedy Subsequences 线段树的相关文章

[Codeforces 1295E]Permutation Separation(线段树+贪心)

[Codeforces 1295E]Permutation Separation(线段树+贪心) 题面 给出一个排列\(p_1,p_2,...p_n\).初始时你需要选择一个位置把排列分成左右两个.然后在两个序列间移动元素使得左边序列的所有元素都比右边的所有元素小.给出每个元素\(p_i\)从一个序列移动到另一个序列的代价\(a_i\). 分析 显然最后得到的序列是小的数在一边,大的数在另一边.设从值为\(i\)的元素处分开之后移动代价为\(ans_i\). 一开始假设所有数都移到右边序列,那么

Codeforces 46D Parking Lot(线段树)

题目链接:Codeforces 46D Parking Lot 题目大意:一个街道,长为N,每辆车停进来的时候必须和前面间隔B米,和后面间隔F米,现在用两种操作,1是停进来一个长为x的车,2是第x辆车开走. 解题思路:区间合并,建一颗长度为N + B + F的线段树,然后每次停车进去的时候都查询x + B + F的区间,然后修改的时候只修改x的长度. #include <cstdio> #include <cstring> #include <algorithm> us

Codeforces 482B Interesting Array(线段树)

题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,现在有M个限制,每个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 否有满足的数列. 解题思路:线段树维护,每条限制等于是对l~r之间的数或上q(取且的性质,相应二进制位一定为1),那么处理完所有的 限制,在进行查询,查询对应每个l~r之间的数取且是否还等于q.所以用线段树维护取且和,修改为或操作. #include <cstdio> #include <c

Codeforces 755D(思维+线段树)

http://codeforces.com/problemset/problem/755/D 从X到X+k点,其实只要求从X到X+k之间的点有多少条线超过X--X+K这条线就行,一开始直接暴力,就时间超时了,而用线段树维护就快多了. 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 #define N 1000010 5 #define INF 0x3f3f3f3f 6 #define lso

Codeforces 777D Hanoi Factory(线段树维护DP)

题目链接 Hanoi Factory 很容易想到这是一个DAG模型,那么状态转移方程就出来了. 但是排序的时候有个小细节:b相同时看a的值. 因为按照惯例,堆塔的时候肯定是内半径大的在下面. 因为N有1e5,那么DP的时候用线段树优化一下,就可以了. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for(int i(a); i <= (b); ++i) 6 7 typedef lo

CodeForces 91B Queue (线段树单点操作)

Description There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age

codeforces 522D. Closest Equals 线段树+离线

题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段树. 如果当前的i等于某个询问的r, 那么就查询, 具体看代码. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <

CodeForces 19D Points(线段树+map)

开始想不通,后来看网上说是set,就有一个想法是对每个x建一个set...然后又想直接建立两重的set就好,最后发现不行,自己想多了...  题意是给你三种操作:add (x y) 平面添加(x y)这个点 remove (x y)平面删除(x y)这个点 find (x y) 查找(x y)这个点严格的右上方中最左边的点,有多个就再找最下方的点,输出 其实想通了还是比较简单的,我的想法就是对于x先排序再对y排序,这样建一颗线段树,用处在于:添加和删除都可以当成单点更新,只需要记录最大值就好.f

Codeforces 460C 二分结果+线段树维护

发现最近碰到好多次二分结果的题目,上次多校也是,被我很机智的快速过了,这个思想确实非常不错.在正面求比较难处理的时候,二分结果再判断是否有效往往柳暗花明. 这个题目给定n个数字的序列,可以操作m次,每次要操作w个连续的数字,每次的操作将使得该段连续数字的数都+1,最后求整个序列最小值的最大值 求最小值最大,明显的二分结果的题目,我一开始还是在ACdream那个群里看到这个题,说是二分+线段树的题目,我就来做了一下..首先二分部分很容易,下界就是初始序列的最小值,上界就是 下界+m,至于怎么判断这