1012: [JSOI2008]最大数maxnumber 线段树

https://www.lydsy.com/JudgeOnline/problem.php?id=1012

现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加上t其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。

强制在线,线段树更新即可

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=200000+10,maxn=8000000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;

struct segtree{
    ll ma[N<<2];
    void build(int l,int r,int rt)
    {
        ma[rt]=-1;
        if(l==r)return ;
        int m=(l+r)>>1;
        build(ls);build(rs);
    }
    void update(int pos,ll x,int l,int r,int rt)
    {
        if(l==r)
        {
            ma[rt]=x;
            return ;
        }
        int m=(l+r)>>1;
        if(pos<=m)update(pos,x,ls);
        else update(pos,x,rs);
        ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);
    }
    ll query(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)return ma[rt];
        int m=(l+r)>>1;
        ll ans=-1;
        if(L<=m)ans=max(ans,query(L,R,ls));
        if(m<R)ans=max(ans,query(L,R,rs));
        return ans;
    }
}tree;
int main()
{
    ll m,d;scanf("%lld%lld",&m,&d);
    int n=200000;
    tree.build(1,n,1);
    ll now=0,ans=0;
    while(m--)
    {
        char op[5];ll x;
        scanf("%s%lld",op,&x);
        if(op[0]==‘A‘)
        {
            ll te=(x+ans)%d;
            now++;
            tree.update(now,te,1,n,1);
        }
        else
        {
            ans=tree.query(now-x+1,now,1,n,1);
            printf("%lld\n",ans);
        }
    }
    return 0;
}
/********************

********************/

原文地址:https://www.cnblogs.com/acjiumeng/p/8808859.html

时间: 2024-08-29 02:33:18

1012: [JSOI2008]最大数maxnumber 线段树的相关文章

bzoj-1012 1012: [JSOI2008]最大数maxnumber(线段树)

题目链接: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MB Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得

BZOJ 1012: [JSOI2008]最大数maxnumber(线段树)

裸的线段树...因为数组开小了而一直RE..浪费了好多时间.. -------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<cctype> #include<iostream> #define rep(i,n) for(int i=

【BZOJ】1012: [JSOI2008]最大数maxnumber(树状数组+区间最值)

http://www.lydsy.com/JudgeOnline/problem.php?id=1012 树状数组原来我只懂得sum和add的操作,今天才知道可以有求区间最值的操作,我学习了一下写了个,1a了. 区间最值其实和区间求和差不多,就是将sum数组的含义转移到max,然后通过特定的区间更新max. 在区间求和中,当我们维护max[i]的时候,要找到它前面所有的max[j]来更新,在这里因为是树状数组,所以可以降成一个log级,画图可知,max[i]需要的max只有max[i-2^0],

BZOJ1012 JSOI2008 最大数maxnumber 线段树/栈+二分法

题意:给定一个数列,要求维护:1.求倒数L个数中的最大值  2.在数列末尾插入(最近的1询问的答案+x)%D.其中初始序列为空. 法一:因为询问最多200000个,所以直接建一棵大小为M的线段树维护即可 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; struc

[BZOJ1012][JSOI2008]最大数maxnumber 线段树

没什么好说的--线段树维护区间就行了.第一次居然写错了,真丢人. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 typedef long long ll; 6 ll inline readll(){ 7 ll Num;char ch; 8 while((ch=getchar())<'0'||ch>'9');Num=ch-'0'; 9

BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树/树状数组/乱搞

1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 4750  Solved: 2145[Submit][Status][Discuss] Description 现 在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一

1012: [JSOI2008]最大数maxnumber

1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 4435  Solved: 2000[Submit][Status] Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如

BZOJ 1012: [JSOI2008]最大数maxnumber 单调栈

单调栈 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MB Submit: 4988  Solved: 2252 [Submit][Status][Discuss] Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. 插入操作.语法:A n 功能:将n加上t,其中

BZOJ 1012: [JSOI2008]最大数maxnumber(线段树)

012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MB Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列