UVA 12436 - Rip Van Winkle's Code

option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3867&mosmsg=Submission+received+with+ID+14331401" target="_blank" style="">题目链接

题意:区间改动一个加入等差数列,一个把区间设为某个值,然后询问区间和

思路:关键在于等差数列的地方,线段树的每一个结点加入一个首项和公差,因为等差数列加上一个等差数列还是一个等差数列。利用这个性质就能够进行维护了,注意set操作会覆盖掉等差数列的操作

代码:

#include <cstdio>
#include <cstring> #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) typedef long long ll;
const int N = 250005;
int n; struct Node {
ll l, r, a1, d, c, val;
int setc;
} node[N * 4]; void build(ll l, ll r, int x = 0) {
node[x].l = l; node[x].r = r;
node[x].a1 = node[x].d = node[x].val = node[x].setc = 0;
if (l == r) return;
ll mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
} void pushup(int x) {
node[x].val = node[lson(x)].val + node[rson(x)].val;
} void pushdown(int x) {
if (node[x].setc) {
node[lson(x)].c = node[rson(x)].c = node[x].c;
node[lson(x)].val = (node[lson(x)].r - node[lson(x)].l + 1) * node[x].c;
node[rson(x)].val = (node[rson(x)].r - node[rson(x)].l + 1) * node[x].c;
node[lson(x)].setc = node[rson(x)].setc = 1;
node[lson(x)].a1 = node[lson(x)].d = node[rson(x)].a1 = node[rson(x)].d = 0;
node[x].setc = 0;
}
node[lson(x)].a1 += node[x].a1;
node[lson(x)].d += node[x].d;
ll l = node[x].l, r = node[x].r;
ll mid = (l + r) / 2;
ll amid = node[x].a1 + node[x].d * (mid - l + 1);
ll len1 = (mid - l + 1), len2 = (r - mid);
node[lson(x)].val += node[x].a1 * len1 + len1 * (len1 - 1) / 2 * node[x].d;
node[rson(x)].a1 += amid;
node[rson(x)].d += node[x].d;
node[rson(x)].val += amid * len2 + len2 * (len2 - 1) / 2 * node[x].d;
node[x].a1 = node[x].d = 0;
} void A(ll l, ll r, ll d, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
ll st = node[x].l - l + 1;
if (d == -1) st = r - node[x].l + 1;
node[x].a1 += st;
node[x].d += d;
ll len = node[x].r - node[x].l + 1;
node[x].val += st * len + len * (len - 1) / 2 * d;
return;
}
pushdown(x);
ll mid = (node[x].l + node[x].r) / 2;
if (l <= mid) A(l, r, d, lson(x));
if (r > mid) A(l, r, d, rson(x));
pushup(x);
} void C(ll l, ll r, ll c, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
node[x].setc = 1;
node[x].c = c;
node[x].val = (node[x].r - node[x].l + 1) * c;
node[x].a1 = node[x].d = 0;
return;
}
pushdown(x);
ll mid = (node[x].l + node[x].r) / 2;
if (l <= mid) C(l, r, c, lson(x));
if (r > mid) C(l, r, c, rson(x));
pushup(x);
} ll S(ll l, ll r, int x = 0) {
if (node[x].l >= l && node[x].r <= r)
return node[x].val;
pushdown(x);
ll mid = (node[x].l + node[x].r) / 2;
ll ans = 0;
if (l <= mid) ans += S(l, r, lson(x));
if (r > mid) ans += S(l, r, rson(x));
pushup(x);
return ans;
} int main() {
while (~scanf("%d", &n)) {
build(1, 250000);
ll a, b, c;
char Q[2];
while (n--) {
scanf("%s%lld%lld", Q, &a, &b);
if (Q[0] == 'C') scanf("%lld", &c);
if (Q[0] == 'A') A(a, b, 1);
if (Q[0] == 'B') A(a, b, -1);
if (Q[0] == 'C') C(a, b, c);
if (Q[0] == 'S') printf("%lld\n", S(a, b));
}
}
return 0;
}

UVA 12436 - Rip Van Winkle&#39;s Code(线段树)的更多相关文章

  1. Uva 12436 Rip Van Winkle&#39;s Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  2. Uva 12436 Rip Van Winkle's Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  3. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  4. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  5. UVA 12501 Bulky process of bulk reduction ——(线段树成段更新)

    和普通的线段树不同的是,查询x~y的话,给出的答案是第一个值的一倍加上第二个值的两倍一直到第n个值的n倍. 思路的话,就是关于query和pushup的方法.用一个新的变量sum记录一下这个区间里面按 ...

  6. UVA-12436 Rip Van Winkle's Code (线段树区间更新)

    题目大意:一个数组,四种操作: long long data[250001]; void A( int st, int nd ) { for( int i = st; i <= nd; i++ ...

  7. POJ训练计划2528_Mayor&#39;s posters(线段树/成段更新+离散化)

    解题报告 id=2528">地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000] ...

  8. Uva 12299 带循环移动的RMQ(线段树)

    题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...

  9. UVa 1471 Defense Lines - 线段树 - 离散化

    题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...

随机推荐

  1. Linux - seq 预设外部命令

    seq 是Linux 中一个预设的外部命令,一般用作一堆数字的简化写法. 常用参数: # 不指定起始数值,则默认为 1 -s # 选项主要改变输出的分格符, 预设是 \n -w # 等位补全,就是宽度 ...

  2. 【洛谷 P2726】 [SHOI2005]树的双中心(树的重心)

    先考虑一个\(O(N^2)\)做法. 设选的两个点为\(x,y\),则一定可以将树分成两个集合\(A,B\),使得\(A\)集合所有点都去\(x\),\(B\)集合所有点都去\(y\),而这两个集合的 ...

  3. 分模块开发创建Action子模块——(九)

    web层选择war打包方式. 1.右击父工程新建maven模块

  4. Nginx是什么,有什么优点?为什么选择Nginx做web服务器软件?(经典经典)

    1.基础知识 代理服务器:    一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器.    一个完整的代理请求过程为:客 ...

  5. Ansible Tower系列 四(使用tower执行一个命令)【转】

    在主机清单页面中,选择一个主机清单,进入后,选择hosts里的主机 Paste_Image.png 点击 RUN COMMANDS MODULE 选择 commandARGUMENTS 填写 ifco ...

  6. mysql 获取当月日期天数

    本月总共天数:SELECT  TIMESTAMPDIFF(day,CURDATE(),(DATE_add(CURDATE(),INTERVAL 1 month)))

  7. JDK7的新玩具java.util.Objects

    空指针异常这个坑爹的异常是初学开发者最常见的坑,那么今天为大家分享一个jdk的新工具java.util.Objects包.里面有很多工具可以帮我们避免空指针异常让我我们的代码写起来更加优雅.下面我们来 ...

  8. Linux学习笔记:sed删除、插入数据

    一.sed删除文件第一行 sed -i '1d' file.txt -- 删除第一行 sed -i 'nd' file.txt -- 删除第n行 sed -i '$d' file.txt -- 删除最 ...

  9. darknet

    darknet第二作者:https://github.com//AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects yolov3实现 ...

  10. 《剑指offer》写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

    弱菜刷题还是刷中文题好了,没必要和英文过不去,现在的重点是基本代码能力的恢复. [题目] 剑指offer 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. [思路] 直觉 ...