CodeForces 444C 线段树
想分块想了很久一点思路都没有,结果一看都是写的线段树= = 。。。完全忘记了还有线段树这种操作
题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差
线段树记得+lazy标记
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 998244353
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=sqrt(N)+,inf=0x3f3f3f3f; ll color[N<<],lazy[N<<],sum[N<<];
bool same[N<<];
void changecolor(ll c,ll x,int l,int r,int rt)//x是改变的差值,c是上一个颜色
{
same[rt]=;
sum[rt]+=x*(r-l+);
color[rt]=c;
lazy[rt]+=x;
}
void pushup(int rt)
{
if(same[rt<<]&&same[rt<<|]&&color[rt<<]==color[rt<<|])
{
same[rt]=;
color[rt]=color[rt<<];
}
else same[rt]=;
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int l,int r,int rt)
{
if(lazy[rt])
{
int m=(l+r)>>;
changecolor(color[rt],lazy[rt],ls);
changecolor(color[rt],lazy[rt],rs);
lazy[rt]=;
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
color[rt]=l;
same[rt]=;
sum[rt]=;
return ;
}
int m=(l+r)>>;
build(ls);
build(rs);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R,ll x)
{
if(L<=l&&r<=R&&same[rt])
{
changecolor(x,abs(x-color[rt]),l,r,rt);
return ;
}
pushdown(l,r,rt);
int m=(l+r)>>;
if(L<=m)update(ls,L,R,x);
if(m<R)update(rs,L,R,x);
pushup(rt);
}
ll query(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R)return sum[rt];
pushdown(l,r,rt);
int m=(l+r)>>;
ll ans=;
if(L<=m)ans+=query(ls,L,R);
if(m<R)ans+=query(rs,L,R);
return ans;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(,n,);
while(m--)
{
int x,y;
ll z;
scanf("%d",&x);
if(x==)
{
scanf("%d%d%lld",&x,&y,&z);
update(,n,,x,y,z);
}
else
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(,n,,x,y));
}
}
return ;
}
/******************** ********************/
CodeForces 444C 线段树的更多相关文章
- DZY Loves Colors CodeForces - 444C (线段树势能分析)
大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[ ...
- Codeforces 444C 线段树 懒惰标记
前天晚上的CF比赛div2的E题,很明显一个线段树,当时还在犹豫复杂度的问题,因为他是区间修改和区间查询,肯定是要用到懒惰标记. 然后昨天真的是给这道题跪了,写了好久好久,...我本来是写了个add标 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- codeforces 1136E 线段树
codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
随机推荐
- Unity使用native读取streamingasset里文件
需求是,使用native方式,读取apk包里的lua代码,读进c#,做解密 一准备unity工程 public class GameMain : MonoBehaviour { public cons ...
- [HEOI2014]南园满地堆轻絮
[HEOI2014]南园满地堆轻絮 BZOJ luogu 二分答案贪心check 首先b[1]最小一定优 之后就贪心的最小化b[i]就行 #include<bits/stdc++.h> u ...
- 如何用jquery获取input输入框中的值?
如何用jquery获取<input id="test" name="test" type="text"/>中输入的值? $(&q ...
- ShutIt:一个基于 Python 的 shell 自动化框架
ShutIt是一个易于使用的基于shell的自动化框架.它对基于python的expect库(pexpect)进行了包装.你可以把它看作是“没有痛点的expect”.它可以通过pip进行安装. Hel ...
- 阿里云免费SSL证书申请及配置过程centos7,Nginx
1:进入购买,等待审核,审核通过后下在一个压缩包 ,里面包含两个文件一个 214979907780888.key 一个214979907780888.pem (如果不在阿里买,也可以在其它平台买,或者 ...
- Oracle学习笔记—归档模式
什么是归档模式 Oracle数据库有联机重做日志,这个日志是记录对数据库所做的修改,比如插入,删除,更新数据等,对这些操作都会记录在联机重做日志里.一般数据库至少要有2个联机重做日志组.当一个联机重做 ...
- SaltStack安装配置
一.环境准备:操作系统CentOS Linux release 7.3.1611master ip:192.168.1.180minion ip:192.168.1.183设置server(maste ...
- 剑指offer 面试15题
面试15题: 题目:二进制中1的个数 题:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题思路一: 最佳方法:把一个整数减去1,再和原整数做“与运算”,会把该整数最右边的1变成0 ...
- Overload and Override without Overwrite - Java
Override(覆盖/覆写): 子类Override父类中的函数(方法).Overload(重载): 同一个类中包含多个同名的函数(方法), 但各个函数的参数列表不同. Override和Overl ...
- 神奇的Timer
最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...