• 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)


    【概念】

      转载连接:树状数组  讲的挺好。

    这两题非常的相似,查询区间的累加和、更新结点。Add(x,d) 与 Query(L,R) 的操作

    【题目链接:candy

      唉,也是现在才发现这题用了这个知识,当初A的第一个数据结构的题就是关于树状数组的,忘了忘了。。

      

    Problem C: candy

    Time Limit: 3 Sec  Memory Limit: 128 MB
    Submit: 252  Solved: 63

    SubmitStatusWeb Board

    Description

    Kimi has a lot of candies, and divides them into piles, where the ith pile contains Ai candies. Each time Kimi will choose an interval [l,r], and calculate the total amount of Al,Al+1,…,Ar. It's a hard task, and you're required to solve it.

    Input

    An integer T(T≤10) will exist in the first line of input, indicating the number of test cases. Each test case begins with the number of pilesN(1≤N≤105). The second line contains N integers Ai(1≤Ai≤100), where Ai stands for the number of candies in the ith pile. The next line is the number of queries M(1≤M≤105). The next M lines, each with two integers l,r(1≤lrN), describe the queried intervals.

    Output

    For each test case, output the total amount of candies in the queried interval.

    Sample Input

    1
    5
    1 2 4 5 9
    3
    1 2
    2 4
    4 5

    Sample Output

    3
    11
    14
     1 #include<cstdio>
     2 #include<cstring>
     3 const int MAXN = 1000008;
     4 int a[MAXN];
     5 int m;
     6 int lowbit(int r){
     7     return r & (-r);
     8 }
     9 int sum(int x){
    10     int ret = 0;
    11     while(x > 0){
    12         ret += a[x];
    13         x -= lowbit(x);
    14     }
    15     return ret;
    16 }
    17 void add(int i,int x){
    18     while(i <= m){
    19         a[i] += x;
    20         i += lowbit(i);
    21     }
    22 }
    23 int main(){
    24     int n;
    25     scanf("%d",&n);
    26     while(n--){
    27         memset(a,0,sizeof(a)); 
    28         scanf("%d",&m);
    29         int i,j;        
    30         for(i = 1;i <= m;i++){
    31             int x;
    32             scanf("%d",&x);
    33             add(i,x);
    34         }
    35         int t;
    36         scanf("%d",&t);
    37         while(t--){
    38             int num1,num2;
    39             scanf("%d%d",&num1,&num2);            
    40             printf("%d
    ",sum(num2) - sum(num1 - 1));
    41         }
    42     }
    43 }

    【题目链接:NYOJ-士兵杀敌(二)

    相似的简单题链接:士兵杀敌(一) 只涉及查询区间和

    士兵杀敌(二)

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:5
    描述

    南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的。

    小工是南将军手下的军师,南将军经常想知道第m号到第n号士兵的总杀敌数,请你帮助小工来回答南将军吧。

    南将军的某次询问之后士兵i可能又杀敌q人,之后南将军再询问的时候,需要考虑到新增的杀敌数

    输入
    只有一组测试数据
    第一行是两个整数N,M,其中N表示士兵的个数(1<N<1000000),M表示指令的条数。(1<M<100000)
    随后的一行是N个整数,ai表示第i号士兵杀敌数目。(0<=ai<=100)
    随后的M行每行是一条指令,这条指令包含了一个字符串和两个整数,首先是一个字符串,如果是字符串QUERY则表示南将军进行了查询操作,后面的两个整数m,n,表示查询的起始与终止士兵编号;如果是字符串ADD则后面跟的两个整数I,A(1<=I<=N,1<=A<=100),表示第I个士兵新增杀敌数为A.
    输出
    对于每次查询,输出一个整数R表示第m号士兵到第n号士兵的总杀敌数,每组输出占一行
    样例输入
    5 6
    1 2 3 4 5
    QUERY 1 3
    ADD 1 2
    QUERY 1 3
    ADD 2 3
    QUERY 1 2
    QUERY 1 5
    样例输出
    6
    8
    8
    20
     1  
     2 #include<stdio.h>
     3 #define size 1000008
     4 
     5 int a[size];
     6 int N;
     7 int lowbit(int r)
     8 {
     9     return r & (-r);
    10 }
    11 int sum(int x)
    12 {
    13     int ret = 0;
    14     while(x>0)
    15     {
    16         ret+=a[x];x-=lowbit(x);
    17     }
    18     return ret;
    19 }
    20 void add(int i,int x)
    21 {
    22     while(i<=N)
    23     {
    24         a[i]+=x;
    25         i+=lowbit(i);
    26     }
    27 }
    28 
    29 int main()
    30 {
    31     int M,m,n,x;
    32     scanf("%d%d",&N,&M);
    33     for(int i=1;i<=N;i++)
    34     {
    35         scanf("%d",&x);
    36         add(i,x);
    37     }
    38     while(M--)
    39     {
    40         char ac[5];
    41         scanf("%s%d%d",ac,&m,&n);
    42         if(ac[0]=='Q')
    43         {
    44             printf("%d
    ",sum(n)-sum(m-1));
    45         }
    46         else
    47         {
    48             add(m,n);
    49         }
    50     }
    51     return 0;
    52 }        

    继续努力吧~

  • 相关阅读:
    一个判断浏览器类型的JS
    asp中输出xml “文本内容中发现无效字符”问题的解决(转)
    Excel VBA工程密码之最简单破解法
    爆破AspriseOCR 4.0
    vi编辑器的使用(转载)
    数据挖掘(转载)
    简单工厂模式(SimpleFactory)
    工厂方法模式(Factory Method)
    让gridview默认处于编辑模式并取得编辑后的值
    GDB调试精粹及使用实例(转载)
  • 原文地址:https://www.cnblogs.com/zhengbin/p/4462942.html
Copyright © 2020-2023  润新知