题目链接: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. UML用户指南--UML图简介

    本节和大家一起学习一下UML图,这里主要介绍UML结构图和UML行为图两部分,下面让我们一起看一下UML图的详细介绍吧. UML图 这里再次提到对软件体系结构进行可视化.详述.构造和文档化,有5种最重 ...

  2. Mongodb FAQ 存储(storage)篇

    1.什么是内存映射文件(memory mapped files)? 内存映射文件是操作系统通过调用函数mmap()创建的一个放在内存中的一个数据文件.这种文件可以当做一个从零开始的内存或者数组,你可以 ...

  3. Codeforces Round #329 div2

    Problem_A(593A): 题意: 给n个单词, 每个单词由小写字母组成, 且长度<=1000. 组成一篇文章的要求是: 所有单词所用字母 <= 2 即最多只能有两个不同的字母. 求 ...

  4. maven编译的时候排除junit测试类

    maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...

  5. UVALive - 4287 Proving Equivalences

    给定n个命题之间的已经证明的关系如 a b表示已经证明蕴含式a→b,要求还需要再作多少次证明使得所有的命题都是等价的.将每个命题看成一个点,已经证明的命题之间连一条边,问题转化为添加多少条单向边使得图 ...

  6. [jobdu]树的子结构

    判断一棵树B是否是A的子树,对A做DFS,然后不断判断是否和B相同. 其实也可以不对A做DFS,直接遍历A中的每个节点和B做树的比较就行了. #include <iostream> #in ...

  7. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created via component-scanni ...

  8. Atom 扩展离线安装

    1.下载原始包 2.解压放入atom的packages文件夹中 3.通过nodejs的npm指令进行安装 运行->cmd 4.重启Atom就ok了

  9. C#中的cookie编程

    Cookie就是所谓的" 小甜饼" ,他最早出现是在Netscape Navigator 2.0中.Cookie其实就是由Web服务器创建的.将信息存储在计算机上的文件.那么为什么 ...

  10. c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。

    Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...