UVA 12436 - Rip Van Winkle's Code(线段树)
UVA 12436 - Rip Van Winkle's Code
题意:区间改动一个加入等差数列,一个把区间设为某个值,然后询问区间和
思路:关键在于等差数列的地方,线段树的每一个结点加入一个首项和公差,因为等差数列加上一个等差数列还是一个等差数列。利用这个性质就能够进行维护了,注意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's Code(线段树)的更多相关文章
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- UVA 12501 Bulky process of bulk reduction ——(线段树成段更新)
和普通的线段树不同的是,查询x~y的话,给出的答案是第一个值的一倍加上第二个值的两倍一直到第n个值的n倍. 思路的话,就是关于query和pushup的方法.用一个新的变量sum记录一下这个区间里面按 ...
- UVA-12436 Rip Van Winkle's Code (线段树区间更新)
题目大意:一个数组,四种操作: long long data[250001]; void A( int st, int nd ) { for( int i = st; i <= nd; i++ ...
- POJ训练计划2528_Mayor's posters(线段树/成段更新+离散化)
解题报告 id=2528">地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000] ...
- Uva 12299 带循环移动的RMQ(线段树)
题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...
- UVa 1471 Defense Lines - 线段树 - 离散化
题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...
随机推荐
- Angular 下的 function
angular.lowercas 将指定的字符串转换为小写的 Usage(使用方法) angular.lowercase(string); Arguments Param Type Details ...
- ZYNQ. LwIP.PHY.KSZ9031RNX
新一块板子 米尔科技的 z-turn 使用的PHY芯片是Micrel的 KSZ9031RNX 而不是zedboard上的Marvell的. 直接使用lwip的echo server demo时会报错 ...
- ipython的%matplotlib inline如何改写在Python
ipython notebook中有一个相当方便的语句: %matplotlib inline,可以实现运行cell即出现结果图像.但是如果想写在Python程序内,貌似直接%matplotlib i ...
- JS判断客户浏览器是否是IE8浏览器、jQuery判断浏览器内核
今天在使用encharts的时候由于要兼容IE8,所以最终决定在非IE8浏览器使用encharts,在IE8使用amcharts.于是需要使用JS判断使用的浏览器版本: function IEVers ...
- Casperjs循环执行(重复执行不退出)
var casper = require('casper').create({ // pageSettings: { // loadImages: true, // loadPlugins: fals ...
- pwd、ln和重定向命令
pwd命令 命令功能: 使用pwd命令可以显示当前的工作目录,该命令很简单,直接输入pwd即可,后面不带参数. pwd命令以绝对路径的方式显示用户当前工作目录.命令将当前目录的全路径名称(从根 ...
- 2.选择元素 - 自定义过滤器《jquery实战》
2.5.6 自定义过滤器 jQuery 中有两种方法创建自定义的过滤器.第一种比较简单,但是不鼓励,从 jQuery 1.8 开始已经被第二种方法取代.记住,使用新方法时,你自定义的过滤器在 jQue ...
- 【LOJ】#2567. 「APIO2016」划艇
题解 显然有个很暴力的dp,\(dp[i][j]\)表示选到第\(i\)个数,末尾的数是\(j\)的方案数 但是第二维就开不下了,怎么办呢 考虑离散化整个区间,我们记录\(dp[i][j][k]\)表 ...
- rabbitMQ的安装(Windows下)
在公司接触到这一块,信息中间件的使用,在公司没有时间了解的更加深入,只是在简单的使用,这里将深入学习一番. 参考:http://blog.csdn.net/lu1005287365/article/d ...
- 007 jquery过滤选择器-----------(屬性过滤选择器)
1.介紹 2.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...