题目链接: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 (线段树)的更多相关文章

  1. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  2. Gym - 101102C线段树

    Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from t ...

  3. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  4. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  5. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  6. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  7. 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  8. Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并

    Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  9. 【拓扑排序】【线段树】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 ...

随机推荐

  1. BZOJ2008 JSOI2010连通数(floyd+bitset)

    一直不明白为什么要用floyd求传递闭包,直接搜不是更快嘛……不过其实可以用bitset优化,方法也比较显然.bitset是真的神奇啊,好多01状态且转移相似的东西都可以用这个优化一下. #inclu ...

  2. ajax 调用 java webapi 多个参数(二)

    第一种方法:http://blog.csdn.net/hanjun0612/article/details/74436273 附上另一种解决方法. 这个方法主要针对  嵌套模型(模型中含有模型)的ap ...

  3. MT【16】证明无理数(2)

    证明:$sin10^0$为无理数. 分析:此处用$sin$的三倍角公式,结合多项式有有理根必须满足的系数之间的关系可以证明. 评:证明$sin9^0$为无理数就不那么简单.思路:先利用$sin54^0 ...

  4. HNOI2017影魔

    影魔 这么简单的方法尽然想不到,我是真的菜 对每个点,用单调栈的方式处理出他左右第一个比他大的数的位置,你可以把\(0\)和\(n+1\)设成\(inf\). 显然对于每对\(lef[i]\)和\(r ...

  5. 自学Python1.7-python变量以及类型

    自学Python之路 自学Python1.7-python 变量以及类型 1 变量是什么 变量是容器 2 变量的作用 存储数据到内存 3 为什么要用变量 存储数据方便后面引用 4 变量定义的规范 变量 ...

  6. 洛谷 P1054 等价表达式 解题报告

    P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...

  7. bzoj4817/luogu3703 树点涂色 (LCT+dfs序+线段树)

    我们发现,这个染色的操作他就很像LCT中access的操作(为什么??),然后就自然而然地想到,其实一个某条路径上的颜色数量,就是我们做一个只有access操作的LCT,这条路径经过的splay的数量 ...

  8. Unity3d-AngryBots实例解读

    最近粗略研究了下Unity3d自带的例子AngryBots,记录一下,部分内容摘自http://oulehui.blog.163.com/blog/static/7961469820125251051 ...

  9. A1047. Student List for Course

    Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course li ...

  10. apigateway-kong(七)配置说明

    这一部分应该在最开始介绍,但是我觉得在对kong有一定了解后再回头看下配置,会理解的更深刻.接下来对这个配置文件里的参数做个详细的解释便于更好的使用或优化kong网关. 目录 一.配置加载 二.验证配 ...