zoukankan      html  css  js  c++  java
  • A Simple Problem with Integers (树状数组区间修改,区间查询)

    You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input

    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample Input

    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4
    

    Sample Output

    4
    55
    9
    15
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 typedef long long ll;
     8 int t,n,jishu,m;
     9 const int N=100005;
    10 ll cnt1[N],arr[N],cnt2[N];
    11 int lowbits(int x) { return x&-x; }
    12 void add(int x,int value){
    13     ll temp=x;
    14     while(x<=N){
    15         cnt1[x]+=value;
    16         cnt2[x]+=value*(temp-1);
    17         x+=lowbits(x);
    18     }
    19 }
    20 ll query(int x){
    21     ll sum=0;
    22     int temp=x;
    23     while(x>0){
    24         sum+=temp*cnt1[x]-cnt2[x];
    25         x-=lowbits(x);
    26     }
    27     return sum;
    28 }
    29 
    30 int main(){
    31 
    32     scanf("%d%d",&n,&m);
    33     for(int i=1,d;i<=n;i++) scanf("%lld",&arr[i]),add(i,arr[i]-arr[i-1]);
    34     char ss[10];
    35     for(int i=1;i<=m;i++){
    36         int d1,d2,d3;
    37         scanf("%s",ss);
    38         if(ss[0]=='Q'){
    39             scanf("%d%d",&d1,&d2);
    40             printf("%lld
    ",query(d2)-query(d1-1));
    41         }
    42         else{
    43             scanf("%d%d%d",&d1,&d2,&d3);
    44             add(d1,d3),add(d2+1,-d3);
    45         }
    46     }
    47     return 0;
    48 }
    View Code


  • 相关阅读:
    怎么修改android飞行模式wifi
    斐讯n1盒子装远程迅雷
    Spring使用大全
    面向对象7大设计原则
    Mybatis之SqlNode解析
    【转载】MongoDB的C#驱动程序教程
    【转载】 mongodb C# 基本类库
    【转载】列举MongoDB C#驱动的几个Query方法
    【转载】MongoDB开发学习
    【转载】sql全国省市区数据库建表过程
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11417792.html
Copyright © 2011-2022 走看看