zoukankan      html  css  js  c++  java
  • bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作

    Pku3468 A Simple Problem with Integers

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 2173  Solved: 951
    [Submit][Status][Discuss]

    Description


    You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000. 
    Each of the next Q lines represents an operation. 
    "C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000. 
    "Q a b" means querying the sum of Aa, Aa+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<cstring>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #include<cstdio>
     6 
     7 #define N 100007
     8 #define ll long long
     9 using namespace std;
    10 inline int read()
    11 {
    12     int x=0,f=1;char ch=getchar();
    13     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    14     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 
    18 int n,q;
    19 char ch[4];
    20 
    21 #define ls p<<1
    22 #define rs p<<1|1
    23 struct seg
    24 {
    25     ll sum[N<<2],add[N<<2];
    26     void build(int p,int l,int r)
    27     {
    28         if (l==r)
    29         {
    30             sum[p]=read();
    31             return;
    32         }
    33         int mid=(l+r)>>1;
    34         build(ls,l,mid),build(rs,mid+1,r);
    35         sum[p]=sum[ls]+sum[rs];
    36     }
    37     void push_down(int p,int l,int r)
    38     {
    39         if (!add[p]) return;
    40         ll ad=add[p],mid=(l+r)>>1;add[p]=0;
    41         sum[ls]+=1ll*(mid-l+1)*ad;
    42         sum[rs]+=1ll*(r-mid)*ad;
    43         add[ls]+=ad,add[rs]+=ad;
    44     }
    45     ll query(int p,int l,int r,int x,int y)
    46     {
    47         if (l==x&&y==r) return sum[p];
    48         push_down(p,l,r);
    49         int mid=(l+r)>>1;
    50         if (y<=mid) return query(ls,l,mid,x,y);
    51         else if (x>mid) return query(rs,mid+1,r,x,y);
    52         else return query(ls,l,mid,x,mid)+query(rs,mid+1,r,mid+1,y);
    53     }
    54     void modify(int p,int l,int r,int x,int y,int z)
    55     {
    56         sum[p]+=1ll*(y-x+1)*z;
    57         if (l==x&&y==r)
    58         {
    59             add[p]+=z;
    60             return;
    61         }
    62         int mid=(l+r)>>1;
    63         if (y<=mid) modify(ls,l,mid,x,y,z);
    64         else if (x>mid) modify(rs,mid+1,r,x,y,z);
    65         else modify(ls,l,mid,x,mid,z),modify(rs,mid+1,r,mid+1,y,z);
    66     }
    67 }seg;
    68 #undef ls
    69 #undef rs
    70 int main()
    71 {
    72     n=read(),q=read();
    73     seg.build(1,1,n);
    74     for (int i=1;i<=q;i++)
    75     {
    76         scanf("%s",ch);
    77         if (ch[0]=='Q')
    78         {
    79             int x=read(),y=read();
    80             printf("%lld
    ",seg.query(1,1,n,x,y));
    81         }
    82         else
    83         {
    84             int x=read(),y=read(),z=read();
    85             seg.modify(1,1,n,x,y,z);
    86         }
    87     }
    88 }
  • 相关阅读:
    operator模块和functools模块
    函数注解
    用户定义的可调用类型、从定位参数到仅限关键字参数
    可调用对象
    nxos启动的初始化和https访问nx-api
    网络安全基础之网络协议与安全威胁
    华为AC中服务集命令解释配置
    转:图解ARP协议(四)代理ARP原理与实践(“善意的欺骗”)
    windows下python3 python2 共存下安装virtualenvwrapper
    关于网络安全学习的网站
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8847812.html
Copyright © 2011-2022 走看看