前言

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

蒟蒻只能拿之前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. Scratch少儿编程系列:(二)界面介绍及相关概念

    本系列后续所有Scratch的讲解均基于2.0版本介绍.系统启动后,界面如下: Scratch主要包括6个区域: 1. 菜单:新建.打开.保存 Scratch文件,2.0版本文件后缀名为 .sb2 2 ...

  2. 【Qt开发】解决Qt5.7.0中文显示乱码的问题

    [Qt开发]解决Qt5.7.0中文显示乱码的问题 亲测可用: 乱码主要是编码格式的问题,这里可以通过Edit菜单中选择当前文档的编码方式,选择按照UTF-8格式保存,然后输入对应的中文,保存,然后运行 ...

  3. 《深入浅出WPF》学习总结之控件与布局

    一.控件到底是什么 控件的本质是“数据+算法”——用户输入原始数据,算法处理原始数据并得到结果数据.问题就在于程序如何将结果数据展示给用户.同样一组数据,你可以使用LED阵列显示出来,或者是以命令行模 ...

  4. TortoiseGit不用每次输入用户名和密码的方法

    TortoiseGit每次同步代码时,都会让输入用户名和密码,虽然安全,但是自己用有点麻烦. 怎么解决呢?废话不多说,直接上图: 1.设置—编辑本地.git/config 2.在本地.git/conf ...

  5. 块设备驱动——ramblock

    一. 什么是块设备. 1.1. 一种具有一定结构的随机存取设备,对这种设备的读写是按块进行的,他使用缓冲区来存放暂时的数据,待条件成熟后,从缓存一次性写入设备或者从设备一次性读到缓冲区.可以随机访问, ...

  6. homestead修改php版本

    登录后 如果之前没有设置过root密码 sudo passwd root 以root 权限执行如下命令,选择对应php版本 # 查看所有 php 版本和当前版本 update-alternatives ...

  7. 深度学习之depthwise separable convolution,计算量及参数量

    目录: 1.什么是depthwise separable convolution? 2.分析计算量.flops 3.参数量 4.与传统卷积比较 5.reference

  8. 56. Merge Intervals (JAVA)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  9. rest_framework框架的认证、权限

    REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.TokenAuth", ] ...

  10. Java并发——线程间通信与同步技术

    传统的线程间通信与同步技术为Object上的wait().notify().notifyAll()等方法,Java在显示锁上增加了Condition对象,该对象也可以实现线程间通信与同步.本文会介绍有 ...