K. Krystalova’s Trivial Problem
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
题目描述
Krystalova is writing trivial problems (as always) but now for some reason (lucky for you) she also wrote a trivial statement.
You have a trivial list of numbers L, and two trivial operations:
-
u l r x: Add x to the numbers in L in the range [l,r].
-
q l r: Get the sum of L2i for every i in the range [l,r].
输入描述
In the first line of input two integers N (1≤N≤10^5), Q (1≤Q≤10^4), the size of L and the number of operations respectively.
The next line contains N integers Li (0≤Li≤10^8), the elements of L.
The next Q lines contains an operation, that can be one of the two types described before. If the line starts with a letter “u” then will be three integers l (1≤l≤N), r (1≤r≤N), x (−108≤x≤108). If the line starts with a letter “q” then will be two integers l (1≤l≤N), r (1≤r≤N)
输出描述
For every operation of type 2, print in one line the result of the operation. As the answer might be very large, please output the answer modulo 10^9+7
输入样例1:
5 51 1 1 1 1
u 1 3 1
q 1 5
u 3 5 1
q 3 3
q 1 5
输出样例1:
149
25
题意
给定一个长度为 n 的数组 q 次 询问
i) 选择 一段区间加上 x
ii) 输出一段区间的元素平方和
思路
一眼数据结构 线段树魔改下
定义原元素为 y 平方为 y^2
加上 x 后原元素变为了 y+x 平方为 y 2 + 2 x y + x 2 y^2+2xy+x^2 y2+2xy+x2
对比可知即原元素 乘上 2x 再加上 x^2
平方和 更新为 y 2 ( 原平分和 ) + 2 x y + x 2 y^2(原平分和)+2xy+x^2 y2(原平分和)+2xy+x2
我们只需要在线段树数组里保存原元素的值用以更新平分和的值即可
故更新时应先更新平方和的值再更新原元素的值
取模细节多注意下 容易爆long long
Code
#include<bits/stdc++.h>
using namespace std;
int p;
struct segtree
{
long long v,vv,mul,add;
}st[400003];
long long a[100003];
void pushup(int rt)
{
st[rt].v=(st[rt<<1].v+st

本文介绍了一种使用线段树解决区间加法与区间平方和查询问题的方法。通过维护原元素值与平方和值,实现高效更新与查询操作。
最低0.47元/天 解锁文章
3619

被折叠的 条评论
为什么被折叠?



