5333. 【NOIP2017提高A组模拟8.23】大新闻

Description

这里写图片描述

Input

这里写图片描述

Output

这里写图片描述

Sample Input

6 8
2 7 4 3 5 9
3 2 5 3
1
2 4
3 1 4 2
2 6
3 1 7 5
1
3 3 6 4

Sample Output

5
4
6
9

Data Constraint

这里写图片描述

Hint

这里写图片描述

题解

到过来做就是一道裸的可持久化线段树了

代码

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 200010;
const int LG = 33;

#define child int t0, int &t, int l = 1, int r = 1e9
#define mid ((l + r) >> 1)
#define ls a[t0].l, a[t].l, l, mid
#define rs a[t0].r, a[t].r, mid + 1, r

struct NOTE
{
    int l,r,s;
}a[N * 2 * LG];

int root[N * 2];
int tim;
int tot;

int f[N]; 
int n, m, x, y, k, op;

void rev(child)
{
    a[t = ++tot] = a[t0], ++a[t].s;
    if (l == r)
        return;
    if (x <= mid)
        rev(ls);
    else rev(rs);
}

int ask(child)
{
    if (l == r)
        return l;
    int tmp = a[a[t0].l].s - a[a[t].l].s;
    if (k <= tmp)
        return ask(ls);
    return k -= tmp, ask(rs);
}

int main()
{
    freopen("news.in","r",stdin);
    freopen("news.out","w",stdout);

    scanf("%d%d",&n,&m);
    for (int i = 1; i <= n; i++)
        scanf("%d", f + i);
    for (int i = n; i >= 1; i--)
    {
        x = f[i];
        ++tim;
        root[tim] = root[tim - 1];
        rev(root[tim - 1], root[tim]);
    }
    while (m--)
    {
        scanf("%d", &op);
        if (op == 1)
        {
            --tim;
            continue;
        }
        if (op == 2)
        {
            scanf("%d", &x);
            ++tim;
            root[tim] = root[tim - 1];
            rev(root[tim - 1], root[tim]);
            continue;
        }
        scanf("%d%d%d", &x, &y, &k);
        printf("%d\n", ask(root[tim - x + 1], root[tim - y]));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值