数据结构题。个人认为是比较好的数据结构题。题意:给定一个长度为n的数组a,然后给定m个操作序列,每个操作:l, r, x将区间[l, r]内的元素都增加a,然后有k个查询,查询形式是对于操作序列x,y是将第x个操作到第y个操作执行一遍。然后求最后的数组的元素值。

1.线段树解法:维护两棵线段树,一棵用于维护执行的操作序列的执行次数,一棵用于维护数组a的值。复杂度O(nlogn)。

2.扫描区间。对于数组和操作序列分别维护一个数组lx[],ly[]。ly[i]表示区间[i, m]中每个操作执行的次数,lx[i]表示区间[i, n]中每个数的增量的值。O(n)的复杂度。

 #include <stdio.h>
#include <string.h>
#define maxn 100005
#define lson(c) (c<<1)
#define rson(c) (c<<1|1)
#define mid(x, y) ((x+y)>>1)
typedef long long LL; struct Tree{
LL f[maxn*];
Tree(){
memset(f, , sizeof(f));
}
void init(){
memset(f, , sizeof(f));
}
void push_down(int c){
int l = lson(c), r = rson(c);
f[l] += f[c];
f[r] += f[c];
f[c] = ;
}
void update(int l, int r, int c, int lp, int rp, LL d){
if(lp <= l && r <= rp){
f[c] += d;
return ;
}
push_down(c);
int m = mid(l, r);
if(rp <= m) update(l, m, lson(c), lp, rp, d);
else if(lp > m) update(m + , r, rson(c), lp, rp, d);
else{
update(l, m, lson(c), lp, m, d);
update(m+, r, rson(c), m+, rp, d);
}
}
void query(int c, int l, int r, LL a[], int s){
if(l==r){
if(s)
a[l] = a[l]*f[c];
else a[l] = a[l] + f[c];
return ;
}
push_down(c);
int mid = mid(l, r);
query(lson(c), l, mid, a, s);
query(rson(c), mid+, r, a, s);
}
}insTree, arrTree;
LL a[maxn], ind[maxn];
int ls[maxn], rs[maxn]; int main(){
//freopen("test.in", "r", stdin);
for(int n, m, k; scanf("%d%d%d", &n, &m, &k)!=EOF; ){
insTree.init();
arrTree.init();
for(int i = ; i <= n; i ++){
scanf("%I64d", &a[i]);
}
for(int i = ; i <= m; i ++){
scanf("%d %d %I64d", &ls[i], &rs[i], &ind[i]);
}
for(int i = , x, y; i <= k; i ++){
scanf("%d%d", &x, &y);
insTree.update(, m, , x, y, );
}
insTree.query(, , m, ind, );
for(int i = ; i <= m; i ++){
arrTree.update(, n, , ls[i], rs[i], ind[i]);
}
arrTree.query(, , n, a, );
for(int i = ; i <= n; i ++){
printf("%I64d ", a[i]);
}
printf("\n");
}
}
 #include <stdio.h>
#include <string.h>
#define maxn 100005
typedef long long LL;
LL a[maxn], ind[maxn];
LL lx[maxn], ly[maxn];
int px[maxn], py[maxn]; int main(){
//freopen("test.in", "r", stdin);
for(int n, m, k; scanf("%d%d%d", &n, &m, &k)!=EOF; ){
memset(lx, , sizeof(lx));
memset(ly, , sizeof(ly));
for(int i = ; i <= n; i ++) scanf("%I64d", &a[i]);
for(int i = ; i <= m; i ++) scanf("%d%d%I64d", &px[i], &py[i], &ind[i]);
for(int i = , x, y; i <= k; i ++){
scanf("%d%d", &x, &y); lx[x] += , lx[y+] -= ;
}
LL s = ;
for(int i = ; i <= m; i ++){
s += lx[i];
ind[i] = ind[i] *s;
}
for(int i = ; i <= m; i ++){
ly[px[i]] += ind[i];
ly[py[i]+] -= ind[i];
}
s = ;
for(int i = ; i <= n; i ++){
s += ly[i];
printf("%I64d ", a[i] + s);
}
printf("\n");
}
return ;
}

Codeforces 296C Greg and Array的更多相关文章

  1. Codeforces 295A Greg and Array

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

  2. Greg and Array CodeForces 296C 差分数组

    Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...

  3. 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 ...

  4. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  5. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  6. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  7. G - Greg and Array CodeForces - 296C 差分+线段树

    题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...

  8. CodeForces Round #179 (295A) - Greg and Array

    题目链接:http://codeforces.com/problemset/problem/295/A 我的做法,两次线段树 #include <cstdio> #include < ...

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

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

随机推荐

  1. leetcode problem (2-4)

    Problem 2 --- Add Two Numbers 简单的模拟题. Problem 3 --- Longest Substring Without Repeating Characters 题 ...

  2. jquery 中的 $("#id") 与 document.getElementById("id") 的区别

    以前没注意过,认为jquery 中的 $("#air") 与 document.getElementById("air") 是一回事,指的是同一个东西.在今天写 ...

  3. Android中图片的异步加载

    转: 1.  为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2.  SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...

  4. obj-c 坑

    BOOL,使用8位存储空间,具有YES和NO值,如果赋值微长于8位的变量,那么只有低位字节会用作BOOL值,例如8960=0x2300,低8位为0,BOOL为NO.

  5. 破解EXCEL2007的密码

    破解EXCEL2007的密码 xshzhao (斑竹)顶楼举报 我有一个EXCEL2007文件(后缀是XLSX),由于设置了打开密码.现在密码搞忘了,这个文件对我很重要. 我试过了Office Pas ...

  6. MyEclipse过期激活方法

    如果已经过期会提示,进行购买.重新激活和退出,我们选择重新激活. 打开浏览器,地址栏输入key.858game.com,然后输入名称,在线获得MyEclipse的激活码. 输入Sumscripter: ...

  7. C语言中.h和.c文件解析(很精彩)

    C语言中.h和.c文件解析(很精彩)   简单的说其实要理解C文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程: 1.预处理阶段 2.词法与语法分析 ...

  8. python基础知识(引用)

    文章连接:http://xianglong.me/article/how-to-code-like-a-pythonista-idiomatic-python/

  9. Android使用pull解析xml

    一.理论准备     Pull解析器的运行方式与 SAX 解析器相似.它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件.跟SAX不同的是, ...

  10. Android 基础知识点(持续更新)

    一.AndroidManifest 每一个安卓工程都有AndroidManifest.xml的配置文件,在所有项目中该名称都不会变.该文件是Android工程的一个全局配置文件,所有在Android中 ...