CF444C DZY Loves Colors
考试完之后打的第一场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的更多相关文章
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- 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 ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Cf 444C DZY Loves Colors(段树)
DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...
- Codeforces 444 C - DZY Loves Colors
C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- CodeForces 445E DZY Loves Colors
DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- Codeforces Round #254 DZY Loves Colors
题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0. 有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...
随机推荐
- Collection、Iterator、Set、HashSet
Collection接口的基本方法 boolean add(Object o) 向集合当中加入一个对象 void clear() 删除集合当中的所有对象 boolean isEmpty() 判断集合是 ...
- 启动FM预算基金管理模块后,0L总账消失的解决办法
只要用SE38运行一下:FMGL_CHANGE_APPL_IN_LEDGER 问题就解决了.
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- golang的++与--
http://godoc.golangtc.com/doc/faq#inc_dec 简单地说, 在golang中++,--操作是语句而不是表达式. 所以a=b++, return x++之类绝对提示错 ...
- openssl AES加密以及padding
好习惯,先上代码再说事 加密 void AesEncrypt(unsigned char* pchIn, int nInLen, unsigned char *ciphertext, int & ...
- 如何查看hadoop与hbase的版本匹配关系
官网:http://hbase.apache.org/book.html 搜索:Hadoop version support matrix 下面有一个二维的支持关系表.
- Python MYSQL - tiny ETL tool - 文件操作和数据库操作
import os import MySQLdb Con= MySQLdb.connect(host=',db='test') #链接数据库 cur=Con.cursor() os.chdir(&qu ...
- 7.FPGA中的同步复位与异步复位
1.异步复位 always @ ( posedge sclk or negedge s_rst_n ) if ( !s_rst_n ) d_out <= 1'b0; else d_out < ...
- 使用itunes同步ios时丢失照片恢复
因没有备份,在使用同步功能后,发现照片被清空了,找到恢复方法,分享之! from:http://modmyi.com/forums/native-iphone-ipod-touch-app-discu ...
- Hibernate学习---第五节:普通组件和动态组件
一.普通组件映射配置 1.创建组件类,代码如下: package learn.hibernate.bean; /** * 组件类 */ public class Phones { private St ...