zoukankan      html  css  js  c++  java
  • LuoguP3374 【模板】树状数组 1

    题目描述

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

    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

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn = 500001;
    int C[maxn],n,m;
    int lowbit(int x){
    	return x & (-x);
    }
    void add(int pos,int x){
    	for(;pos <= n;pos += lowbit(pos)) C[pos] += x;
    }
    int query(int pos){
    	int res = 0;
    	for(;pos > 0;pos -= lowbit(pos)) res += C[pos];
    	return res;
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin>>n>>m;
    	int op,x,y,tmp;
    	for(int i = 1;i <= n;i++){
    		cin>>tmp;
    		C[i] = C[i-1] + tmp;
    	} 
    	for(int i = 1;i <= m;i++){
    		cin>>op>>x>>y;
    		if(op == 1) add(x,y);
    		else{
    			cout<<query(y) - query(x-1)<<endl;;
    		}
    	}
    	return 0;
    }
    
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    spring cloud 之config配置
    java HTTP连接 可以结合springcloud服务发布注册
    webStrom的注册码地址
    VUE的富文本编辑器
    vue2.0对于IE9浏览器的兼容
    用花生壳代理出现Invalid Host header错误
    用于时间统计数据的SQL
    Leetcode 136. Single Number
    Leetcode 36. Valid Sudoku
    VS Code
  • 原文地址:https://www.cnblogs.com/Zforw/p/10370576.html
Copyright © 2011-2022 走看看