题目大意:Codeforces 444C DZY Loves Colors

题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值。

解题思路:线段树模板题。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std;
const int maxn = 5*1e5;
typedef long long ll; ll s[maxn], del[maxn], mark[maxn]; void build (int u, int l, int r) {
if (l < r) {
int mid = (l + r)/2;
build(u*2, l, mid);
build(u*2+1, mid+1, r);
} else
mark[u] = l;
} ll query (int u, int l, int r, int x, int y) {
if (y < l || x > r)
return 0; if (x <= l && r <= y)
return s[u]; int mid = (l + r) / 2;
ll L = query(u*2, l, mid, x, y);
ll R = query(u*2+1, mid+1, r, x, y);
return L + R + max(0, min(r, y) - max(l, x) + 1) * del[u];
} void clear (int u, int l, int r, int z) {
if (mark[u]) {
del[u] += abs(mark[u]-z);
s[u] += 1LL * (r-l+1) * abs(mark[u]-z);
mark[u] = 0;
} else { if (l == r)
return; int mid = (l + r) / 2, lc = u*2, rc = u*2+1;
clear(lc, l, mid, z);
clear(rc, mid+1, r, z); s[u] = s[lc] + s[rc] + 1LL * (r-l+1) * del[u];
}
} void modify (int u, int l, int r, int x, int y, int z) {
// printf("%d %d %d %d %d %d\n", u, l, r, x, y, z);
if (y < l || x > r)
return; if (x <= l && r <= y) {
clear(u, l, r, z);
mark[u] = z;
return;
} int mid = (l + r) / 2, lc = u*2, rc = u*2+1;
if (mark[u]) {
mark[lc] = mark[rc] = mark[u];
mark[u] = 0;
}
modify(lc, l, mid, x, y, z);
modify(rc, mid+1, r, x, y, z);
s[u] = s[lc] + s[rc] + 1LL * (r-l+1) * del[u];
} int main () {
int n, m;
scanf("%d%d", &n, &m);
/*
memset(sum, 0, sizeof(sum));
memset(del, 0, sizeof(del));
memset(mark, 0, sizeof(mark));
*/ build(1, 1, n); int type, x, y, z;;
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &type, &x, &y);
if (type == 1) {
scanf("%d", &z);
modify(1, 1, n, x, y, z);
} else
printf("%lld\n", query(1, 1, n, x, y));
}
return 0;
}

Codeforces 444C DZY Loves Colors(线段树)的更多相关文章

  1. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  2. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  4. codeforces 444 C. DZY Loves Colors(线段树)

    题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r  操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...

  5. Cf 444C DZY Loves Colors(段树)

    DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...

  6. CodeForces 445E DZY Loves Colors

    DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  7. CodeForces 444C. DZY Loves Physics(枚举+水题)

    转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...

  8. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

  9. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

随机推荐

  1. Google 多源码管理工具 gclient

    google的chromium项目是用gclient来管理源码的checkout, update等. gclient是google专门为这种多源项目编写的脚本,它可以将多个源码管理系统中的代码放在一起 ...

  2. Windows 之间用rsync同步数据(cwRsyncServer配置)

    rsync是一款优秀的数据同步软件,在跨服务器,跨机房,跨国备份服务器的首选工具,下面就来介绍下如何配置安装cwRsyncServer很大多数软件一样是B/C架构,cwRsyncServer是rsyn ...

  3. data guard折腾记一

    终于有空闲的机器腾出来了,生产环境上的一套Oracle环境终于可以鸟枪换炮了,生产环境有Data Guard,为了减少停机时间,而且避免重新构建Data Guard的麻烦(其实也不麻烦,就是浪费时间) ...

  4. ubuntu终端提示符@name修改

    需要修改两个文件: 1.在终端输入 vim  /etc/hosts 将当前的name改为ubuntu: 2.在终端输入 vim /etc/hostsname 将当前的name改为ubuntu: 3.重 ...

  5. 初识NuGet - 概念, 安装和使用

    1. NuGet是什么? NuGet is a Visual Studio 2010 extension that makes it easy to add, remove, and update l ...

  6. 发布代码小助手V2.1发布了——Code2HTML工具

    设计起源: 新浪博客似乎没有插入代码的功能,所以不得不用打空格的方法格式化代码.而且没法显示行号. 描述: 发布代码小助手用python和Tkinter开发,可以在任何常见操作系统上运行.主要用于在不 ...

  7. js中的继承2--原型继承

    一. 原型与构造函数 Js所有的函数都有一个prototype属性,这个属性引用了一个对象,即原型对象,也简称原型.这个函数包括构造函数和普通函数,我们讲的更多是构造函数的原型,但是也不能否定普通函数 ...

  8. 【Hadoop代码笔记】目录

    整理09年时候做的Hadoop的代码笔记. 开始. [Hadoop代码笔记]Hadoop作业提交之客户端作业提交 [Hadoop代码笔记]通过JobClient对Jobtracker的调用看详细了解H ...

  9. 使用SVN服务器管理源码

    最近在学习使用SVN管理自己的项目文件,正好有好的文章就拿来标记一下,正所谓: 站在巨人的肩膀上   天下知识为我所用 转载两篇关于使用SVN管理源码的文章. 使用SVN进行源码管理(上):http: ...

  10. POJ 3648-Wedding(2-SAT)

    题面很邪恶啊... 一对新人请n-1对夫妻吃饭,人们坐在一张桌子的两侧,每一对互为夫妻关系的人必须坐在桌子的两侧.而且有些人两两之间会存在“通奸”关系,通奸关系不仅在男女之间,同性之间也有.新娘对面不 ...