考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了。A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了。然后就在不停地YY B题,赛后听了英姐的答案,看了题解,发现其实自己是捕捉到了正确的解题思路的,但是因为不知道怎么算出期望的复杂度,因而就没敢敲,哪里会想到算复杂度会算期望的情况的呢- -0

然后就来说下坑爹的C题了,比赛的时候看了觉得是线段树,它支持段更,但是每次段更的时候对于每个点,更新的差值|x-y|要被记录下来,心里想,要是用线段树必然会TLE的,赛后看了题解也觉得这不是O(n^2)的节奏吗。。。后来学习了才明白,单次操作确实有可能会是O(n)的,但是均摊下来是可以O(1)的。

单次复杂度之所以会达到O(n)是因为下面的clear函数,如果clear的那个区间本身没有连成一片(即cov==-1),那么必然要将这个区间左右递归一次,直到往下递归的区间里有连成一片的才会停止,于是乎当我clear某个区间的时候的复杂度取决于该区间下有多少个不连续的区间。一开始所有区间都是不连续的(1,2,3,4...n),假如我一开始更新1~n,那么复杂度就是O(n)的,因为1~n下面的区间有n个不连续的区间,但是这次操作之后不连续的区间就变成1了。然后不难发现的是,每次更新一段的时候,最多会产生2个不连续的区间,所以m次操作后,不连续的总区间数控制在n+2m(局部最大是n个,就像一开始一样),所以所有clear的复杂度加起来撑死了也在n+2m这个范围内。因此复杂度是不会达到O(n^2)的。 智硬下想了好久才领悟到了为什么复杂度能控制在O(nlogn)下。

#pragma warning(disable:4996)
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
using namespace std; #define ll long long
#define maxn 100500 struct Node
{
int l, r;
ll sum;
ll delta;
ll cov;
}N[4*maxn]; int n, m; void build(int i, int L, int R)
{
N[i].l = L; N[i].r = R; N[i].sum = N[i].delta = 0; N[i].cov = -1;
if (L >= R){
N[i].cov = L;
return;
}
int M = (L + R) >> 1;
build(i << 1, L, M);
build(i << 1 | 1, M + 1, R);
} void clear(int i, int L, int R, int val)
{
if (N[i].cov != -1){
N[i].delta += abs(val - N[i].cov);
N[i].sum += abs(val - N[i].cov)*(N[i].r - N[i].l + 1);
}
else{
clear(i << 1, L, R, val);
clear(i << 1 | 1, L, R, val);
N[i].sum = N[i << 1].sum + N[i << 1 | 1].sum + 1LL * N[i].delta*(N[i].r - N[i].l + 1);
}
N[i].cov = -1;
} void update(int i, int L, int R, int val)
{
if (N[i].l == L&&N[i].r == R){
clear(i, L, R, val);
N[i].cov = val;
return;
}
if (N[i].cov != -1){
N[i << 1].cov = N[i << 1 | 1].cov = N[i].cov;
N[i].cov = -1;
}
int M = (N[i].l + N[i].r) >> 1;
if (R <= M) update(i << 1, L, R, val);
else if (L > M) update(i << 1 | 1, L, R, val);
else update(i << 1, L, M, val), update(i << 1 | 1, M + 1, R, val);
N[i].cov = -1;
N[i].sum = N[i << 1].sum + N[i << 1 | 1].sum + N[i].delta*(N[i].r - N[i].l + 1);
} ll query(int i, int L, int R)
{
if (N[i].l == L&&N[i].r == R){
return N[i].sum;
}
int M = (N[i].l + N[i].r) >> 1;
if (R <= M) return query(i << 1, L, R) + N[i].delta*(R - L + 1);
else if (L > M) return query(i << 1 | 1, L, R) + N[i].delta*(R - L + 1);
else return query(i << 1, L, M) + query(i << 1 | 1, M + 1, R) + N[i].delta*(R - L + 1);
} int main()
{
while (cin >> n >> m)
{
build(1, 1, n);
int t, l, r, x;
for (int i = 0; i < m; i++){
scanf("%d%d%d", &t, &l, &r);
if (t == 1) {
scanf("%d", &x);
update(1, l, r, x);
}
else{
printf("%I64d\n", query(1, l, r));
}
}
}
return 0;
}

CF444C DZY Loves Colors的更多相关文章

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

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

  2. Codeforces444C DZY Loves Colors(线段树)

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

  3. 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 ...

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

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  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 444 C - DZY Loves Colors

    C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...

  7. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

  8. CodeForces 445E DZY Loves Colors

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

  9. Codeforces Round #254 DZY Loves Colors

    题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0.      有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...

随机推荐

  1. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

  2. [转]vim常用命令

    [转]vim常用命令 http://www.cnblogs.com/sunyubo/archive/2010/01/06/2282198.html http://blog.csdn.net/wooin ...

  3. 关于SVN 目录结构,使用教程

    SVN使用教程:http://www.cnblogs.com/armyfai/p/3985660.html Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn ...

  4. Filter介绍

    Filter可人为是Servlet的一种“加强版”,它重要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理,是个典型的处理链.使用Filter的完整的流程是:Filt ...

  5. Introduction to Haskell

    "I know why you're here. ...why you hardly sleep, why night after night, you sit by your comput ...

  6. apache maven

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj0AAAGXCAYAAABY/uEUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw ...

  7. Linux Shell常用技巧(目录)

    Linux Shell常用技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html一. 特殊文件: /dev ...

  8. Java学习笔记--反射

    什么是Java反射 概念 java反射是指java能够在运行时确定类的类型信息,包括其方法.字段.构造函数等,并能够通过反射调用类或者类对象的方法.在Java中,java.lang.Class类与ja ...

  9. Java团队项目总结

    Java团队项目总结 1.项目实现情况 项目概述: 我们团队项目准备实现一个有关于大富翁有的游戏程序. 大富翁游戏,以经营权为主要的游戏方式,通过购买经营权与架构经营的星级服务来获得最大的利益,当其他 ...

  10. 【Populating Next Right Pointers in Each Node II】cpp

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...