数据结构题。个人认为是比较好的数据结构题。题意:给定一个长度为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. Php之Http请求urlencode/rawurlencode

    在http请求中,如果参数值带中文最好使用urlencode/rawurlencode函数. 如果参数值中带加号也最好使用,否则后台接收时,+号变成空格,引起不必要的麻烦. 注:urlencode和r ...

  2. jdk环境变量配置(总结)

    进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),我下载的版本是 ...

  3. PHP设计模式之工厂/单例/注册者模式

    工厂模式 简单工厂模式 [静态工厂方法模式](Static Factory Method)是类的创建模式 工厂模式的几种形态: 1.简单工厂模式(Simple Factory)又叫做 静态工厂方法模式 ...

  4. Mac下安装Scrapy

    执行 sudo pip install Scrapy 时会碰到libxml/xmlversion file no found 解决方法: brew install libxml2 brew insta ...

  5. 用vld分析C++内存泄漏

    最近发现项目里有内存泄漏,到网上搜了一些资料,决定用vld来分析解决这个问题. 直接在vs里的"工具和扩展"中搜索vld,然后下载安装就可以了. 我下载下来的是个exe文件,安装在 ...

  6. zzuli oj 1167逆转数(指针专题)

    Description 任意给你一个整数,这个数可能很大(最长不超过100位),你能求出它的逆转数吗?  逆转数定义如下:  1.一个末尾没有0的整数,它的逆转数就是各位数字逆序输出:  2.一个负数 ...

  7. 用powerdesigner建模工具生成数据库

    1,使用powerdesignerP:主键 F:外键 M:是否可为空 2,两张表建立主外键关系时,主外键名称要相同.比如A表 A_id name  说明id(P) B表 B_id name  说明id ...

  8. Excel Skill (1) -- 判断时如何去掉框里的空格

    使用命令 TRIM 说明: Purpose. Remove extra spaces from text. Text with extra spaces removed. =TRIM (text) t ...

  9. [刷机教程] 三星Note8 N5100不卡屏的唯一解决办法--落雨刷机教程

    首先我自己写了一个word,在附件里.大概23页,图文并茂.附带三星NOTE8 N5100 MD2下载包 刷机要谨慎啊,小伙伴们. 刷机教程已经上传到我本人的网站:点击进入去看吧.和word一样. h ...

  10. Ubuntu修改语言环境为英文

    转自把语言环境变量改为英文 将Ubuntu系统语言环境改为英文的en_US.UTF-8 查看当前系统语言环境 locale 编辑配置文件,将zh_US.UTF-8改为en_US.UTF-8,zh改为e ...