题目大意:

有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. spark升级后 集成hbase-1.0.0-cdh5.4.5异常

    .具体场景如下: spark1.6  升级  spark2.2 后    分析查询hbase  数据报异常: 具体错误如下:       ERROR TableInputFormat: java.io ...

  2. popen() 使用举例 (转载)

    函数原型: #include "stdio.h" FILE *popen( const char* command, const char* mode ) 参数说明: comman ...

  3. 这是一个测试,测试markdown语法

    [TOC] 1. chpt1 这是一段话,前面没有空格 前面有4个空格,且在编辑状态下上面没有空行 前面有4个空格,且在编辑状态下上面有一个空行. 前面按了一下tab 1.1 1.1 2 段落1 前面 ...

  4. TensorFlow实验环境搭建

    初衷: 由于系统.平台的原因,网上有各种版本的tensorflow安装教程,基于linux的.mac的.windows的,各有不同,tensorflow的官网也给出了具体的安装命令.但实际上,即使te ...

  5. Winform DevExpress控件库(二) 使用SplashScreenManager控件定制程序加载页面

    SplashScreenManager控件:主要作用是显示在进行耗时操作时的等待界面: 位于 工具箱 -> Navigation & Layout(导航栏与布局类控件) 目录下: 在工具 ...

  6. ThreadLocal(线程绑定)

    为保证在DAO层里的操作都在同一事务里,我们曾使用以参数的形式将Connection向下传递的方式,而ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection ...

  7. android自定义View之3D索引效果

    效果图: 我的小霸王太卡了. 最近工作比较忙,今天搞了一下午才搞出来这个效果,这种效果有很多种实现方式,最常见的应该是用贝塞尔曲线实现的.今天我们来看另一种不同的实现方式,只需要用到 canvas.s ...

  8. 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果

    效果图: 所用的Ui就三张图: 案例代码: 结构 MainActivity.java package com.example.myapp; import android.content.Intent; ...

  9. Dynamics CRM Trigger plugin for N:N relationships

    博客原文:https://demystifyingcrm.wordpress.com/2014/12/17/trigger-plugin-for-nn-relationships-in-dynamic ...

  10. Mac 下安装运行Rocket.chat

    最近花了一周的时间,复习了HTML.CSS.原生JS,并学习了Node.js.CoffeeScript.js.MongoDB,入了下门. 因为准备在Rocket.chat 上做二次开发,所以先下载和安 ...