题目大意: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. tap,touch,touchstart,事件与click事件的区别

    根据源码所见, 移动端为了将将单击事件更加灵敏,所以现在的JQM,ST...框架都将JS单击事件封装成tap,或者touch或者touchstart事件, 其实现本质是将click触发多次,以打成移动 ...

  2. [Mac][$PATH]如何修改$PATH变量

    从 stackoverflow 找到的方法 http://stackoverflow.com/questions/7703041/editing-path-variable-on-mac 首先打开终端 ...

  3. disabled

    http://blog.csdn.net/dinglang_2009/article/details/6974887 如果把页面viewstate设为disabled 那么 ispost就一直为fal ...

  4. shell获取db信息及上传下载操作

    这个脚本是获取目标机器的db信息和os信息的.os信息很好获取,db的信息包含db名字,db版本以及所有的db instance,db信息的获取稍显复杂,下面是整个脚本代码: 关键字: awk, se ...

  5. PHP 进行蜘蛛访问日志统计

    $useragent = addslashes(strtolower($_SERVER['HTTP_USER_AGENT'])); if (strpos($useragent, 'googlebot' ...

  6. Channel 详解

    java.nio.channels.FileChannel封装了一个文件通道和一个FileChannel对象,这个FileChannel对象提供了读写文件的连接. 1.接口 2.通道操作 a.所有通道 ...

  7. js,css压缩工具

    常见的js压缩工具: js commpress: 运行平台:windows系统, 优点:能自动压缩: 缺点:不能拖动文件压缩:只支持windows平台: s.press 运行平台:mac ox系统 优 ...

  8. Linux Oracle碰到错误:ORA-27101: shared memory realm does not exist

    从ITPUB上摘抄并已验证 1.实例没有启动 sqlplus /nologconnect / as sysdbastartup

  9. 初识SQL注入

    什么是SQL注入(SQL Injection)? SQL注入是网站攻击途径之一,这里引用一下百度百科的解释:“所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串, ...

  10. SimpleHttpServer的学习之UML

    如何分析一个稍微大点的源码呢? 静态分析 除了看代码,就是 uml图,UML虽然在书本类与类之间的关系很复杂,可能要一本书,但是最核心的其实很简单: (1)继承 extends (2)实现接口 imp ...