zkw好写吗
codeforces果然名不虚传,仔细研读了该篇文章后感觉受益良多!
其实这篇文章探讨的就是zkw,其中的一些写法让我大开眼界,感觉是zkw那篇论文的又一个提升:
- 内存不再是2的幂了,直接就是\(2n\),zkw应该万万没想到吧。
- zkw的PowerPoint下标的处理有点麻烦,左边又要减一,事实上不用这么麻烦。
这里贴两个模板:
单点加,区间求和
题目。
#include <cstdio>
#include <iostream>
using namespace std;
const int MAXN = 50000;
int n;
struct SegmentTree {
int T[MAXN * 2];
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", T + n + i);
for (int i = n - 1; i; i--)
T[i] = T[i << 1] + T[i << 1 ^ 1];
}
void modify(int i, int d) {
i += n - 1;
for (; i; i >>= 1) T[i] += d;
}
int query(int l, int r) {
l += n - 1;
r += n;
int ret = 0;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret += T[l ++];
if (r & 1) ret += T[-- r];
}
return ret;
}
};
SegmentTree T;
int main() {
int cases;
scanf("%d", &cases);
for (int i = 0; i < cases; i++) {
printf("Case %d:\n", i + 1);
T.init();
while (true) {
char s[16];
int a, b;
scanf("%s", s);
if (s[0] == 'E') break;
scanf("%d%d", &a, &b);
if (s[0] == 'Q') {
printf("%d\n", T.query(a, b));
}
else {
if (s[0] == 'S') b = - b;
T.modify(a, b);
}
}
}
return 0;
}
区间加,区间求和
题目。
这个是我以前一直没弄懂的,现在豁然开朗。
#include <cstdio>
const int MAXN = (int) 1e5;
int n, m, h;
long long t[MAXN * 2];
int d[MAXN];
void apply(int p, int value, int k) {
t[p] += (long long) value * k;
if (p < n) d[p] += value;
}
void push(int p) {
for (int s = h - 1, k = 1 << (h - 1); s; s--, k >>= 1) {
int i = p >> s;
if (d[i]) {
apply(i << 1, d[i], k >> 1);
apply(i << 1 | 1, d[i], k >> 1);
d[i] = 0;
}
}
}
void build(int p) {
p >>= 1;
for (int k = 2; p; p >>= 1, k <<= 1) {
t[p] = t[p << 1] + t[p << 1 | 1] + (long long) d[p] * k;
}
}
void modify(int l, int r, int value) {
l += n, r += n;
push(l);
push(r - 1);
int l0 = l, r0 = r;
for (int k = 1; l < r; l >>= 1, r >>= 1, k <<= 1) {
if (l & 1) apply(l++, value, k);
if (r & 1) apply(--r, value, k);
}
build(l0);
build(r0 - 1);
}
long long query(int l, int r) {
l += n, r += n;
long long res = 0;
push(l);
push(r - 1);
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) res += t[l++];
if (r & 1) res += t[--r];
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
while (1 << h <= n) h++;
for (int i = 0; i < n; i++)
scanf("%lld", t + n + i);
for (int i = n - 1; i; i--)
t[i] = t[i << 1] + t[i << 1 | 1];
for (int i = 0; i < m; i++) {
char command;
scanf("\n%c", &command);
if (command == 'C') {
int l, r, value;
scanf("%d%d%d", &l, &r, &value);
l--;
modify(l, r, value);
}
else {
int l, r;
scanf("%d%d", &l, &r);
l--;
printf("%lld\n", query(l, r));
}
}
return 0;
}
zkw好写吗的更多相关文章
- zkw线段树——简单易懂好写好调的线段树
0.简介 zkw线段树是一种非递归线段树,与普通线段树不同的是,它是棵标准的满二叉树,所以遍历过程可全程使用位运算,常数一般比线段树小得多. 1.结构/建树 前面说了,zkw线段树是满二叉树,可是原数 ...
- ZKW线段树
简介 zkw线段树虽然是线段树的另一种写法,但是本质上已经和普通的递归版线段树不一样了,是一种介于树状数组和线段树中间的存在,一些功能上的实现比树状数组多,而且比线段树好写且常数小. 普通线段树采用从 ...
- zkw线段树详解
转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...
- 备战NOIP每周写题记录(一)···不间断更新
※Recorded By ksq2013 //其实这段时间写的题远远大于这篇博文中的内容,只不过那些数以百记的基础题目实在没必要写在blog上; ※week one 2016.7.18 Monday ...
- 写在SDOI2016Round1前的To Do List
理性的整理了一下自己的不足. 计算几何啥都不会,字符串类DP毫无练习,数据结构写的不熟,数论推不出式子,网络流建模常建残: 需要达成的任务: 一.网络流: 熟练网络流的板子(之前一直仰慕zkw费用流, ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- zkw费用流
期末结束,竞赛生活继续开始,先怒刷完寒假作业再说 至于期末考试,数学跪惨,各种哦智障错,还有我初中常用的建系大法居然被自己抛至脑后,看来学的还是不扎实,以后数学要老老实实学.物理被永哥黑了两分,然后很 ...
- [SinGuLaRiTy] ZKW线段树
[SinGuLaRiTy-1007] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. 关于ZKW线段树 Zkw线段树是清华大学张昆玮发明非递 ...
- 数据结构3——浅谈zkw线段树
线段树是所有数据结构中,最常用的之一.线段树的功能多样,既可以代替树状数组完成"区间和"查询,也可以完成一些所谓"动态RMQ"(可修改的区间最值问题)的操作.其 ...
随机推荐
- flexbox 伸缩布局盒
Flexbox(伸缩布局盒) 是 CSS3 中一个新的布局模式,为了现代网络中更为复杂的网页需求而设计. Flexbox 由 伸缩容器 和 伸缩项目 组成.通过设置元素的 display 属性为 ...
- 内置的材质包含文件 .cginc
Unity中包含几个文件,可以用于你的Shader程序,里面包含了预定义的变量和辅助函数.使用它需要用 #include指令 CGPROGRAM // ... #include "Unity ...
- BZOJ 1034: [ZJOI2008]泡泡堂BNB( 贪心 )
贪心...用最弱的赢最弱的,用最强的赢最强的,否则用最弱的和最强的比... (贴个官方题解:将双方的选手均按从强到弱排序,然后第一次扫描尽可能用当前剩下的选手中能赢对手当前最强选手中最弱的一个去赢得胜 ...
- Bandwidthd+Postgresql数据库配置笔记
Bandwidthd+Postgresql数据库配置笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/zjianbo/article/detai ...
- Python之三层菜单
三层菜单,根据用户所选数字,进入子菜单.一级一级呈现. menu = { 'Beijing': { "ChaoYang": { "CBD": ['CICC', ...
- JS 深拷贝
使用递归进行深拷贝 http://lingyu.wang/2014/03/20/js-interview-1/ Object.prototype.deepClone = function() { va ...
- 解决64位系统下IIS 8下Asp+Access网站配置
一.IIS7的安装 Windows 中IIS8是默认不安装的,所以在安装完windows 8,之后如果需要安装IIS8的话,就要自己动手了. 安装的步骤为:开始>控制面板>程序>打开 ...
- Maven本地
<localRepository></localRepository>
- C#使用系统的“显示桌面”功能(Shell.Application)
原文 C#使用系统的“显示桌面”功能(Shell.Application) 在 Windows 系统的 任务栏 上的 快速启动栏 里,通常有一个图标 ,点击这个图标,就会切换到桌面.这个图标实际是一 ...
- CI引入外部javascript和css
假定baseurl 为 $config['base_url']='http://localhost/codeigniter/'; 调用 <link rel="stylesheet&qu ...