• 转换成一个数在(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: 取石子的更多相关文章

  1. 【poj1067】 取石子游戏

    http://poj.org/problem?id=1067 (题目链接) 题意 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走 ...

  2. HDU 2516 取石子游戏(斐波那契博弈)

    取石子游戏 Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  3. hdu 1527 取石子游戏(Wythoff Game)

    题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...

  4. HDU 2516 取石子游戏(FIB博弈)

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. poj 1067 取石子游戏( 威佐夫博奕)

    题目:http://poj.org/problem?id=1067 题意:有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的 ...

  6. [原博客] POJ 1067 取石子游戏

    题目链接有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者 ...

  7. HDU-1527 取石子游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=1527 交换  :可实现. if( n < m ) { n^=m; m^=n; n^=m; } (三)尼姆博 ...

  8. HDU 2516 取石子游戏 (博弈论)

    取石子游戏 Problem Description 1堆石子有n个,两人轮流取.先取者第1次能够取随意多个,但不能所有取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出" ...

  9. HDU - 4994 Revenge of Nim (取石子游戏)

    Problem Description Nim is a mathematical game of strategy in which two players take turns removing ...

随机推荐

  1. haxe相关的计划安排

    用多少写多少.先从入门开始,再到怎么写DSL解析器.最后自举(只是做个示范而已 在深入haxe的过程中,多少会跟typescript有交集,这些会边走边总结. 因为只是for example,所以这个 ...

  2. laravel 解决session保存不了,取不出的问题

    555  上传服务器的时候 storage下的framework 没有上传啊

  3. L2-007. 家庭房产(并查集)*

    L2-007. 家庭房产 参考博客 #include <iostream> #include <cstdio> #include <cstring> #includ ...

  4. 配置mysql主从步骤

    在公司开发中,有时候为了缓解数据库压力,会把读写分开为两个数据库来操作,读为一个数据库,写为一个数据库,然后两个数据库做同步,这样能明显降低数据库的压力,下面给大家介绍如何进行mysql主从数据库配置 ...

  5. Linux命令行下:把程序放后台执行,以及从后台继续执行程序

    把任务放到后台用 & 和 Ctrl+z 让后台任务从停止状态转为运行状态用 bg %N 把后台任务调回到前台用 fg %N 查看所有任务用jobs

  6. Centos6.5搭建Elasticsearch

    ElasticSearch是基于Lucene的搜索服务.支持分布式多用户能力的全文搜索引擎,提供RESTful web接口.Elasticsearch是用Java开发的,Apache旗下开源项目,支持 ...

  7. linux服务器架设--学习笔记

    PS: Centos是属于红帽子的操作系统

  8. tomcat访问错误调试方法

    生产环境中经常用到tomcat,所以还是要学一下tomcat的排错的 很重要的一点,就是实时查看catalina.out日志 执行tail -f catalina.out就会实时刷新日志了 catal ...

  9. OpenLDAP主从

    yum -y install compat-openldap必须得安装这个   1:在主上       备份         cp /etc/openldap/slapd.conf /etc/open ...

  10. laravel路由别名

    在定义路由时使用数组键 as 指定路由名称: Route::get('user/profile', ['as' => 'profile', function () { // }]); 另外,还可 ...