题目大意

给定一个序列a[1],a[2]……a[n]

接下来给出m种操作,每种操作是以下形式的:

l r d

表示把区间[l,r]内的每一个数都加上一个值d

之后有k个操作,每个操作是以下形式的:

x y

表示把第x种操作一直到第y种操作都执行一遍

最终输出在k个操作结束之后的序列

题目大意

就是线段树的成段更新嘛~~~先用线段树统计每种操作的次数,然后再执行m次成段更新,最后查询到底的查询即可~~~树状数组也可搞,似乎写起来还更简单些~~~还有一个更犀利的O(n)的算法,不过我暂时还没弄懂~~~这里是某大神的题解

代码

在update和query的时候穿参数传错了,把m和n搞混了,提交上去RE了,搞好久才找出错误。。。。o(╯□╰)o

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 111111
#define lson l,m,s<<1
#define rson m+1,r,s<<1|1
#define x first
#define y second
typedef long long LL;
pair<pair<int,int>,LL> op[MAXN];
LL a[MAXN],addv[MAXN<<2],sumv[MAXN<<2];
void PushDown(int s,int opr)
{
if(opr==1)
{
if(addv[s])
{
addv[s<<1]+=addv[s];
addv[s<<1|1]+=addv[s];
addv[s]=0;
}
}
else
{
if(sumv[s])
{
sumv[s<<1]+=sumv[s];
sumv[s<<1|1]+=sumv[s];
sumv[s]=0;
}
}
}
void update(int opr,int ql,int qr,LL d,int l,int r,int s)
{
if(ql<=l&&r<=qr)
{
if(opr==1)addv[s]+=d;
else
sumv[s]+=d;
return;
}
PushDown(s,opr);
int m=(l+r)>>1;
if(ql<=m) update(opr,ql,qr,d,lson);
if(qr>m) update(opr,ql,qr,d,rson);
}
LL query(int opr,int p,int l,int r,int s)
{
if(l==r)
{
if(opr==1)
return addv[s];
else
return sumv[s];
}
PushDown(s,opr);
int m=(l+r)>>1;
LL ans=0;
if(p<=m) ans=query(opr,p,lson);
else
ans=query(opr,p,rson);
return ans;
}
int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=m;i++) scanf("%d%d%d",&op[i].x.x,&op[i].x.y,&op[i].y);
while(k--)
{
int xx,yy;
scanf("%d%d",&xx,&yy);
update(2,xx,yy,1,1,m,1);
}
for(int i=1;i<=m;i++)
op[i].y*=query(2,i,1,m,1);
for(int i=1;i<=m;i++)
update(1,op[i].x.x,op[i].x.y,op[i].y,1,n,1);
for(int i=1;i<=n;i++)
printf("%I64d ",a[i]+query(1,i,1,n,1));
printf("\n");
return 0;
}

Codeforces295A - Greg and Array(线段树的成段更新)的更多相关文章

  1. hdu 1698 Just a Hook(线段树之 成段更新)

    Just a Hook                                                                             Time Limit: ...

  2. 线段树之成段更新( 需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候)

    HDU  1698 链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直 ...

  3. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  4. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  5. hdu 4614 Vases and Flowers(线段树:成段更新)

    线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...

  6. poj2528 Mayor's posters(线段树之成段更新)

    Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...

  7. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  8. 线段树(成段更新,区间求和lazy操作 )

    hdu1556 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. hdu1698 Just a Hook 线段树:成段替换,总区间求和

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem ...

随机推荐

  1. 八,WPF 命令

    WPF命令模型 ICommand接口 WPF命令模型的核心是System.Windows.Input.ICommand接口,该接口定义了命令的工作原理,它包含了两个方法和一个事件: public in ...

  2. 对WPF中MeasureOverride 和ArrangeOverride 浅理解

    以前对MeasureOverride 和ArrangeOverride十分费解,看到了这篇博文茅塞顿开~ public class CustomControl1 : Panel { /// <s ...

  3. PL/SQL — 函数

    函数通常用于返回特定的数据.其实质是一个有名字的PL/SQL块,作为一个schema对象存储于数据库,可以被反复执行.函数通常被作为一个表达式来调用或存储过程的一个参数,具有返回值.   一.建立函数 ...

  4. SQL中子查询为聚合函数时的优化

    测试数据:create table test1 as select * from dba_objects where rownum<=10000;--10000条记录create table t ...

  5. iostream/fstream中的输入输出流指针的绑定,tie函数的使用。

      为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...

  6. 安装ubuntu12.04LTS卡住以及花屏问题

    昨天在XP下用grub4dos安装了ubuntu12.04LTS,总体上还算比较顺利,中途有碰到两个异常问题,解决了记录一下. 问题一:安装过程中读取ISO镜像文件时,卡在"checking ...

  7. UGUI-组件

    2015-06-22 UGUI 组件 Canvas 画布 The Canvas component represents the abstract space in which the UI is l ...

  8. mysql的错误:The server quit without updating PID file /usr/local/mysql/data/door.pid).

    mysql错误解决: 先 参考:http://www.jb51.net/article/48625.htm 参考第四条: mysql在启动时没有指定配置文件时会使用/etc/my.cnf配置文件,请打 ...

  9. posix和system v有什么区别/?

    posix和system v有什么区别/?现在在应用时应用那一标准浮云484212 | 浏览 243 次 2014-11-06 10:362014-11-19 22:36 最佳答案们是有关信号量的两组 ...

  10. 上下切换js

    <div class="wview"> <span class="prevs" id="prevs-j"></ ...