zoukankan      html  css  js  c++  java
  • HDU1166 树状数组入门

    /**/
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cctype>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <vector>
    #include <string>
    #include <stack>
    #include <queue>
    
    typedef long long LL;
    typedef unsigned long long ULL;
    using namespace std;
    
    bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
    const double PI = acos(-1.0), ESP = 1e-10;
    const LL INF = 99999999999999;
    const int inf = 999999999, N = 5e4 + 24;
    int T, n, w, a[N], C[N];
    char s[6];
    
    int lowbit(int x) { return (x & -x); }
    
    void Add(int i, int w) {
        while(i <= n) {
            C[i] += w;
            i += lowbit(i);
        }
    }
    
    int sum(int i) {
        int sum = 0;
        while(i > 0) {
            sum += C[i];
            i -= lowbit(i);
        }
        return sum;
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        scanf("%d", &T);
        for(int kase = 1; kase <= T; kase++) {
            scanf("%d", &n);
            // memset(a, 0, sizeof a); memset(C, 0, sizeof C);
            for(int i = 0; i <= n; i++) a[i] = C[i] = 0;
            for(int i = 1; i <= n; i++) { scanf("%d", &w); Add(i, w); }
            printf("Case %d:
    ", kase);
            // if(s[0] == 'E') continue;
            // else if(s[0] == 'A') {
            // }
            // else if(s[0] == 'S') Sub();
            // else if(s[0] == 'Q') Query();
            int u, v;
            // getchar();
            while(scanf("%s", s), strcmp(s, "End") != 0) {
                // printf("
    s = %s
    ", s);
                scanf("%d%d", &u, &v);
                if(strcmp(s, "Query") == 0) printf("%d
    ", sum(v) - sum(u - 1));
                if(strcmp(s, "Add") == 0) Add(u, v);
                if(strcmp(s, "Sub") == 0) Add(u, -v); 
            }
        }
    
        return 0;
    }
    /*
        input:
        output:
        modeling:
        methods:
        complexity:
        summary:
    */
    Sample Input
    1 10 1 2 3 4 5 6 7 8 9 10
    Query 1 3
    Add 3 6
    Query 2 7
    Sub 10 2
    Add 6 3
    Query 3 10
    End
     
    Sample Output
    Case 1:
    6
    33
    59
  • 相关阅读:
    perl学习笔记三
    linux下编译C/C++
    redis学习笔记——数据类型
    redis学习笔记二
    perl学习笔记二
    hadoop实战 -- 网站日志KPI指标分析
    Java反射与动态代理
    使用maven来管理您的java项目
    使用MapReduce实现一些经典的案例
    编译本地64位版本的hadoop-2.6.0
  • 原文地址:https://www.cnblogs.com/000what/p/11565881.html
Copyright © 2011-2022 走看看