前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】

BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】

作者头像
Angel_Kitty
发布2018-04-09 14:58:20
5690
发布2018-04-09 14:58:20
举报

1012: [JSOI2008]最大数maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB

Submit: 10374  Solved: 4535

[Submit][Status][Discuss]

Description

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

Input

  第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足D在longint内。接下来 M行,查询操作或者插入操作。

Output

  对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。

Sample Input

5 100 A 96 Q 1 A 97 Q 1 Q 2

Sample Output

96 93 96

HINT

  数据如下http://pan.baidu.com/s/1i4JxCH3

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012

分析:线段树,,,,,我可能又是智障了,调了俩个小时,一个update写错一个细节,WA了三发,然后一直Re,mmp,开80W数组都不够?开了个270W的数组才过,这题目有毒!这题其实也很简单,先建一棵树,结点都为空,然后记录下插入的数的个数为cnt,逐个插入,每次询问cnt-l+1到cnt的最大值即可!

下面给出AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=300020;
 5 #define inf 0x7fffffff
 6 inline int read()
 7 {
 8     int x=0,f=1;
 9     char ch=getchar();
10     while(ch<'0'||ch>'9')
11     {
12         if(ch=='-')
13             f=-1;
14         ch=getchar();
15     }
16     while(ch>='0'&&ch<='9')
17     {
18         x=x*10+ch-'0';
19         ch=getchar();
20     }
21     return x*f;
22 }
23 inline void write(int x)
24 {
25     if(x<0)
26     {
27         putchar('-');
28         x=-x;
29     }
30     if(x>9)
31         write(x/10);
32     putchar(x%10+'0');
33 }
34 struct Tree
35 {
36     int L,R,maxn;
37 }tree[N<<3];
38 inline void buildtree(int l,int r,int pos)
39 {
40     tree[pos].L=l;
41     tree[pos].R=r;
42     tree[pos].maxn=-inf;
43     if(l==r)
44         return;
45     int mid=(l+r)/2;
46     buildtree(l,mid,pos*2);
47     buildtree(mid+1,r,pos*2+1);
48 }
49 inline int Query(int l,int r,int pos)
50 {
51     if(tree[pos].L==l&&tree[pos].R==r)
52         return tree[pos].maxn;
53     int mid=(tree[pos].L+tree[pos].R)/2;
54     if(r<=mid)
55         return Query(l,r,pos*2);
56     else if(l>mid)
57         return Query(l,r,pos*2+1);
58     else return max(Query(l,mid,pos*2),Query(mid+1,r,pos*2+1));
59 }
60 inline void update(int l,int r,int pos)
61 {
62     if(tree[pos].L==tree[pos].R)
63     {
64         tree[pos].maxn=r;
65         return;
66     }
67     int mid=(tree[pos].L+tree[pos].R)/2;
68     if(l<=mid)
69         update(l,r,pos*2);
70     else update(l,r,pos*2+1);
71     tree[pos].maxn=max(tree[pos*2].maxn,tree[pos*2+1].maxn);
72 }
73 int m,mod,last,cnt;
74 int main()
75 {
76     m=read();
77     mod=read();
78     buildtree(1,m,1);
79     for(int i=1;i<=m;i++)
80     {
81         char ch[5];
82         scanf("%s",ch);
83         int x;
84         if(ch[0]=='A')
85         {
86             cnt++;
87             x=read();
88             x=(x+last)%mod;
89             update(cnt,x,1);
90         }
91         else
92         {
93             x=read();
94             last=Query(cnt-x+1,cnt,1);
95             printf("%d\n",last);
96         }
97     }
98     return 0;
99 }

似乎可以用单调队列写?

贴个AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=200001;
 4 int m,d,a[maxn<<3],t,maxx[maxn<<3],l,p;
 5 char q[1];
 6 int main()
 7 {
 8     scanf("%d%d",&m,&d);
 9     while(m--)
10     {
11         scanf("%s%d",q,&p);
12         if(q[0]=='A')
13         {
14             a[++t]=(l+p)%d;
15             for(int i=t;i;i--)
16             {
17                 if(maxx[i]<a[t])
18                     maxx[i]=a[t];
19                 else break;
20             }
21         }
22         else printf("%d\n",l=maxx[t-p+1]);
23     }
24     return 0;
25 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1012: [JSOI2008]最大数maxnumber
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档