import java.io.*;
import java.util.Scanner;
import static java.lang.Character.isValidCodePoint;
import static java.lang.Math.pow;
import static java.lang.System.in;
public class Main {
static PrintWriter printWriter = new PrintWriter(System.out);
static Read read = new Read();
static Scanner sc = new Scanner(in);
public static void main(String[] args) throws Exception {
//输入
int n = read.nextInt();
int m = read.nextInt();
//原始序列(即原始数组)
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) arr[i] = read.nextInt();
//将线段树封装进了类stree
stree s1 = new stree(arr);
while (m-- > 0) {
int c = read.nextInt();
int x, y, k;
if (c == 1) {
x = read.nextInt();
y = read.nextInt();
k = read.nextInt();
s1.updateRange(0, 0, n - 1, x - 1, y - 1, k);
} else {
x = read.nextInt();
y = read.nextInt();
printWriter.println(s1.queryRange(0, 0, n - 1, x - 1, y - 1));
}
}
//优化输出
printWriter.flush();
printWriter.close();
}
/**
* 注意:线段树查询的时候:本质就是看 “题目要求查询的区间与当前节点所管理的区间的关系“
*/
static class stree {
long tree[];
long lazy[];
int n;
//初始化对象
public stree(int arr[]) {
this.n = arr.length;
this.tree = new long[4 * n];
this.lazy = new long[4 * n];
//
build(arr, 0, 0, n - 1);
}
//创建线段树的函数
/**
*
* @param arr:初始序列(即main中的a数组)
* @param node:当前节点
* @param start:当前节点管理的左边界(:build(arr, 0, 0, n - 1) ),刚开始是0
* @param end: 当前节点管理的右边界,刚开始是n-1
*/
public void build(int arr[], int node, int start, int end) {
if (start == end)
tree[node] = arr[start];
else {
int mid = (start + end) / 2;
build(arr, 2 * node + 1, start, mid);
build(arr, 2 * node + 2, mid + 1, end);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
}
//懒节点向下传递的函数
public void pushdown(int node, int start, int end) {
if (lazy[node] != 0) {
tree[node] += (lazy[node] * (end - start + 1));
if (start != end) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
}
public void updateRange(int node, int start, int end, int l, int r, int val) {
pushdown(node, start, end);
if (start > end || start > r || end < l)
return;
if (start >= l && end <= r) {
tree[node] += val * (end - start + 1);
if (start != end) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (start + end) / 2;
//更新[2*node+1,mid]与[2*node+2,r]区间
updateRange(2 * node + 1, start, mid, l, r, val);
updateRange(2 * node + 2, mid + 1, end, l, r, val);
tree[node] = tree[2 * node + 1] + tree[2 * node + 2];
}
/**
* @param node:当前节点
* @param start:查询的l
* @param end:要查询的r
* @param l:当前节点node管理的左边界
* @param r:当前节点node管理的右边界
* @return
*/
public long queryRange(int node, int start, int end, int l, int r) {
//向下传递
pushdown(node, start, end);
//超出范围
if (start > end || start > r || end < l)
return 0;
//刚好在区间内
if (start >= l && end <= r)
return tree[node];
//取半
int mid = (start + end) / 2;
//左孩子:2*node+1 右孩子:2*node+2
long p1 = queryRange(2 * node + 1, start, mid, l, r);
long p2 = queryRange(2 * node + 2, mid + 1, end, l, r);
//p1:是左边的和,p2是右边的和
return (p1 + p2);
}
}
//自定义的输入类
static class Read {
private InputStream stream = in;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() throws IOException {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() throws IOException {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') return res * pow(10, nextInt());
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') return res * pow(10, nextInt());
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String next() throws Exception {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
if (isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private char nextChar() throws IOException {
int c = read();
while (isSpaceChar(c)) c = read();
return (char) c;
}
private boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return isWhitespace(c);
}
private boolean isSpaceChar2(int c) {
if (filter != null) {
return filter.isSpaceChar2(c);
}
return isWhitespace2(c);
}
private static boolean isWhitespace(int c) {
return c == '\n' || c == '\r' || c == '\t' || c == -1 || c == ' ';
}
private static boolean isWhitespace2(int c) {
return c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
public boolean isSpaceChar2(int ch);
}
}
}
洛谷 P3372 【模板】线段树 1 Java+线段树(懒标记)
最新推荐文章于 2024-08-30 18:40:43 发布