【洛谷P3374】树状数组模板

题目描述

如题,已知一个数列,你需要进行下面两种操作:

1.将某一个数加上x

2.求出某区间每一个数的和

输入格式

第一行包含两个整数N、M,分别表示该数列数字的个数和操作的总个数。

第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值。

接下来M行每行包含3个整数,表示一个操作,具体如下:

操作1: 格式:1 x k 含义:将第x个数加上k

操作2: 格式:2 x y 含义:输出区间[x,y]内每个数的和

输出格式

输出包含若干行整数,即为所有操作2的结果。

输入输出样例

输入 #1复制

5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4

输出 #1复制

14
16

说明/提示

时空限制:1000ms,128M

数据规模:

对于30%的数据:N<=8,M<=10

对于70%的数据:N<=10000,M<=10000

对于100%的数据:N<=500000,M<=500000

样例说明:

故输出结果14、16

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	static long[] ch;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		InputReader sc=new InputReader(System.in);
//		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		ch=new long[n+1];
		for(int i=1;i<=n;i++) {
			update(i,sc.nextInt());
		}
		while(m-->0) {
			int a=sc.nextInt();
			int b=sc.nextInt();
			int c=sc.nextInt();
//			System.out.println(m+" "+i);
//			System.out.println(a);
			if(a==1) {
				update(b, c);
			}else if(a==2) {
//				System.out.println(a);
				long ans=getBetweenSum(b, c);
//				System.out.println(getSum(c));
				System.out.println(ans);
			}
//			System.out.println(m);
		}
	}

	private static int lowbit(int x) {
		// TODO Auto-generated method stub
		return x&(-x);
	}
	public static long getSum(int x) {
		long sum=0;
		for(;x>0;x-=lowbit(x))
			sum+=ch[x];
		return sum;
	}
	
	public static void update(int x,int v) {
//		System.out.println(ch.length);
		for(;x<ch.length;x+=lowbit(x)) {
			ch[x]+=v;
		}
//		System.out.println(-1);
	}
	public static long getBetweenSum(int st,int ed) {
		return getSum(ed)-getSum(st-1);
	}

}
class InputReader{
	private final InputStream stream;
	private final byte[] buf=new byte[8192];
	private int curChar,snumChars;
	public InputReader(InputStream st) {
		this.stream=st;
	}
	public int read() {
		if(snumChars==-1)
			throw new InputMismatchException();
		if(curChar>=snumChars) {
			curChar=0;
			try {
				snumChars=stream.read(buf);
			}catch (IOException e) {
				// TODO: handle exception
				throw new InputMismatchException();
			}
			if(snumChars<=0)
				return -1;
		}
		return buf[curChar++];
	}
	public int nextInt() {
		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 long nextLong() {
		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 int[] nextIntArray(int n) {
		int a[]=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=nextInt();
		}
		return a;
	}
	public String readString() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isSpaceChar(c));
		return res.toString();
	}
	public String nextLine() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isEndOfLine(c));
		return res.toString();
	}
	private boolean isEndOfLine(int c) {
		// TODO Auto-generated method stub
		return c=='\n'||c=='\r'||c==-1;
	}
	private boolean isSpaceChar(int c) {
		// TODO Auto-generated method stub
		return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值