题目链接: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. Unity3d Shader开发(四)UsePass ,GrabPass ,SubShader Tags

    (一)UsePass 命令 使用 来自另一个着色器的命名通道. Syntax 语法 UsePass "Shader/Name" 插入所有来自给定着色器中的给定名字的通道.Shade ...

  2. js中构造字符串若放入Grails中gsp的<g:link>标签出错

    Grails的ajax使用json格式返回,在js中构造字符串时若放入<g:link>标签,字符串构造就会错误 如下就会发生错误,导致回调函数无法执行 function show(obj) ...

  3. C# const和statci readonly区别

    1.const 是属于编译时的变量,它定义的常量是在对象初始化时赋值,以后不能改变他的值. 它适用于两种场景:1.取值永久不变(比如圆周率.一天包含的小时数.地球的半径等)  2.对程序性能要求非常苛 ...

  4. 实时动态更新曲线图,x轴时间s随数据的变化而变化

    $(function () {    $(document).ready(function () {        Highcharts.setOptions({            global: ...

  5. Decision Boundaries for Deep Learning and other Machine Learning classifiers

    Decision Boundaries for Deep Learning and other Machine Learning classifiers H2O, one of the leading ...

  6. Linux按照时间查找文件

    linux按照时间查找文件 需要用到一个根据最后修改时间来处理的脚本. 前面有个有关find的基本用法,根据文件大小,类型什么的,这个是关于时间的.  linux 文件的三种时间(以 find 为例) ...

  7. 一位IT行业高收入者的理财规划方案

    一位IT行业高收入者的理财规划方案 http://zhuanlan.zhihu.com/invest/19670220 Alex · 12 天前 回望2013,这一年是极其不寻常的.理财浪潮席卷大江南 ...

  8. FZU 2140 Forever 0.5

     Problem 2140 Forever 0.5 Accept: 36    Submit: 113    Special JudgeTime Limit: 1000 mSec    Memory ...

  9. MyGui 3.2.0(OpenGL平台)的编译(五篇文章)

    MyGui是一个用来创建用户图形界面的库,用于游戏和3D应用程序.这个库的主要目标是达到:快速.灵活.易用. 1.下载准备: 源代码:http://svn.code.sf.net/p/my-gui/c ...

  10. 浏览器助手,请求拦截,后台模拟键鼠操作,页内嵌入JS

    http://www.cnblogs.com/ /// <summary>        /// 网页浏览器助手        /// 请求拦截,后台模拟键鼠操作,页内嵌入JS       ...