当前位置:网站首页>外挂---线段树懒标记板子+简单数学推理
外挂---线段树懒标记板子+简单数学推理
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';
}
}
}
边栏推荐
猜你喜欢

MySQL和Redis的双写一致性

ES6 detailed quick start!

多重继承与派生类成员标识

Three implementation methods of Servlet
![[quality] code quality evaluation standard](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
[quality] code quality evaluation standard

Servlet三种实现方式

6-21漏洞利用-mysql弱口令破解

Code implementation - the greatest common factor of polynomials (linear algebra)

新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版

《微信小程序-进阶篇》Lin-ui组件库源码分析-Button组件(二)
随机推荐
Flink生产环境经典问题汇总
ROCBOSS开源微社区轻论坛类源码
远离才会思念
童年的快乐时光
Only when you are far away will you miss
Shell 脚本 快速入门 -01
HTTP cache
time_ Wait and close_ Cause of wait
Some records during the development of ros2/ros1
0728~ sorting out interview questions
一文搞懂 Redis 架构演化之路
How to migrate thinkphp5 projects to Alibaba cloud function computing to cope with traffic peaks?
ES6 detailed quick start!
一文理解分布式开发中的服务治理
Polygon zkEVM——Hermez 2.0简介
OSPF实验
h. 264 code stream explanation
Tesla neural network model hydranet
Mqtt routine
Code implementation - the greatest common factor of polynomials (linear algebra)