一开始以为是读入数据可能有超过int范围的,把StreamTokenizer换掉了,但是现在还是70分,每日下载已经用完了,请各位大佬看看
class Tnode{
long lazy;
long val;
int[] sec;
Tnode right;
Tnode left;
}
public class Main {
static long[] arr;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] s = reader.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
String[] sp = reader.readLine().split(" ");
arr = new long[n];
for (int i = 0; i < sp.length; i++) {
arr[i]=Long.parseLong(sp[i]);
}
LinkedList<Long> longs = new LinkedList<>();
//build tree
Tnode root=buildTree(1,n);
//pass
for (int i = 0; i < m; i++) {
int x,y,k;
sp = reader.readLine().split(" ");
x=Integer.parseInt(sp[1]);
y=Integer.parseInt(sp[2]);
if (sp[0].equals("2")) {
//区间查询
longs.add(getSecSum(x, y, root));
}else {
k=Integer.parseInt(sp[3]);
//区间修改
plusSecSum(x,y,root,k);
}
}
longs.forEach(System.out::println);
}
static void plusSecSum(int begin,int end,Tnode root,long k){
if(begin>root.sec[1]||end<root.sec[0])return;
if(begin<=root.sec[0]&&end>=root.sec[1]) {
root.lazy+=k;
root.val+=k*(root.sec[1]-root.sec[0]+1);
}else {
pushdown(root);
plusSecSum(begin, end,root.right,k);
plusSecSum(begin, end,root.left,k);
pushup(root);
}
}
static long getSecSum(int begin,int end,Tnode root){
if(begin>root.sec[1]||end<root.sec[0])return 0;
if(begin<=root.sec[0]&&end>=root.sec[1])return root.val;
pushdown(root);
int rtu=0;
if(root.right!=null)rtu+=getSecSum(begin,end,root.right);
if(root.left!=null)rtu+=getSecSum(begin,end, root.left);
return rtu;
}
static void pushup(Tnode n){
if(n.right==null&&n.left==null)return;
n.val=0;
if(n.right!=null)n.val+=n.right.val;
if(n.left!=null)n.val+=n.left.val;
}
static void pushdown(Tnode n){
if(n.lazy!=0){
Tnode tmp;
if(n.right!=null) {
tmp = n.right;
tmp.val+=(n.lazy*(tmp.sec[1]-tmp.sec[0]+1));
tmp.lazy+=n.lazy;
}
if(n.left!=null) {
tmp = n.left;
tmp.val+=(n.lazy*(tmp.sec[1]-tmp.sec[0]+1));
tmp.lazy+=n.lazy;
}
n.lazy=0;
}
}
static Tnode buildTree(int begin,int end){
if(end<begin)return null;
Tnode root=new Tnode();
root.sec=new int[]{begin,end};
int mid=(begin+end)/2;
if(begin!=end){
root.left=buildTree(begin,mid);
root.right=buildTree(mid+1,end);
pushup(root);
}else {
root.val=arr[begin-1];
}
return root;
}
}