当前位置:网站首页>外挂---线段树懒标记板子+简单数学推理
外挂---线段树懒标记板子+简单数学推理
2022-07-29 02:17:00 【WAWA源】
题目链接
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int N = 100010;
int n, m;
int w[N];
struct node
{
int l, r;
int sum, sum2;
int add;
}tr[N*4];
void pushup(int u)
{
tr[u].sum = tr[u<<1].sum + tr[u<<1|1].sum;
tr[u].sum2 = tr[u<<1].sum2 + tr[u<<1|1].sum2;
}
void cal(node &t,int add)
{
t.sum2 = t.sum2 + 2ll * t.sum * add + (t.r - t.l + 1) * add * add;
t.sum = t.sum + (t.r - t.l + 1) * add;
}
void pushdown(int u)
{
if(tr[u].add)
{
cal(tr[u<<1], tr[u].add);
cal(tr[u<<1|1], tr[u].add);
tr[u<<1].add += tr[u].add;
tr[u<<1|1].add += tr[u].add;
tr[u].add = 0;
}
}
void build(int u,int l,int r)
{
if(l == r)tr[u] = {
l, r, w[l], w[l]*w[l], 0};
else
{
tr[u] = {
l,r};
int mid = l + r >> 1;
build(u<<1, l, mid);
build(u<<1|1, mid+1, r);
pushup(u);
}
}
void modify(int u,int l,int r,int v)
{
if(tr[u].l >= l && tr[u].r <= r)
{
cal(tr[u], v);
tr[u].add += v;
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) modify(u<<1, l, r, v);
if(r > mid) modify(u<<1|1, l, r, v);
pushup(u);
}
pair<int,int> query(int u,int l,int r)
{
if(tr[u].l >= l && tr[u].r <= r)return {
tr[u].sum, tr[u].sum2};
int sum1 = 0, sum2 = 0;
int mid=tr[u].l + tr[u].r >> 1;
pushdown(u);
if(l <= mid)
{
pair<int,int> t = query(u<<1, l, r);
sum1 += t.first;
sum2 += t.second;
}
if(r>mid)
{
pair<int,int> t = query(u<<1|1, l, r);
sum1 += t.first;
sum2 += t.second;
}
return {
sum1,sum2};
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>w[i];
build(1, 1, n);
int op,l,r;
while(m--)
{
cin>>op>>l>>r;
if(op==1)
{
int k;cin>>k;
modify(1, l, r, k);
}
else
{
pair<int,int> t = query(1, l, r);
cout<<(t.first * t.first - t.second) / 2 <<'\n';
}
}
}
边栏推荐
猜你喜欢
ROCBOSS开源微社区轻论坛类源码
以科技传递温度,vivo亮相数字中国建设峰会
Understand the evolution of redis architecture in one article
Workflow of wireless vibrating wire acquisition system
【报错】node:internal/modules/cjs/loader:936 【解决方法】
I want to talk about high concurrency.
用于校园流浪猫信息记录和分享的小程序源码/微信云开发中大猫谱小程序源码
IOT components
手把手教你安装VSCode(附带图解步骤)
MySQL和Redis的双写一致性
随机推荐
When I look at the source code, what am I thinking?
ECCV 2022 | airdet: a small sample target detection method without fine tuning
Polygon point test
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
家庭亲戚关系计算器微信小程序源码
平凡的快乐
Code implementation - the greatest common factor of polynomials (linear algebra)
别人的快乐
Talk about the implementation principle of feign
Shell script quick start-01
一款好看的iapp捐赠榜单源码
云开发打工人必备上班摸鱼划水微信小程序源码
Ten methods to prevent blackmail software from attacking data
Three expiration strategies
MySQL和Redis的双写一致性
【无标题】
How to use RPA to achieve automatic customer acquisition?
3d智能工厂工艺流转可视化交互展示应用优点
QT screen adaptive automatic layout, drag the window to automatically grow larger and smaller (I)
Workflow of wireless vibrating wire acquisition system