zoukankan      html  css  js  c++  java
  • 洛谷 P1198 [JSOI2008]最大数

    P1198 [JSOI2008]最大数

    题目描述

    现在请求你维护一个数列,要求提供以下两种操作:

    1、 查询操作。

    语法:Q L

    功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。

    限制:L不超过当前数列的长度。

    2、 插入操作。

    语法:A n

    功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。

    限制:n是整数(可能为负数)并且在长整范围内。

    注意:初始时数列是空的,没有一个数。

    输入输出格式

    输入格式:

     

    第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足(0<D<2,000,000,000)

    接下来的M行,每行一个字符串,描述一个具体的操作。语法如上文所述。

     

    输出格式:

     

    对于每一个查询操作,你应该按照顺序依次输出结果,每个结果占一行。

     

    输入输出样例

    输入样例#1:
    5 100
    A 96
    Q 1
    A 97
    Q 1
    Q 2
    
    输出样例#1:
    96
    93
    96
    

    说明

    [JSOI2008]

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<stack>
     5 using namespace std;
     6 const int M=200005,INF=2000000000;
     7 int m,d,len,x,last_query;
     8 char cmd;
     9 struct node{
    10     int l,r,mx;
    11 }t[M*5];
    12 void build(int now,int l,int r){
    13     t[now].l=l,t[now].r=r;t[now].mx=-INF;
    14     if(l==r)return;
    15     int mid=(l+r)/2;
    16     build(now*2,l,mid);
    17     build(now*2+1,mid+1,r);
    18 }
    19 int query(int now,int l,int r){
    20     if(t[now].l>r||t[now].r<l)return -INF;
    21     if(t[now].l>=l&&t[now].r<=r)return t[now].mx;
    22     return max(query(now*2,l,r),query(now*2+1,l,r));
    23 }
    24 void update(int u,int x,int k){
    25     if(x>=t[u].l&&x<=t[u].r)t[u].mx=max(t[u].mx,k);
    26     else return;
    27     update(u*2,x,k);
    28     update(u*2+1,x,k);
    29 }
    30 int main()
    31 {
    32     scanf("%d %d
    ",&m,&d);
    33     build(1,1,m);
    34     for(int i=1;i<=m;i++){
    35         cin>>cmd;scanf("%d",&x);
    36         if(cmd=='Q')printf("%d
    ",last_query=query(1,len-x+1,len));
    37         // 从倒数 x 中 查找
    38         if(cmd=='A') update(1,++len,(x+last_query)%d);
    39     }
    40     return 0;
    41 }

    暴力超时稳稳地,注意线段树多开四到五倍空间、、、头一次做省选题、、虽然、、、

  • 相关阅读:
    程序员:不要自称为码农
    SpringBoot对静态资源配置
    LeetCode 572. Subtree of Another Tree(子树)
    LeetCode 437. Path Sum III(统计路径和等于sum的路径数量)
    LeetCode 112. Path Sum(判断路径和是否等于一个数)
    LeetCode 617. Merge Two Binary Trees(归并两棵二叉树)
    LeetCode 226. Invert Binary Tree(翻转二叉树)
    Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 的解决办法
    linux-查询某软件的安装的目录
    WebService概念解释
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6426183.html
Copyright © 2011-2022 走看看