【金色】种瓜得瓜,种豆得豆 Gym - 102072H (线段树)
题目链接:https://cn.vjudge.net/problem/Gym-102072H
题目大意:中文题目
具体思路:通过两棵线段树来维护,第一棵线段树来维护当前坐标的点的日增长速度(默认每一年的增长速度都是当前年份的增长速度),对于第一棵线段树,肯定会存在多算的情况,那么我们第二棵线段树就维护每一个点的多算的情况就可以了。
举个例子:当前的n只有1,初始值也是1, 三个操作,第一年加一个,第二年加一个,第三年加一个。然后问你第四年的当前的个数。在第二年的时候增长速度还是1,第三年的增加速度就是2,第四年的增长速度就是3。对于第四年的话,第一棵线段树的结果出来的是3,在乘上年份就是3*4=12,这个12指的是四年的产量按照每一年的增长速度都是3计算的,然后我们通过第二棵线段树减去不合法的情况,分别是第二年和第三年多算了,我们再通过12-3-2就能解出正确的答案了。
感谢qyn的讲解。
AC代码:
#include<iostream>
#include<stack>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
# define ll long long
# define lson l,m,rt<<
# define rson m+,r, rt<<|
const int maxn = 2e5+;
ll tree1[maxn<<],tree2[maxn<<];
ll lazy1[maxn<<],lazy2[maxn<<];
ll sto[maxn];
void up(ll rt)
{
tree1[rt]=tree1[rt<<]+tree1[rt<<|];
tree2[rt]=tree2[rt<<]+tree2[rt<<|];
}
void buildtree(ll l,ll r,ll rt)
{
tree1[rt]=tree2[rt]=;
lazy1[rt]=lazy2[rt]=;
if(l==r)
{
scanf("%lld",&tree1[rt]);
return ;
}
ll m=(l+r)>>;
buildtree(lson);
buildtree(rson);
up(rt);
}
void down(ll rt,ll l,ll r)
{
ll mid=(l+r)>>;
lazy1[rt<<]+=lazy1[rt];
lazy1[rt<<|]+=lazy1[rt];
tree1[rt<<]+=lazy1[rt]*(mid-l+);
tree1[rt<<|]+=lazy1[rt]*(r-mid);
lazy1[rt]=;
lazy2[rt<<]+=lazy2[rt];
lazy2[rt<<|]+=lazy2[rt]; tree2[rt<<]+=lazy2[rt]*(mid-l+);
tree2[rt<<|]+=lazy2[rt]*(r-mid);
lazy2[rt]=;
}
void update(ll l,ll r,ll rt,ll L,ll R,ll year)
{
if(L<=l&&R>=r)
{
tree1[rt]+=(r-l+);
lazy1[rt]+=;
tree2[rt]+=(r-l+)*year;
lazy2[rt]+=year;
return ;
}
down(rt,l,r);
ll m=(l+r)>>;
if(L<=m)
update(lson,L,R,year);
if(R>m)
update(rson,L,R,year);
up(rt);
}
ll query(ll l,ll r,ll rt,ll L,ll R,ll year)
{
if(R<l||L>r)
return ;
if(L<=l&&R>=r)
{
return tree1[rt]*year-tree2[rt];
}
down(rt,l,r);
ll m=(l+r)>>;
return query(lson,L,R,year)+query(rson,L,R,year);
up(rt);
}
int main()
{
//freopen("data1.out","r",stdin);
ll n;
while(~scanf("%lld",&n))
{
buildtree(,n,);
ll year=;
int m;
char str[];
ll st,ed,num=;
scanf("%d",&m);
while(m--)
{
scanf("%s %lld %lld",str,&st,&ed);
year++;
if(str[]=='Q')
{
ll ans=query(,n,,st,ed,year);
sto[++num]=ans;
}
else
{
update(,n,,st,ed,year);
}
}
for(int i=; i<=num; i++)
{
if(i==)
printf("%lld",sto[i]);
else
printf(" %lld",sto[i]);
}
printf("\n");
}
return ;
}
【金色】种瓜得瓜,种豆得豆 Gym - 102072H (线段树)的更多相关文章
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- Gym - 101102C线段树
Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from t ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并
Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
随机推荐
- BZOJ4822[Cqoi2017]老C的任务——树状数组(二维数点)
题目描述 老 C 是个程序员. 最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C 轻松 地完成了系统的大部分功能,并把其中一个功能交给你来实 ...
- HDU4288-STL模拟
给你插入,删除的操作,维护一个降序排列的表,查询所有下标模5等于3的元素和. 2012年成都网络赛的一道签到题.但是还是搜了题解才做出来了,意志不坚定啊... 这道题给10s的时间,做法多种多样. 最 ...
- TCP的三次握手与四次挥手过程,各个状态名称与含义
三次握手 第一次握手:主机A发送位码为syn=1,随机产生seq number=10001的数据包到服务器,主机B由SYN=1知道,A要求建立联机,此时状态为SYN_SENT: 第二次握手:主机B收到 ...
- JavaScript 数据类型检测总结
JavaScript 数据类型检测总结 原文:https://blog.csdn.net/q3254421/article/details/85483462 在js中,有四种用于检测数据类型的方式,分 ...
- 自学Linux Shell12.4-for命令
点击返回 自学Linux命令行与Shell脚本之路 12.4-for命令 1. for命令 格式一 for var in list do commands done 格式二 for var in li ...
- Azure Powershell script检测登陆并部署ARM Template
本文简单提供了一个Azure powershell脚本,能实现如下功能 Azure (China)账户是否已经登陆了,如果没登陆,会提示你登陆. 要创建的资源组是否存在,存在的话不再创建,直接部署te ...
- TextView 借助Linkify,使用自定义模式设置链接
http://my.oschina.net/fengheju/blog/176105 TextView是android中的一个比较常用的控件,它有一个非常有趣的特性,可以通过android:autoL ...
- c++ io标准库2
转自:http://www.2cto.com/kf/201110/109445.html 接下来我们来学习一下串流类的基础知识,什么叫串流类? 简单的理解就是能够控制字符串类型对象进行输入输出的类,C ...
- (转)Maven中的DependencyManagement和pluginmanagement
背景:最近在学习maven的多模块构建过程中看到DependencyManagement选项,对这个选项的使用做个记录! 区别与联系 这里介绍一个在父项目中的根结点中声明dependencyManag ...
- list 删除一个元素的三种做法--python
我们以一个字符串为元素类型的 list 为例,进行列表元素的删除: l = ['no surfing', 'flippers'] 法一:remove(val) >>> l.remov ...