CodeForces Round #179 (295A) - Greg and Array
题目链接: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的更多相关文章
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l , op.r , op.c= times* ...
- 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 ...
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #179 (Div. 1 + Div. 2)
A. Yaroslav and Permutations 值相同的个数不能超过\(\lfloor \frac{n + 1}{2} \rfloor\). B. Yaroslav and Two Stri ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- 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 ...
- 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 ...
- Codeforces Round #616 (Div. 2) B. Array Sharpening
t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...
- 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 ...
随机推荐
- 【JSF框架】 是一种标准
典型的JSF应用程序包含下列部分: 一组JSP页面 一组后台bean(为在一个页面上的UI组件定义的属性和函数的JavaBean组件) 应用程序配置资源文件(定义页面导航规则.配置bean和其它的自定 ...
- uitableviewcell 自适应大小 参考
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ...
- 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 ...
- zepto源码学习-03 $()
在第一篇的时候提到过关于$()的用法,一个接口有很多重载,用法有很多种,总结了下,大概有一以下几种 1.$(selector,context?) 传入一个选择器返回一个zepto对象 2.$(func ...
- Java使用socket实现两人聊天对话
import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /* ...
- UVA 11294 Wedding
给n对夫妇安排座位,其中0h,0w分别表示新郎,新娘.同一对新郎,新娘不能坐在同一侧,而且互为通奸关系的人不能同时坐在新娘对面. 这道题目真是掉尽节操啊,,,欧美的氛围还是比较开放的. 分析: 首先说 ...
- ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件
一.简介 1.在onListItemClick()中new Intent,Intent以存储序列化后的mp2Info对象作为参数,启动serivce 2.DownloadService在onStart ...
- 【Quick 3.3】资源脚本加密及热更新(一)脚本加密
[Quick 3.3]资源脚本加密及热更新(一)脚本加密 注:本文基于Quick-cocos2dx-3.3版本编写 一.脚本加密 quick框架已经封装好加密模块,与加密有关的文件在引擎目录/quic ...
- ruby 方法重载
class MyClass def sayHello return "hello from MyClass" end def sayGoodbye return "Goo ...
- Android开发UI之自定义视图属性
Android框架中,所有自定义的view类都继承自View,也可以继承Button等view的子类 为了允许ADT能够与view交互,必须提供一个能够获取Context和作为属性的Attribute ...