zoukankan      html  css  js  c++  java
  • poj3468(A Simple Problem with Integers)

    题目地址:A Simple Problem with Integers

    题目大意:

         给你N个数进行两种操作,Q,代表查询区间的和,C代表是在区间内所有的值都增加val值。结果可能大于2^32。

    解题思路:

         线段数,区间更新,区间求和。

    代码:

      1 #include <algorithm>
      2 #include <iostream>
      3 #include <sstream>
      4 #include <cstdlib>
      5 #include <cstring>
      6 #include <cstdio>
      7 #include <string>
      8 #include <bitset>
      9 #include <vector>
     10 #include <queue>
     11 #include <stack>
     12 #include <cmath>
     13 #include <list>
     14 //#include <map>
     15 #include <set>
     16 using namespace std;
     17 /***************************************/
     18 #define ll long long
     19 #define ll64 __ll64
     20 #define PI 3.1415927
     21 /***************************************/
     22 
     23 const ll M=100100;
     24 struct tree
     25 {
     26     ll sum;
     27     ll lazy,tag;
     28     ll left,right;
     29 } node[M*4];
     30 ll p[M];
     31 ll cnt;
     32 void build__tree(ll id,ll l,ll r)
     33 {
     34     ll mid=(l+r)/2;
     35     node[id].left=l;
     36     node[id].right=r;
     37     node[id].lazy=0;
     38     node[id].tag=0;
     39     if (l==r)
     40     {
     41         node[id].sum=p[l];
     42         return ;
     43     }
     44     build__tree(id*2,l,mid);
     45     build__tree(id*2+1,mid+1,r);
     46     node[id].sum=node[id*2].sum+node[id*2+1].sum;
     47 }
     48 void updata(ll id,ll l,ll r,ll v)
     49 {
     50     ll mid=(node[id].left+node[id].right)/2;
     51     if (node[id].left==l&&node[id].right==r)
     52     {
     53         node[id].lazy=1;
     54         node[id].tag+=v;
     55         node[id].sum+=(r-l+1)*v;
     56         return ;
     57     }
     58     if (node[id].lazy)
     59     {
     60         node[id].lazy=0;
     61         int t=node[id].tag;
     62         node[id].tag=0;
     63         updata(id*2,node[id].left,mid,t);
     64         updata(id*2+1,mid+1,node[id].right,t);
     65     }
     66     if (r<=mid)
     67         updata(id*2,l,r,v);
     68     else if (l>mid)
     69         updata(id*2+1,l,r,v);
     70     else
     71     {
     72         updata(id*2,l,mid,v);
     73         updata(id*2+1,mid+1,r,v);
     74     }
     75     node[id].sum=node[id*2].sum+node[id*2+1].sum;
     76 }
     77 void query(ll id,ll l,ll r)
     78 {
     79     ll mid=(node[id].left+node[id].right)/2;
     80     if (node[id].left==l&&node[id].right==r)
     81     {
     82         cnt+=node[id].sum;
     83         return ;
     84     }
     85     if (node[id].lazy)
     86     {
     87         node[id].lazy=0;
     88         int t=node[id].tag;
     89         node[id].tag=0;
     90         updata(id*2,node[id].left,mid,t);
     91         updata(id*2+1,mid+1,node[id].right,t);
     92     }
     93     if (r<=mid)
     94         query(id*2,l,r);
     95     else if (l>mid)
     96         query(id*2+1,l,r);
     97     else
     98     {
     99         query(id*2,l,mid);
    100         query(id*2+1,mid+1,r);
    101     }
    102 }
    103 int main()
    104 {
    105     ll n,q;
    106     while(scanf("%lld%lld",&n,&q)!=EOF)
    107     {
    108         ll i,j;
    109         for(i=1; i<=n; i++)
    110             scanf("%lld",&p[i]);
    111         build__tree(1,1,n);
    112         while(q--)
    113         {
    114             getchar();
    115             ll l,r,val;
    116             char c;
    117             scanf("%c",&c);
    118             if (c=='Q')
    119             {
    120                 cnt=0;
    121                 scanf("%lld%lld",&l,&r);
    122                 query(1,l,r);
    123                 printf("%lld
    ",cnt);
    124             }
    125             if (c=='C')
    126             {
    127                 scanf("%lld%lld%lld",&l,&r,&val);
    128                 updata(1,l,r,val);
    129             }
    130         }
    131     }
    132     return 0;
    133 }
    View Code
  • 相关阅读:
    网络对抗实验一
    计蒜课--顺序表查找、删除、遍历操作的复习
    实验六
    实验五
    实验四
    实验三
    python补码转源码
    教学设计的方法
    十、python进程和线程
    九、文件和异常
  • 原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/4065137.html
Copyright © 2011-2022 走看看