数据结构题。个人认为是比较好的数据结构题。题意:给定一个长度为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. tmux与vim主题不一致

    在centos6.5 x64 vim6.2 需要在tmux.conf中添加set -g default-terminal "screen-256color" 然后再次启动tmux的 ...

  2. JS分段上传文件(File)并使用MD5.js加密文件段用来后台校验

    HTML <form method="POST" name="form1" action="/mupload/upload/" enc ...

  3. 『奇葩问题集锦』Zepto 页面唤醒拨号功能点透

    不废话直接上代码: HTML: <a class="js-tel tel" data-tel="1312414"></a> JS: // ...

  4. php发送http请求

    http请求有get,post. php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]. file_get_contents();详情见:http://www.cnblogs.com/ ...

  5. 【python】元组的插入

    >>> temp=(1,2,3,4,5)>>> temp=temp[:2]+(8,)+temp[2:]>>> temp(1, 2, 8, 3, 4 ...

  6. linux安装ruby ruby-devel rubygems bundler

    linux安装ruby ruby-devel rubygems yum install ruby ruby-devel rubygems 安装bundler gem install bundleror ...

  7. C语言-06复杂数据类型-04 结构体

    结构体的说明(构造类型) 数组:只能由多个相同类型的数据构成 结构体:可以由多个不同类型的数据构成 ,结构体的类型是不存在的,自己定义 int main() { // 1.定义结构体类型 定义类型的时 ...

  8. 常见内部函数----Python

    In [10]: test = [1,2,"yes"] ---append(x) 追加到链尾 In [11]: test.append(1) In [12]: test Out[1 ...

  9. UISearchBar -- 备忘

    搜索功能的备忘 UISearchBar UISearchBar是一个搜索栏,继承自UIView,也是常用的控件之一,所以特别写一篇备忘方便以后做工具文章. 例子: let searchBar = UI ...

  10. Http协议Get方式获取图片

    一.                二.                         我试了试,Post方式也行啊,干嘛要叫强调Get方式,费解~~       答曰:get是向服务器请求数据,p ...