zoukankan      html  css  js  c++  java
  • poj 3468 A Simple Problem with Integers

    题目链接:http://poj.org/problem?id=3468

    解题思路:线段树(成段更新)

      1 /**************************************************************************
      2 user_id: SCNU20102200088
      3 problem_id: poj 3468
      4 problem_name: A Simple Problem with Integers
      5 **************************************************************************/
      6 
      7 #include <algorithm>
      8 #include <iostream>
      9 #include <iterator>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <fstream>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <climits>
     16 #include <bitset>
     17 #include <string>
     18 #include <vector>
     19 #include <cstdio>
     20 #include <cctype>
     21 #include <ctime>
     22 #include <cmath>
     23 #include <queue>
     24 #include <stack>
     25 #include <list>
     26 #include <set>
     27 #include <map>
     28 using namespace std;
     29 
     30 //线段树
     31 #define lson l,m,rt<<1
     32 #define rson m+1,r,rt<<1|1
     33 
     34 //手工扩展栈
     35 #pragma comment(linker,"/STACK:102400000,102400000")
     36 
     37 const double EPS=1e-9;
     38 const double PI=acos(-1.0);
     39 const double E=2.7182818284590452353602874713526;  //自然对数底数
     40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
     41 
     42 const int x4[]={-1,0,1,0};
     43 const int y4[]={0,1,0,-1};
     44 const int x8[]={-1,-1,0,1,1,1,0,-1};
     45 const int y8[]={0,1,1,1,0,-1,-1,-1};
     46 
     47 typedef long long LL;
     48 
     49 typedef int T;
     50 T max(T a,T b){ return a>b? a:b; }
     51 T min(T a,T b){ return a<b? a:b; }
     52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     54 
     55 ///////////////////////////////////////////////////////////////////////////
     56 //Add Code:
     57 const int maxn=100005;
     58 LL sum[maxn<<2],res[maxn<<2];
     59 
     60 void pushup(int rt){
     61     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     62 }
     63 
     64 void pushdown(int rt,int len){
     65     if(res[rt]){
     66         res[rt<<1]+=res[rt];
     67         res[rt<<1|1]+=res[rt];
     68         sum[rt<<1]+=(len-(len>>1))*res[rt];
     69         sum[rt<<1|1]+=(len>>1)*res[rt];
     70         res[rt]=0;
     71     }
     72 }
     73 
     74 void build(int l,int r,int rt){
     75     res[rt]=0;
     76     if(l==r){
     77         scanf("%I64d",&sum[rt]);
     78         return ;
     79     }
     80     int m=(l+r)>>1;
     81     build(lson);
     82     build(rson);
     83     pushup(rt);
     84 }
     85 
     86 void update(int L,int R,LL val,int l,int r,int rt){
     87     int len=r-l+1;
     88     if(l>=L && r<=R){
     89         res[rt]+=val;
     90         sum[rt]+=val*len;
     91         return ;
     92     }
     93     pushdown(rt,len);
     94     int m=(l+r)>>1;
     95     if(L<=m) update(L,R,val,lson);
     96     if(R>m) update(L,R,val,rson);
     97     pushup(rt);
     98 }
     99 
    100 LL query(int L,int R,int l,int r,int rt){
    101     int len=r-l+1;
    102     if(L<=l && R>=r) return sum[rt];
    103     pushdown(rt,len);
    104     int m=(l+r)>>1;
    105     LL ret=0;
    106     if(L<=m) ret+=query(L,R,lson);
    107     if(R>m) ret+=query(L,R,rson);
    108     return ret;
    109 }
    110 ///////////////////////////////////////////////////////////////////////////
    111 
    112 int main(){
    113     std::ios::sync_with_stdio(false);
    114     //freopen("in.txt","r",stdin);
    115     //freopen("out.txt","w",stdout);
    116     ///////////////////////////////////////////////////////////////////////
    117     //Add Code:
    118     int n,q,a,b,c;
    119     char ch[5];
    120     while(scanf("%d%d",&n,&q)!=EOF){
    121         build(1,n,1);
    122         while(q--){
    123             scanf("%s",ch);
    124             if(ch[0]=='C'){
    125                 scanf("%d%d%d",&a,&b,&c);
    126                 update(a,b,c,1,n,1);
    127             }
    128             else{
    129                 scanf("%d%d",&a,&b);
    130                 printf("%I64d
    ",query(a,b,1,n,1));
    131             }
    132         }
    133     }
    134     ///////////////////////////////////////////////////////////////////////
    135     return 0;
    136 }
    137 
    138 /**************************************************************************
    139 Testcase:
    140 Input:
    141 10 5
    142 1 2 3 4 5 6 7 8 9 10
    143 Q 4 4
    144 Q 1 10
    145 Q 2 4
    146 C 3 6 3
    147 Q 2 4
    148 Output:
    149 4
    150 55
    151 9
    152 15
    153 **************************************************************************/
  • 相关阅读:
    java中的异常类
    Mysql--JDBC的基础
    eclipse使用断言
    idea中使用断言
    java的null
    array,集合(collection),集合(list)的区别
    命名管道FIFO
    标准库中的管道操作
    现代进程间的通信方式--管道
    广播编程之发送者
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3356797.html
Copyright © 2011-2022 走看看