Problem B: 取石子
- 转换成一个数在(0,X + Y)的加减问题
- 考虑一种使用线段树处理的方法, 维护前缀最大值, 前缀最小值, 前缀和, 然后查询的时候先询问右区间是否会同时碰到上下界, 会的话左区间无用直接递归右区间, 否则的话递归左区间, 然后右区间只会碰到上边界或者下边界, 分两种情况讨论即可
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define M 500010
#define ls now << 1
#define rs now << 1 | 1
#define lson l, mid, now << 1
#define rson mid + 1, r, now << 1 | 1
#define ll long long
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int n, q, x, y, z;
ll sum[M << 2], minn[M << 2], maxx[M << 2];
void pushup(int now) {
sum[now] = sum[ls] + sum[rs];
minn[now] = min(minn[ls], sum[ls] + minn[rs]);
maxx[now] = max(maxx[ls], sum[ls] + maxx[rs]);
}
void modify(int l, int r, int now, int pl, int v) {
if(l > pl || r < pl) return;
if(l == r) {
sum[now] = v;
minn[now] = min(v, 0);
maxx[now] = max(v, 0);
return;
}
int mid = (l + r) >> 1;
modify(lson, pl, v), modify(rson, pl, v);
pushup(now);
}
ll s;
ll merge(ll v, int now) {
if(v + maxx[now] > s) return s - maxx[now] + sum[now];
if(v + minn[now] < 0) return sum[now] - minn[now];
return v + sum[now];
}
ll query(int l, int r, int now, ll v) {
if(l == r) return max(min(s, sum[now] + v), 0ll);
int mid = (l + r) >> 1;
if(maxx[rs] - minn[rs] > s) return query(rson, 0);
else return merge(query(lson, v), rs);
}
int main() {
// freopen("stone1.in", "r", stdin);
n = read(), q = read();
x = read(), y = read();
for(int i = 1; i <= n; i++) {
z = read();
if(i % 2 == 0) z = -z;
modify(1, n, 1, i, z);
}
while(q--) {
int op = read();
if(op == 1) x = read();
else if(op == 2) y = read();
else {
int pl = read(), v = read();
if(pl % 2 == 0) v = -v;
modify(1, n, 1, pl, v);
}
s = x + y;
cout << query(1, n, 1, x) << "\n";
}
return 0;
}
Problem B: 取石子的更多相关文章
- 【poj1067】 取石子游戏
http://poj.org/problem?id=1067 (题目链接) 题意 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走 ...
- HDU 2516 取石子游戏(斐波那契博弈)
取石子游戏 Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- hdu 1527 取石子游戏(Wythoff Game)
题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...
- HDU 2516 取石子游戏(FIB博弈)
取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- poj 1067 取石子游戏( 威佐夫博奕)
题目:http://poj.org/problem?id=1067 题意:有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的 ...
- [原博客] POJ 1067 取石子游戏
题目链接有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者 ...
- HDU-1527 取石子游戏
http://acm.hdu.edu.cn/showproblem.php?pid=1527 交换 :可实现. if( n < m ) { n^=m; m^=n; n^=m; } (三)尼姆博 ...
- HDU 2516 取石子游戏 (博弈论)
取石子游戏 Problem Description 1堆石子有n个,两人轮流取.先取者第1次能够取随意多个,但不能所有取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出" ...
- HDU - 4994 Revenge of Nim (取石子游戏)
Problem Description Nim is a mathematical game of strategy in which two players take turns removing ...
随机推荐
- 为什么企业需要IT资产管理
为什么企业需要IT资产管理 为什么企业需要IT资产管理?其实这个问题,如同企业为什么要管理一样重要. 企业需要管理.因为有了管理,人.物可尽其用,在不同的位置.角色上发挥作用,然后按照一定的运维规则, ...
- 解决Fiddler查看Post参数中文乱码的问题
解决Fiddler查看Post参数中文乱码的问题 解决方法: 1.win+R 2.打开注册表编辑器:输入regedit +回车+是 3.HKEY_CURRENT_USER\Software\Micro ...
- latex中使用listings显示代码
\documentclass[12pt,a4paper]{article}\usepackage{ctex}\usepackage{listings}\usepackage{xcolor}\begin ...
- Bitmap RGB24 4字节对齐
Bitmap RGB24 4字节对齐 本文中说的图片都是无压缩的彩色Bitmap图片. 最近在一个项目中有一个场景是需要将RGB32或RGB24的Bitmap转换成为RGB565的Bitmap,在RG ...
- linux下的$0-n作用
电面的时候回答上来一部分了....呵呵......总结一下!!! $0 Shell本身的文件名 $1-$n 添加到Shell的各参数值.$1是第1参数.$2是第2参数… $$ Shell本身的PID( ...
- asp.net mvc6+ef框架做的书籍管理项目
效果图: 目录结构: book控制器代码: using System; using System.Collections.Generic; using System.Linq; using Syste ...
- centos7.4 分区 格式化
1. 查看磁盘 [root@yzb-centos72-3 ~]# fdisk -l Disk /dev/vda: 32.2 GB, 32212254720 bytes, 62914560 sector ...
- WKWebView 加载本地HTML随笔
一天的时间 解决两个坑~~ 1.加载不出来本地HTML 的JS CSS 样式,问题是引用到项目中 是用的group 这个是错的 直接上图 就知道了 像上图一样,加入相对路径即可,因为如果使用了gro ...
- yuan的第二次随笔
第一题: 计算两数的和与差 设计思路: 1:看题目:主函数与函数声明,知道它要你干什么 2:理解与分析:在main中,定义两个实数a,b;要你求两数的和与差 3:解答:通过调用函数sum_diff,指 ...
- Jacobi-Anger expansion
[转载请注明出处]http://www.cnblogs.com/mashiqi 2017/06/16 适合于自己的关于Jacobi-Anger expansion的推导方法,这里记下来,方便以后查阅. ...