题目链接:http://codeforces.com/problemset/problem/295/A

我的做法,两次线段树

#include <cstdio>
#include <cstring> const int N = 100005; long long sumv[N * 3];
long long add[N * 3];
long long a[N]; struct opera {
int l, r;
long long d;
} op[N]; void pushdown(int o, int l, int r)
{
int m = (l + r) >> 1;
sumv[o << 1] += add[o] * (m - l + 1);
add[o << 1] += add[o];
sumv[o << 1 | 1] += add[o] * (r - m);
add[o << 1 | 1] += add[o];
add[o] = 0;
} int ql, qr;
long long val;
void update(int o, int l, int r)
{
if (ql <= l && qr >= r) {
sumv[o] += (r - l + 1) * val;
add[o] += val;
return ;
}
if (add[o]) pushdown(o, l, r);
int m = (l + r) >> 1;
if (m < qr) update(o << 1 | 1, m + 1, r);
if (m >= ql) update(o << 1, l, m);
sumv[o] = sumv[o << 1] + sumv[o << 1 | 1];
} int q;
long long query(int o, int l, int r)
{
if (l == r) return sumv[o];
if (add[o]) pushdown(o, l, r);
int m = (l + r) >> 1;
if (m < q) return query(o << 1 | 1, m + 1, r);
else return query(o << 1, l, m);
} int main()
{
//freopen("a.in", "r", stdin); int n, m, k;
int i;
scanf("%d%d%d", &n, &m, &k);
for (i = 1; i <= n; ++i) scanf("%lld", a + i);
for (i = 1; i <= m; ++i) {
scanf("%d%d%lld", &op[i].l, &op[i].r, &op[i].d);
}
val = 1;
for (i = 1; i <= k; ++i) {
scanf("%d%d", &ql, &qr);
update(1, 1, m);
}
for (i = 1; i <= m; ++i) {
q = i;
op[i].d *= query(1, 1, m);
}
memset(sumv, 0, sizeof sumv);
memset(add, 0, sizeof add);
for (i = 1; i <= m; ++i) {
ql = op[i].l;
qr = op[i].r;
val = op[i].d;
update(1, 1, n);
}
for (i = 1; i <= n; ++i) {
q = i;
printf("%lld", a[i] + query(1, 1, n));
if (i != n) printf(" ");
}
return 0;
}

  

后来看了学长的代码,又写了一遍.......:

#include <cstdio>

const int N = 100005;

long long l[N];
long long r[N];
long long d[N];
long long a[N];
long long op[N];
long long b[N]; main()
{
//freopen("in.txt", "r", stdin);
long long n, m, k;
scanf("%lld%lld%lld", &n, &m, &k);
for (int i = 1; i <= n; ++i)
scanf("%lld", &a[i]);
for (int i = 1; i <= m; ++i)
scanf("%lld%lld%lld", &l[i], &r[i], &d[i]);
long long x, y;
for (int i = 1; i <= k; ++i) {
scanf("%lld%lld", &x, &y);
++op[x];
--op[y+1];
}
for (int i = 1; i <= m; ++i) {
op[i] += op[i - 1];
d[i] *= op[i];
}
for (int i = 1; i <= m; ++i) {
b[l[i]] += d[i];
b[r[i]+1] -= d[i];
}
for (int i = 1; i <= n; ++i) {
b[i] += b[i - 1];
a[i] += b[i];
}
printf("%lld", a[1]);
for (int i = 2; i <= n; ++i)
printf(" %lld", a[i]);
}

  

CodeForces Round #179 (295A) - Greg and Array的更多相关文章

  1. CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用

    线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l  , op.r , op.c= times* ...

  2. Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改

    A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...

  3. Codeforces 295A Greg and Array

    传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...

  4. Codeforces Round #179 (Div. 1 + Div. 2)

    A. Yaroslav and Permutations 值相同的个数不能超过\(\lfloor \frac{n + 1}{2} \rfloor\). B. Yaroslav and Two Stri ...

  5. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  6. Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配

    题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...

  7. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  8. Codeforces Round #616 (Div. 2) B. Array Sharpening

    t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...

  9. Codeforces Round #617 (Div. 3)A. Array with Odd Sum(水题)

    You are given an array aa consisting of nn integers. In one move, you can choose two indices 1≤i,j≤n ...

随机推荐

  1. linux下i2c驱动笔记 转

    1. 几个基本概念 1.1. 设备模型 由 总线(bus_type) + 设备(device) + 驱动(device_driver) 组成,在该模型下,所有的设备通过总线连接起来,即使有些设备没有连 ...

  2. CODEVS 3943 数学奇才琪露诺

    [题目描述 Description] 作为上白泽慧音老师的出色弟子,数学奇才琪露诺在算术方面有很深的造诣.今天,codevs有幸请到了这位数学界的奇葩作为本场考试的第一题主考官. 琪露诺喜欢0-9之间 ...

  3. c语言通过89C51驱动1602液晶显示(入门级别)

    工具proteus,keil 步骤: 1.画好电路图 2.在指令模式下,设置好显示模式以及光标位置 3.在写数据模式下,向1602写入显示字符(1602只能显示数字和字符) 电路图 #include ...

  4. java连接数据库时出现如下错误:

    java.lang.ClassNotFoundException: com.mysql.jdbc.driver at org.apache.catalina.loader.WebappClassLoa ...

  5. sum(iterable[, start]) 对集合求和

    >>> LL [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] >>> L [3, 4, 5, 6, 7, 8, 9] >> ...

  6. Weex详解:灵活的移动端高性能动态化方案

    原文地址:http://www.infoq.com/cn/articles/introducing-weex 在2016年4月份的QCon上,阿里巴巴资深总监,淘宝移动平台及新业务事业部.阿里百川负责 ...

  7. PHP漏洞全解(三)-客户端脚本植入

    本文主要介绍针对PHP网站的客户端脚本植入攻击方式.所谓客户端脚本植入攻击,是指将可以执行的脚本插入到表单.图片.动画或超链接文字等对象内.当用户打开这些对象后,黑客所植入的脚本就会被执行,进而开始攻 ...

  8. const变量的存储区及修改权限

    转自const变量的存储区及修改权限 [cpp] view plaincopy const int a = 1; int *p = const_cast<int*>(&a); *p ...

  9. java客户端连接MongoDB数据库的简单使用

    1.下载mongoDB的jar包,并引入到工程的CLASSPATH中下载:mongodb2.5驱动包下载 如果使用maven项目,最新的依赖如下: <dependency> <gro ...

  10. minicom installation and configuration on ubuntu

    minicom是一个串口通信工具,就像Windows下的超级终端,可用来与串口设备通信.minicom完全通过键盘实现操作. install sudo apt-get install minicom ...