zoukankan      html  css  js  c++  java
  • 洛谷树状数组1、2

    传送门

    树状数组1

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define re register
    using namespace std;
    
    inline long long read() {
    	char ch = getchar();
    	long long f = 1 , x = 0;
    	while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ;ch = getchar();}
    	while(ch >= '0' && ch <= '9') {x = (x << 1) + ( x << 3) + ch - '0';ch = getchar();}
    	return x * f;
    }
    
    long long n,a,m,flag,x,y;
    long long c[500005];
    
    inline long long lowbit(long long x){return x & (-x) ;}
    
    inline void add(long long x , long long y){
    	while(x <= n) {
    		c[x] += y ;
    		x += lowbit(x);
    	}
    }
    
    inline long long sum(long long x){
    	long long ans = 0;	 
    	while(x) {
    		ans += c[x] ;
    		x -= lowbit(x);
    	}
    	return ans;
    }
    
    int main(){
    	n = read(); m = read();
    	for(int i = 1 ; i <= n ; ++i) {
    		a = read();
    		add(i , a) ;
    	}
    	for(re int i = 1 ; i <= m ; ++i) {
    		flag = read(); 
    		if(flag == 1) {
    			x = read(); y = read();
    			add(x , y);
    		}
    		if(flag == 2) {
    			x = read(); y = read();
    			printf("%lld
    " , sum(y) - sum(x - 1));
    		}
    	}
    	return 0;
    }
    

    传送门

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define re register
    using namespace std;
    
    inline long long read() {
    	char ch = getchar();
    	long long f = 1 , x = 0;
    	while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ;ch = getchar();}
    	while(ch >= '0' && ch <= '9') {x = (x << 1) + ( x << 3) + ch - '0';ch = getchar();}
    	return x * f;
    }
    
    long long n,a,m,flag,x,y,w,last;
    long long c[500005];
    
    inline long long lowbit(long long x){return x & (-x) ;}
    
    inline void add(long long x , long long y){
    	while(x <= n) {
    		c[x] += y ;
    		x += lowbit(x);
    	}
    }
    
    inline long long query(long long x){
    	long long ans = 0;
    	while(x) {
    		ans += c[x];
    		x -= lowbit(x);
    	}
    	return ans;
    }
    
    int main(){
    	n = read(); m = read();
    	for(re int i = 1 ; i <= n ; ++i) {
    		a = read();
    		add(i , a - last);
    		last = a ;
    	}
    	for(re int i = 1 ; i <= m ; ++i) {
    		flag = read();
    		if(flag == 1) {
    			x = read(); y = read(); w = read();
    			add(x , w);
    			add(y + 1 , -w);
    		}
    		else {
    			x = read();
    			printf("%lld
    ",query(x));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    Lucene 全文检索入门
    Elastic Stack 笔记(九)Elasticsearch5.6 集群管理
    Elastic Stack 笔记(八)Elasticsearch5.6 Java API
    Elastic Stack 笔记(七)Elasticsearch5.6 聚合分析
    Elastic Stack 笔记(六)Elasticsearch5.6 搜索详解
    Elastic Stack 笔记(五)Elasticsearch5.6 Mappings 映射
    JAVA中的static
    类继承和初始化类的执行顺序
    java继承 初始化顺序
    分析java类的初始化契机
  • 原文地址:https://www.cnblogs.com/Stephen-F/p/9933086.html
Copyright © 2011-2022 走看看