zoukankan      html  css  js  c++  java
  • bzoj 1012 最大数

    题目大意:

    一个空队列,两种操作

    1.查询数列内末尾l个数的最大值

    2.每次在数列末尾插入一个数,该数为输入的数和上一次查询的值之和对固定常数取模

    思路:

    很明显线段树

    维护一个就好了

    中间写错好多次

    还需多练

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstdlib>
     6 #include<cstring>
     7 #include<vector>
     8 #include<queue>
     9 #define ll long long
    10 #define MAXN 801010
    11 #define inf 2147483611
    12 using namespace std;
    13 ll read()
    14 {
    15     ll x=0,f=1;
    16     char ch=getchar();
    17     while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    18     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    19     return x*f;
    20 }
    21 struct data
    22 {
    23     ll k,l,r,maxn;
    24 }tr[MAXN];
    25 ll n,mod,len,t;
    26 void build(ll k,ll l,ll r)
    27 {
    28     if(l==r) {tr[k].l=tr[k].r=r;tr[k].maxn=-inf;return;}
    29     ll m=(l+r)>>1;
    30     tr[k].l=l;tr[k].r=r;
    31     tr[k].maxn=-inf;
    32     build(k<<1,l,m);
    33     build(k<<1|1,m+1,r);
    34 }
    35 void add(ll k,ll pos,ll a)
    36 {
    37     ll l=tr[k].l,r=tr[k].r;
    38     if(l==r) {tr[k].maxn=a;return ;}
    39     ll m=(l+r)>>1;
    40     if(pos<=m) add(k<<1,pos,a);
    41     else add(k<<1|1,pos,a);
    42     tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    43 }
    44 ll query(ll k,ll a,ll b)
    45 {
    46     ll l=tr[k].l,r=tr[k].r;
    47     if(l==a&&r==b) return tr[k].maxn;
    48     ll m=(l+r)>>1;
    49     if(b<=m) return query(k<<1,a,b);
    50     else if(a>m) return query(k<<1|1,a,b);
    51     else return max(query(k<<1,a,m),query(k<<1|1,m+1,b));
    52 }
    53 int main()
    54 {
    55     n=read();mod=read();
    56     char ch[5];ll a;
    57     build(1,1,n);
    58     len=t=0;
    59     while(n--)
    60     {
    61         scanf("%s",ch);
    62         a=read();
    63         if(ch[0]=='A') {len++;add(1,len,(a+t)%mod);}
    64         if(ch[0]=='Q')
    65         {
    66             t=query(1,len-a+1,len);
    67             printf("%lld
    ",t);
    68         }
    69     }
    70 }
    View Code

     upd:2018.2.2

    单调栈

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 #include<map>
    10 #define inf 2139062143
    11 #define ll long long
    12 #define MAXN 200100
    13 using namespace std;
    14 inline ll read()
    15 {
    16     ll x=0,f=1;char ch=getchar();
    17     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    18     while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    19     return x*f;
    20 }
    21 ll n,st[MAXN],pos[MAXN],tp,p,L,t;
    22 ll tmp;
    23 int main()
    24 {
    25     n=read(),p=read();
    26     char ch[4];ll x;
    27     while(n--)
    28     {
    29         scanf("%s %lld",ch,&x);
    30         if(ch[0]=='A')
    31         {
    32             (x+=t)%=p;
    33             while(st[tp]<=x&&tp) tp--;
    34             st[++tp]=x,pos[tp]=++L;
    35         }
    36         else 
    37         {
    38             ll k=lower_bound(pos+1,pos+tp+1,L-x+1)-pos;
    39             printf("%lld
    ",t=st[k]);
    40         }
    41     }
    42 }
    View Code
  • 相关阅读:
    shell编程-项目部署(优化篇)
    数据库相关
    python基础面试
    scrapy爬取数据进行数据库存储和本地存储
    C# 对字符串操 替换数字 替换非数字 去除首尾字符 长沙
    还在为删除集合中的相同项而烦恼吗?
    C#之Task&匿名方法
    如何在火狐里面实现如下功能
    valueOf和toString曾经欺骗过你吗?
    JS 实现Json查询方法
  • 原文地址:https://www.cnblogs.com/yyc-jack-0920/p/7615783.html
Copyright © 2011-2022 走看看