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. supperset (python 2.7.12 + mysql)记录

    网上看到superset,比较感兴趣,虚机上搭一下,记录操作过程. 版本信息:CentOS 6.6 + python 2.7.12 + mysql 5.1.73 + setuptools 36.5.0 ...

  2. mybatis批量增加与删除——(十五)

    1.首先应该明白,mybatis增删改返回值是int型的影响行数的值 mapper接口 package cn.xm.mapper; import java.util.List; import cn.x ...

  3. centos memcached

    2014年1月19日 16:58:37 memcached 是基于libevent事件监听功能的,所以要安装 libevent 和 libevent-devel 启动命令 ./memcached -d ...

  4. css1-puchong1

    HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. 一:HTML DOM 树 ...

  5. Java基础91 mysql的用户权限设置问题

    1.概述 1)MySQL数据库中root用户具有最高的权限(超级用户),可以对任何数据库,任何表进行操作. 2)权限账户,只拥有部分权限(CRUD) .例如:只能操作某个数据库的某张表等等. 2.my ...

  6. apollo配置相关

    一.运维 1. 数据库ConfigDB,PortalDB 2. 基础服务 :Config-Service,(Meta-Server),Admin-Service,Portal 3. 应用在SIT.UA ...

  7. [转]Mahout推荐算法API详解

    Mahout推荐算法API详解 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeepe ...

  8. js时间格式化函数(兼容IOS)

    * 时间格式化 * @param {Object} dateObj 时间对象 * @param {String} fmt 格式化字符串 */ dateFormat(dateObj, fmt) { le ...

  9. ASP.Net1

    一.Web应用程序与传统桌面应用程序的不同: 1.产品级的Web应用程序总是包括至少两台联网的机器:一台承载网站,另一台在Web浏览器中查看数据. 即:我们通过自己的电脑浏览Web程序,这个程序会向服 ...

  10. Eclipse代码注释模板-code template

    参考帖子blog.csdn.net/security08/article/details/5588013 操作步骤:打开Window->Preferences->Java->Code ...