题目大意: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. Samba 'smbcacls'命令安全绕过漏洞

    漏洞版本: Samba 4.x 漏洞描述: Bugtraq ID:66232 CVE ID:CVE-2013-6442 Samba是一款实现SMB协议.跨平台进行文件共享和打印共享服务的程序. 当使用 ...

  2. poj2752 水题

    又2b了一次…… var s:ansistring; ans,pre:..] of longint; i,k,tot:longint; procedure main; begin pre[]:=;k: ...

  3. 分布式存储Memcache替代Session方案

    PHP自带的Session实际是在服务器中为每个客户建立独立的文件存放各自的信息. 在不做处理的情况下,很容易被客户端伪造.并且由于采用文件形式,所以存在着IO 读写的瓶颈.一般当用户在线达到1000 ...

  4. 【转】[MTK软件原创] [SELinux] 如何设置确认selinux模式

    原文网址:http://bbs.16rd.com/thread-54766-1-1.html [Description] linux SELinux 分成Enforce 以及 Permissive 两 ...

  5. ubuntu常用快捷键

    1.直接退出窗口  Ctrl + q 2.窗口变大变小  Alt + Q 3.最小化窗口,显示桌面Ctrl + Alt + D 4.把当前窗口移到另一个工作区 Shift+ Ctrl + Alt +方 ...

  6. less命令

    less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或向后浏览文件,而more命令只能向前浏览.用less命令显示文件时,用PageUp键向上翻页,用P ...

  7. 黑盒测试用例设计方法&理论结合实际 -> 边界值分析法

    一. 概念 边界值分析法就是对输入或输出的边界值进行测试的一种黑盒测试方法.通常边界值分析法是作为对等价类划分法的补充,这种情况下,其测试用例来自等价类的边界. 二. 边界值分析法的应用 根据大量的测 ...

  8. Java:基础

    Hello World //HelloWorld.java文件 public class HelloWorld { public static void main(String[] args) { S ...

  9. 【HTML】Advanced7:HTML5 Forms Pt. 2: Attributes and Data Lists

    1.<label for"" ></label> <input type="email" placeholder=" & ...

  10. [原]SyntaxHighlighter使用笔记

    [Date]2013-09-21 [Environment]SyntaxHighlighter 3.0.83 [Author]wintys (wintys@gmail.com) http://wint ...