[JSOI2008]最大数 题解
前言
巨佬说:要有线段树,于是蒟蒻就开始打线段树。
蒟蒻只能拿之前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]最大数 题解的更多相关文章
- BZOJ1012:[JSOI2008]最大数——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1012 https://www.luogu.org/problemnew/show/P1198 现在 ...
- 【题解】[JSOI2008]最大数
[题解][P1198 JSOI2008]最大数 正难则反,意想不到. 这道题是动态让你维护一个数列,已经在数列里面的数据不做改变,每次在最后加上一个数,强制在线. 既然正着做很难,考虑如果时间倒流,不 ...
- 洛谷P1198 [JSOI2008]最大数
P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...
- [JSOI2008]最大数maxnumber
[JSOI2008]最大数maxnumber 标签: 线段树 单独队列 题目链接 题解 线段树裸题. 如果一直RE可能是你用的cin/cout. Code #include<cstdio> ...
- 洛谷 P1198 [JSOI2008]最大数
洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树/树状数组/乱搞
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 4750 Solved: 2145[Submi ...
- bzoj1012: [JSOI2008]最大数maxnumber(貌似是道线段树喔)
1012: [JSOI2008]最大数maxnumber 题目:传送门 题解: 发现自己空了一道水题... 1~210000建线段树,其实就是一道裸题... 单点修改+区间查询...1A~ 代码: # ...
- P1198 [JSOI2008]最大数(线段树基础)
P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
随机推荐
- python3+selenium常用语法汇总
Selenium常用语法总结 一.Selenium常用定位语法 1.元素定位 (1)ID定位元素: find_element_by_id(‘’) (2)通过元素的类名称定位元素: find_eleme ...
- 【MM系列】SAP 财务帐与后勤不一致情况
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 财务帐与后勤不一致情况 ...
- Spoj 2798 Qtree3
一棵结点为黑色或白色的树,初始都是白色的.有两种操作 1 将一个结点换颜色 2 询问从根到结点u路径上面的第一个黑色点,没有则输出-1 InputIn the first line there are ...
- P1233木棍加工
这个题被算法标签标为DP,但其实可能只是用dp求子序列,,(n方) 给出l与w,只要是l与w同时满足小于一个l与w,那么这个木棍不需要时间,反之需要1.看到这个题,首先想到了二维背包,然后发现没有最大 ...
- /cat/cpuinfo信息查看
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...
- Neo4j清空所有数据
两种方法: 一.用下列 Cypher 语句: match (n) detach delete n 二. 1.停掉服务: 2.删除 graph.db 目录: 3.重启服务. 原文地址:http://ne ...
- getchar、putchar、puts、gets
getchar(字符) 输入获取一个字符 putchar(字符) 输出控制台一个字符 scanf()格式化输入 printf() 格式化输出 gets(arr) 输入一个字符串给已经声明的数组ar ...
- how to install protobuff python
当前环境: operate system: Ubuntu 14.04.1 LTS protoc --version: libprotoc 2.5.0 protocol-buffers versi ...
- SPSS Statistics 多个版本的下载安装激活步骤
SPSS 23:https://www.cnblogs.com/coco56/p/11648386.html SPSS25:https://www.cnblogs.com/coco56/p/11648 ...
- 计蒜客 蓝桥模拟 I. 天上的星星
计算二维前缀和,节省时间.容斥定理. 代码: #include <cstdio> #include <cstdlib> #include <cstring> #in ...