• A Simple Problem with Integers POJ


    A Simple Problem with Integers  POJ - 3468 

    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

    Hint

    The sums may exceed the range of 32-bit integers.
     
      1 #include <iostream>
      2 #include <algorithm>
      3 #include <cstring>
      4 #include <string>
      5 #include <vector>
      6 #include <map>
      7 #include <set>
      8 #include <list>
      9 #include <deque>
     10 #include <queue>
     11 #include <stack>
     12 #include <cstdlib>
     13 #include <cstdio>
     14 #include <cmath>
     15 #include <iomanip>
     16 #define ull unsigned long long
     17 #define ll long long
     18 #define pb push_back
     19 #define mem(sum,x) memset(sum,x,sizeof(sum))
     20 #define rep(i,start,end) for(int i=start;i<=end;i++)
     21 #define per(i,end,start) for(int i=end;i>=start;i--)
     22 #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
     23 using namespace std;
     24 const int mod = 998244353;
     25 const int mxn = 2e5 +7;
     26 int _,n,m,k,t,u,v,w,ans,cnt,ok;
     27 int num[mxn] , last[mxn] , key[mxn] ;
     28 /// string str ;
     29 char str[10] , ch ;
     30 #define lc now<<1
     31 #define rc now<<1|1
     32 struct node {ll val , l , r , sum , lazy ;};node no[mxn<<4];
     33 int lowbit(int x) {return x&-x;};
     34 /// void add(int i,int val){while(i<=n*4) key[i]+=val , i+=lowbit(i) ;}
     35 /// int ask(int x){int ans = 0 ;while(x>0) ans+=key[x] , x-=lowbit(x) ;return ans;}
     36 void pushup(int now){
     37     no[now].val = no[lc].val + no[rc].val ;
     38 }
     39 void updown(int now)
     40 {
     41     /// no[now].val = no[lc].val + no[rc].val;
     42     /// no[now].val = max( no[lc].val , no[rc].val );
     43     if(no[now].lazy)
     44     {
     45         no[lc].lazy += no[now].lazy;
     46         no[rc].lazy += no[now].lazy;
     47         no[lc].val += (no[lc].r-no[lc].l+1)*no[now].lazy;
     48         no[rc].val += (no[rc].r-no[rc].l+1)*no[now].lazy;
     49         no[now].lazy = 0;
     50     }
     51 }
     52 void build(int l,int r,int now)
     53 {
     54     no[now].l = l, no[now].r = r ,no[now].lazy = 0 ,no[now].val = 0 ;
     55     if(l==r){
     56         cin>>no[now].val;return ;
     57     }
     58     int mid = (l+r)>>1;
     59     build(l,mid,lc); build(mid+1,r,rc); pushup(now) ;
     60 }
     61 ll ask(int l,int r,int now)
     62 {
     63     if(l<=no[now].l && r>=no[now].r){
     64         return no[now].val;
     65     }
     66     updown(now);
     67     int mid = (no[now].l+no[now].r)>>1;
     68     ll ans = 0 ;
     69     if(l<=mid) ans+=ask(l,r,lc);
     70     if(r>mid) ans+=ask(l,r,rc);
     71     return ans ;
     72 }
     73 void add(int l,int r,int val,int now)
     74 {
     75     if(l<=no[now].l && no[now].r<=r){
     76         no[now].lazy += val ;
     77         no[now].val += (no[now].r-no[now].l+1)*val;
     78         return ;
     79     }
     80     updown(now);
     81     int mid = (no[now].l+no[now].r)>>1;
     82     if(l<=mid) add(l,r,val,lc);
     83     if(r>mid) add(l,r,val,rc);
     84     pushup(now);
     85 }
     86 int main()
     87 {
     88     tle;
     89     while(cin>>n>>m)
     90     {
     91         build(1,n,1);
     92         while(m--)
     93         {
     94             int u,v;
     95             cin>>ch>>u>>v;
     96             if(ch=='Q') cout<<ask(u,v,1)<<endl;
     97             else {
     98             cin>>k; add(u,v,k,1);
     99             }
    100         }
    101     }
    102 }
    所遇皆星河
  • 相关阅读:
    APP——Python——元素定位方法01
    APP——python——Appium中 setCapability的各种方法
    APP——python——自动化环境搭建02——个人详细搭建——真机模拟
    APP——python——自动化环境搭建01
    APP自动化定位元素——Android SDK——打开uiautomatorviewer.bat文件——查看元素
    APP自动化定位元素——Android SDK——安装
    mock搭建——python——搭建一个简单的mock服务——修改版本
    mock搭建——python——搭建一个简单的mock服务——简单版本
    adb命令——基础系统类命令 ——系统cpu、内存、进程等属性相关命令
    adb命令——基础系统类命令 ——adb shell getprop 获取系统属性
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/12828813.html
Copyright © 2020-2023  润新知