Codeforces 444C DZY Loves Colors(线段树)
题目大意: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(线段树)的更多相关文章
- 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 ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- codeforces 444 C. DZY Loves Colors(线段树)
题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r 操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...
- Cf 444C DZY Loves Colors(段树)
DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...
- CodeForces 445E DZY Loves Colors
DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- CodeForces 444C. DZY Loves Physics(枚举+水题)
转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...
- HDU5649 DZY Loves Sorting 线段树
题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
随机推荐
- HDU 5622 KK's Chemical DP
题意:bc round 71(中文题面) 分析(官方题解): 根据药品之间的相互关系,我们可以构建一张图,我们对相互会发生反应的药品连边 这个图的特征,是一个环加上一些“树”(可能有多个联通块) 一个 ...
- Eclipse导入的工程后referenced libraries中的jar包中文注释显示乱码解决方法
Preferences-General-Workspace-Text file encoding 设置为uft-8 最后重启一下eclipse.
- java.lang.NoClassDefFoundError: javax/transaction/Synchronization (jUnit测试报错)
测试hibernate 报错原因项目缺少包 在 hibernate 解压目录下找到 jta.jar 文件 往项目中添加该 jar 包,即可解决 添加方法:[右击项目]--> ...
- iOS 之NSJSONReadingOptions说明【转】
首先用代码来说明NSJSONReadingMutableContainers的作用: NSString *str = @"{\"name\":\"kaixuan ...
- Java 核心技术-集合-集合框架
说在前面的话: 关于Core Java 集合方面的博文网上已经写烂了,为啥我还要写呢? 答:他们写的都很好,我也学到不少东西,如果把我当做一个系统的话,学习别人.看书.读源码是输入,但是往往形不成一个 ...
- Android Viewpager PagerAdapter update data 刷新界面数据
最近做的项目涉及到ViewPager数据刷新,网上的资料挺多,但是和现在做的这个不太相同,所以并没有找到有效的. 折腾了大半天,整理一下思路: 问题1: 后台刷新数据次数过多后,界面出现卡顿现象,判断 ...
- C# 调用Dll 传递字符串指针参(转)
http://www.cnblogs.com/jxsoft/archive/2011/07/06/2099061.html
- HIbernate学习笔记(六) 关系映射之多对多
六.多对多 - 单向 Ø 一般的设计中,多对多关联映射,需要一个中间表 Ø Hibernate会自动生成中间表 Ø Hibernate使用many-to-ma ...
- BNUOJ-29365 Join in tasks 简单数学
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29365 首先排序,然后维护一个后缀,等差求下和就可以了.. //STATUS:C++_AC ...
- 【转】 hive安装配置及遇到的问题解决
原文来自: http://blog.csdn.net/songchunhong/article/details/51423823 1.下载Hive安装包apache-hive-1.2.1-bin.ta ...