考试完之后打的第一场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. Python学习教程(learning Python)--2.2.2 Python全局和局部变量

    Python的变量也有全局和局部变量之分. 1. 局部变量 用在子函数里的变量称之为局部变量,其生命周期为该函数执行周期,即函数执行完后变量即不存在.由于局部变量和某个函数直接相关,故不同子函数里可以 ...

  2. WPF socket通讯 UDP接收消息

    客户端: private Socket socket; private IPEndPoint ipEndPoint; private void sendMessageHandler() { //服务端 ...

  3. C/C++ 内存管理 (《高质量C++》-- 整理笔记)

    内存管理是我们在编程时经常遇到的问题,而关于内存管理的问题往往会导致我们无从下手,这篇随笔是我阅读<高质量C++>第7章“内存管理”时一些总结. 1.内存分配方式 在C++中内存分为5个区 ...

  4. Mybatis 的日志管理

    Mybatis通过日志工厂提供日志信息,Mybatis内置的日志模版是log4j,commons.log,jdk log也可以通过slf4j简单日志模版结合log4j使用日志信息输出.具体选择哪个日志 ...

  5. jdbc 连接 oracle rac

    jdbc 连接 oracle rac 的连接串如下:   jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192. ...

  6. poj 1338 Ugly Numbers

    原题链接:http://poj.org/problem?id=1338 优先队列的应用,如下: #include<cstdio> #include<cstdlib> #incl ...

  7. 8 C#中的字符串输出

    我们在前面已经用Console.WriteLine("*********")往dos窗口中输出过字符串.我们还定义过字符串的变量. string words ="我喜欢D ...

  8. PB建数据窗口的时候会报内存错误

     同事碰到了这个问题,百度了一下,按照下边的方法解决了 ------解决方案--------------------我遇到过,是powerbuilder的注册表出问题了,找到注册表中HKEY_USER ...

  9. JAVA数据结构-----枚举

    枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广. 枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式. 例如,枚举定义了 ...

  10. Android -- Layout布局文件里的android:layout_height等属性为什么会不起作用?

    有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性来布局,比如我们设置了android:layout_marginTop="100dip ...