前言

巨佬说:要有线段树,于是蒟蒻就开始打线段树。

蒟蒻只能拿之前0分的板子题开刀了QAQ。

题解

一开始我以为插入操作不带取模,于是打了下面这个弱智玩意

下面的代码是会WA的

#include <cstdio>
#include <algorithm>
#define ll long long using namespace std; ll read(){
ll x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
} ll s[800005];
ll _max[800005], cnt[800005];
int m, d, pos = 1;
const int MAXM = 200001; pair<ll, ll> query(int pos, int l, int r, int x, int y){
if (x <= l && r <= y)
return make_pair(cnt[pos], _max[pos]);
pair<ll, ll> ans = make_pair(-(1ll << 62), -(1ll << 62));
int mid = (l + r) >> 1;
if (x <= mid)
ans = max(ans, query(pos << 1, l, mid, x, y));
if (mid < y)
ans = max(ans, query(pos << 1 | 1, mid + 1, r, x, y));
return ans;
} void add(int pos, int l, int r, int x, ll val){
if (l == r){
s[pos] += val, cnt[pos] += s[pos] / d; s[pos] %= d;
_max[pos] = s[pos];
return ;
}
int mid = (l + r) >> 1;
if (x <= mid)
add(pos << 1, l, mid, x, val);
else if (mid < x)
add(pos << 1 | 1, mid + 1, r, x, val);
s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % d;
if (cnt[pos << 1] > cnt[pos << 1 | 1])
_max[pos] = _max[pos << 1], cnt[pos] = cnt[pos << 1];
else if (cnt[pos << 1] < cnt[pos << 1 | 1])
_max[pos] = _max[pos << 1 | 1], cnt[pos] = cnt[pos << 1 | 1];
else{
cnt[pos] = cnt[pos << 1];
_max[pos] = max(_max[pos << 1], _max[pos << 1 | 1]);
}
} int main(){
m = read(), d = read(); char op[1]; ll t = 0;
while (m--){
scanf("%s", op); int n = read();
if (op[0] == 'Q')
printf("%lld\n", t = query(1, 1, MAXM, pos - n + 1, pos).second);
else if (op[0] == 'A')
add(1, 1, MAXM, ++pos, n + t);
}
return 0;
}

一波上交WA 0。

然后一看不对啊,样例都过不了啊(我自信的没测样例)。

仔细看了一下题目,发现插入操作带取模。QAQ

简直有毒。

然后一遍过...

#include <cstdio>
#include <algorithm>
#define ll long long using namespace std; ll read(){
ll x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
} ll s[800005];
ll _max[800005];
int m, d, pos = 1;
const int MAXM = 200001; ll query(int pos, int l, int r, int x, int y){
if (x <= l && r <= y)
return _max[pos];
ll ans = -(1ll << 62);
int mid = (l + r) >> 1;
if (x <= mid)
ans = max(ans, query(pos << 1, l, mid, x, y));
if (mid < y)
ans = max(ans, query(pos << 1 | 1, mid + 1, r, x, y));
return ans;
} void add(int pos, int l, int r, int x, ll val){
if (l == r){
(s[pos] += val) %= d, _max[pos] = s[pos];
return ;
}
int mid = (l + r) >> 1;
if (x <= mid)
add(pos << 1, l, mid, x, val);
else if (mid < x)
add(pos << 1 | 1, mid + 1, r, x, val);
s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % d;
_max[pos] = max(_max[pos << 1], _max[pos << 1 | 1]);
} int main(){
m = read(), d = read(); char op[1]; ll t = 0;
while (m--){
scanf("%s", op); int n = read();
if (op[0] == 'Q')
printf("%lld\n", t = query(1, 1, MAXM, pos - n + 1, pos));
else if (op[0] == 'A')
add(1, 1, MAXM, ++pos, (n + t) % d);
}
return 0;
}

[JSOI2008]最大数 题解的更多相关文章

  1. BZOJ1012:[JSOI2008]最大数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1012 https://www.luogu.org/problemnew/show/P1198 现在 ...

  2. 【题解】[JSOI2008]最大数

    [题解][P1198 JSOI2008]最大数 正难则反,意想不到. 这道题是动态让你维护一个数列,已经在数列里面的数据不做改变,每次在最后加上一个数,强制在线. 既然正着做很难,考虑如果时间倒流,不 ...

  3. 洛谷P1198 [JSOI2008]最大数

    P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...

  4. [JSOI2008]最大数maxnumber

    [JSOI2008]最大数maxnumber 标签: 线段树 单独队列 题目链接 题解 线段树裸题. 如果一直RE可能是你用的cin/cout. Code #include<cstdio> ...

  5. 洛谷 P1198 [JSOI2008]最大数

    洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...

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

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 4750  Solved: 2145[Submi ...

  7. bzoj1012: [JSOI2008]最大数maxnumber(貌似是道线段树喔)

    1012: [JSOI2008]最大数maxnumber 题目:传送门 题解: 发现自己空了一道水题... 1~210000建线段树,其实就是一道裸题... 单点修改+区间查询...1A~ 代码: # ...

  8. P1198 [JSOI2008]最大数(线段树基础)

    P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: ...

  9. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

随机推荐

  1. Java多线程学习——例子:模拟电影院抢座位

    Cinema——List<Integer>数据结构存储电影院座位 public class Cinema{ private List<Integer> seats; //剩余座 ...

  2. seaborn用heatmap画热度图

    原文链接 https://blog.csdn.net/m0_38103546/article/details/79935671

  3. Go语言入门篇-复合数据类型

    复合数据类型 主要讨论四种类型——数组.slice.map和结构体 数组和结构体聚合类型   --数组和结构体都是有固定内存大小的数据结构 --数组:由同构的元素组成——每个数组元素都是完全相同的类型 ...

  4. (已实践)PLSQL本地还原Oracle数据库dmp文件

    这个方法很烂,导致重装Oracle时候处处出现问题,不建议使用这个方法,除非你以后不再用Oracle这个软件了,这个方法很烂,再评论一下. 第一,启动服务,(如果数据库处于启动状态,那么略过这一步) ...

  5. SwipeRefreshLayout和RecyclerView类

    1 SwipeRefreshLayout和RecyclerView之间的关系 内容栏上下滚动是RecyclerView控制的,只有当内容栏滑动到最顶上时,再也拉不动了的时候,这个时候将动作交给Swip ...

  6. Python模块unittest

    Python自带一个单元测试框架,unittest模块,用它来做单元测试,它里面封装好了一些校验返回结果的方法和一些用例执行前的初始化操作 在说unittest之前,先说几个概念: TestCase: ...

  7. jmeter应用之批量插入数据

    上一篇讲到如何在jmeter中配置并连接使用mysql数据库,这一节主要是讲数据库连接的简单应用——批量插入数据 总体步骤如下: 1)新建线程组和添加JDBC Connection Configura ...

  8. faker数据填充详解

    安装 在laravel中已经自动集成,无需手动安装.如需在其他地方使用,可使用以下命令进行安装. composer require fzaninotto/faker 为Faker指定中文支持 可通过在 ...

  9. HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )

    pog loves szh III Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  10. mongodb启动报错,child process failed, exited with error number 1

    error: child process failed, exited with error number 1 第一次安装mongodb,随后启动一般不会出现上面的错误,出现这种错误的原因一般是mon ...