题目链接: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. jenkins--svn+Email自动触发3(jenkins全局设置)

    全局java配置: 全局sonar-scanner插件配置:

  2. BZOJ3589 动态树(树链剖分+容斥原理)

    显然容斥后转化为求树链的交.这个题非常良心的保证了查询的路径都是到祖先的,求交就很休闲了. #include<iostream> #include<cstdio> #inclu ...

  3. BZOJ3998 TJOI2015弦论(后缀数组+二分答案)

    先看t=1的情况.显然得求出SA(因为我不会SAM).我们一位位地确定答案.设填到了第len位,二分这一位填什么之后,在已经确定的答案所在的范围(SA上的某段区间)内二分,找到最后一个小于当前串的后缀 ...

  4. day21 正则表达式

    正则表达式 简单的范围的字符组 0-9 匹配所有的数字 a-z 匹配所有的小写字母 A-Z 匹配所有的大写字母 A-Za-z 匹配所有的字母 字符 . 换行符以外的任意字符 \w word 匹配数字, ...

  5. Java中字符串string的数据类型

    Java中字符串string的数据类型 时间:2017-07-03 08:01:47 YuanMxy 原文:https://blog.csdn.net/YuanMxy/article/details/ ...

  6. luogu1514 [NOIp2010]引水入城 (bfs+记忆化搜索)

    我们先bfs一下看看是否能到最底下的所有点 如果不能的话,直接把不能到的那几个数一数就行了 如果能的话: 可以发现(并不可以)某格能到达的最底下的格子一定是一个连续的区间 (因为如果不连续的话,我们先 ...

  7. SPOJ GSS系列

    众所周知的仅次于ynoi的毒瘤数据结构系列.(跟Qtree系列并列?) GSS1: 长度为 $n$ 的序列 $a$,$m$ 个询问,每次询问区间 $[l,r]$ 之间的最大子段和. $1\le n,m ...

  8. CPP--借助神器VS理解内存存储(含大小端对齐)

    单位,补码之类的可以看这个:http://www.cnblogs.com/dotnetcrazy/p/8178175.html 先说说大小端对齐的事情,然后再看: 内存最小单位==>Byte,i ...

  9. Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)

    Luogu 1080 [NOIP2012]国王游戏 (贪心,高精度) Description 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己 ...

  10. <? extends T>和<? super T>的理解

    背景:对泛型中使用super和extends关键字进行分析总结. 问题: public class TestExtend { public static void main(String[] args ...