题目大意:

有N,M两个数

Q 表示查询, 后面两个数a b,表示查询从a 到b计算它们的和

C 表示增加   后面三个数a,b,c 表示从a开始,一直到b,每个数都增加c



除了查询要进行输出,增加不要输出

#include<iostream>
using namespace std;
#define LL(x)((x)<<1)
#define RR(x)((x)<<1|1)
struct_Seg_Tree{
   int left,right;
   long long sum;
   int add;
   int calmid(){
       return (left+right)/2;
   }
   int caldis(){
       return right-left+1;
   }
}tt[1000000];
int val[1000002];

long long build(int l,int r,int idx){
   tt[idx].left=l;
   tt[idx].right=r;
   tt[idx].add=0;
   if(l==r){
      return tt[idx].sum=val[l];
   }
   return tt[idx].sum=build(l,mid,LL(idx)+build(mid+1,r,RR(idx)));
}     

void update(int l,int r,int add,int idx){
   if(l<=tt[idx].left&&r>=tt[idx].right){
       tt[idx].add+=add;
       tt[idx].sum+=(long long)tt[idx].caldis()*add;//通过乘以后来添加的数
       return ;
   }
   if(tt[idx].add){
       tt[LL(idx).sum]+=(long long)tt[LL(idx)].caldis()*tt[idx].add;
       tt[RR(idx).sum]++(long long)tt[RR(idx)].caldis()*tt[idx].add;
       tt[LL(idx)].add+=tt[idx].add;
       tt[RR(idx)].add+=tt[idx].add;
       tt[idx].add=0; 

   }
   int mid=tt[idx].calmid();
   if(l<=mid) update(l,r,add,LL(idx));
   if(mid<r) update(l,r,add,RR(idx));
   tt[idx].sum = tt[LL(idx)].sum + tt[RR(idx)].sum;
}

long long query(int l,int r,int idx){
     if(l==tt[idx].left&&r==tt[idx].right){
        return tt[idx].sum;
     }
     if(tt[idx].add){
        tt[LL(idx)].sum += (long long )tt[LL(idx)].caldis() * tt[idx].add;
		tt[RR(idx)].sum += (long long)tt[RR(idx)].caldis() * tt[idx].add;
		tt[LL(idx)].add += tt[idx].add;
		tt[RR(idx)].add += tt[idx].add;
		tt[idx].add = 0;
     }
     int mid=tt[idx].calmid();
     if(r<=mid){
        return query(l,r,LL(idx));
     } else if(mid<1){
         return query(l,r,RR(idx));
     } else{
         return query(l,mid,LL(idx)+query(mid+1,r,RR(idx));
     }
}

int main(){
   int n,m;
   while(scanf("%d %d",&n,&m)==2){
       for(int i=1;i<=n;i++)
               scanf("%d",&val[i]);
       build(1,n,1);
       while(m--){
           char com[2];
           scanf("%s",com);
           if(com[0]=='Q'){
              int a,b;
              scanf("%d %d",&a,&b);
              pirntf("lld\n",query(a,b,1));
           }else{
              int a,b,c;
              scanf("%d %d %d",&a,&b,&c);
              update(a,b,c,1);
           }
       }
   }
}

PKU 3468 A Simple Problem with Integers的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  3. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  4. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  5. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  6. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  7. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  8. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

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

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

随机推荐

  1. JavaScript正则表达式模式匹配(1)——基本字符匹配

    var pattern=/g..gle/; //点符号表示匹配除了换行符外的任意字符 var str='g78gle'; alert(pattern.test(str)); var pattern=/ ...

  2. JAVA数组练习

    1 package com.zhang.hello; public class TestArray { public static int NO1(double [] score){ int coun ...

  3. 漫谈Web缓存架构

    计算机领域多处地方用到缓存,比如说为了缓解CPU和内存之间的速度不匹配问题,我们往往通过增加一级.二级.三级缓存,CPU先从缓存中取指令,如果取不到,再从内存中取,并更新缓存,同时,根据程序的局部性原 ...

  4. MySQL EXTRACT() 函数

    定义和用法 EXTRACT() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 EXTRACT(unit FROM date) date 参数是合法的日期表达式.unit 参 ...

  5. Microsoft Dynamics 365 Developer Toolkit下载地址

    下载,支持Visual Studio 2012, 2013, 2015

  6. 安卓高级2 Volley框架的使用案例

    初始化类: MyApp.java package qianfeng.com.day37_volley_pull.app; import android.app.Application; import ...

  7. [OpenCV]在显示窗口中截图

    [OpenCV]在显示窗口中截图 简介 介绍使用OpenCV实现简单的截图功能.首先阐述实现此功能的基本步骤,然后给出实现代码,最后贴出实验结果以及遇到的问题. 基本步骤 我们需要知道OpenCV使用 ...

  8. 团队项目必备神器——自定义Lint

    Lint 在android studio中内置了大概200个左右的lint检查,比如定义变量未使用,直接Handler报内存泄漏提醒,时时刻刻在监督着我们的代码.自己定制了一些Lint规则,项目开源在 ...

  9. Ajax PHP JavaScript MySQL实现简易的无刷新在线聊天室

    思路 消息显示区 发消息 板块 消息显示 消息发送 优化 显示非重复性的数据 优化显示 加上滚动条 每次都显示最新消息 完整代码 前端代码 数据库表结构 服务器端代码 总结与展望 总结 展望 为更好的 ...

  10. 修改CUSTOM.PLL文件调用客户化FORM&修改标准FORM

    修改custom.pll文件里 的过程event:参考例子如下,修改好后上传至$AU_TOP/resource 运行编译frmcmp_batch CUSTOM apps/apps module_typ ...