题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值。

分析:很明显我们不可能对区间的每个点都进行更新,不过我们可以使用一个op的开关,如果op等于0说明这个不需要更新,如果等于1就说明需要进行更新,这样只需要和插入的时候进行一下更新即可
***********************************************************************
注意:在向下更新的时候如果是更新子树有可能一个区间会更新多次,而子树区间更新的时候不能乘本区间的更新值,只能乘根节点的更新值,根节点的跟新值用完后归0;
 
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. #define maxn 100005
  8. #define Lson root<<1,L,tree[root].Mid()
  9. #define Rson root<<1|1,tree[root].Mid()+1,R
  10.  
  11. struct Tree//op等于0的时候子树不需要更新,op等于1需要更新
  12. {
  13.     int L, R, op;//需要向下更新的值为e
  14.     long long sum, e;//因为区间和比较大,所以使用long long 保存
  15.     int Mid(){return (L+R)/2;}
  16.     int Len(){return (R-L+1);}
  17. }tree[maxn*4];
  18. long long val[maxn];
  19.  
  20. void Down(int root)//向下更新
  21. {
  22.     if(tree[root].op && tree[root].!= tree[root].R)
  23.     {
  24.         tree[root].op = false;
  25.         tree[root<<1].op = tree[root<<1|1].op = true;
  26.         tree[root<<1].+= tree[root].e;
  27.         tree[root<<1|1].+= tree[root].e;
  28.  
  29.         tree[root<<1].sum += tree[root<<1].Len() * tree[root].e;
  30.         tree[root<<1|1].sum += tree[root<<1|1].Len() * tree[root].e;
  31.  
  32.         tree[root].= 0;
  33.     }
  34. }
  35. void Build(int root, int L, int R)
  36. {
  37.     tree[root].= L, tree[root].= R;
  38.     tree[root].op = false;
  39.  
  40.     if(== R)
  41.     {
  42.         tree[root].sum = val[L];
  43.         return ;
  44.     }
  45.  
  46.     Build(Lson);
  47.     Build(Rson);
  48.  
  49.     tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
  50. }
  51. void Insert(int root, int L, int R, long long e)
  52. {
  53.     Down(root);
  54.  
  55.     tree[root].sum += (R-L+1) * e;
  56.  
  57.     if(tree[root].== L && tree[root].== R)
  58.     {
  59.         tree[root].= e, tree[root].op = true;
  60.         return ;
  61.     }
  62.  
  63.     if(<= tree[root].Mid())
  64.         Insert(root<<1, L, R, e);
  65.     else if(> tree[root].Mid())
  66.         Insert(root<<1|1, L, R, e);
  67.     else
  68.     {
  69.         Insert(Lson, e);
  70.         Insert(Rson, e);
  71.     }
  72. }
  73. long long Query(int root, int L, int R)
  74. {
  75.     Down(root);
  76.  
  77.     if(tree[root].== L && tree[root].== R)
  78.         return tree[root].sum;
  79.     if(<= tree[root].Mid())
  80.         return Query(root<<1, L, R);
  81.     else if(> tree[root].Mid())
  82.         return Query(root<<1|1, L, R);
  83.     else
  84.         return Query(Lson) + Query(Rson);
  85. }
  86.  
  87. int main()
  88. {
  89.     int i, N, Q;
  90.  
  91.     while(scanf("%d%d", &N, &Q) != EOF)
  92.     {
  93.         for(i=1; i<=N; i++)
  94.             scanf("%I64d", &val[i]);
  95.         Build(1, 1, N);
  96.  
  97.         int a, b; char s[10];
  98.         long long c;
  99.  
  100.         while(Q--)
  101.         {
  102.             scanf("%s%d%d", s, &a, &b);
  103.  
  104.             if(s[0] == 'C')
  105.             {
  106.                 scanf("%I64d", &c);
  107.                 Insert(1, a, b, c);
  108.             }
  109.             else
  110.             {
  111.                 long long ans = Query(1, a, b);
  112.                 printf("%I64d\n", ans);
  113.             }
  114.         }
  115.     }
  116.  
  117.     return 0;
  118. }
  119.  
  120. /*
  121. 5 2
  122. 1 2 3 4 5
  123. C 1 5 5
  124. Q 1 1
  125. */

C - A Simple Problem with Integers - poj 3468(区间更新)的更多相关文章

  1. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 69589   ...

  2. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  3. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  4. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  6. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  8. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  9. A Simple Problem with Integers~POJ - 3468

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  10. A Simple Problem with Integers POJ - 3468 (分块)

    题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...

随机推荐

  1. POJ 1470 Closest Common Ancestors(LCA&RMQ)

    题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...

  2. 9.5noip模拟试题

    题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer.o ...

  3. codevs3008加工生产调度(Johnson算法)

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  4. jsp页面可以巧用模态框

    jsp页面使用模态框配合ajax出来的效果真的没话说,当然你也可以使用模态框配合action,但是在删除和更新的时候传值有点麻烦,用ajax 就没有这些问题 ,比如删除代码的时候在js文件中传值可以这 ...

  5. 关于异常的疑难解答:System.Runtime.InteropServices.COMException

    COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.&qu ...

  6. Excel.Application手册

    ----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明 ...

  7. dbms_job dbms_scheduler简单比较

    ---------------------------陈旧的-------------------------------------/*--------------------- 创建job --- ...

  8. 关于NSURL的一些属性的记录

    关于NSURL的一些属性的记录 NSLog(@"%@", request.URL.absoluteString); NSLog(@"%@", request.U ...

  9. Objective-C 类,函数调用

    // // main.m // L02HelloObjC // // Created by JinXin on 15/11/25. // Copyright © 2015年 JinXin. All r ...

  10. Best Time to Buy and Sell sock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...