题目链接: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. openfalcon架构及相关服务配置详解

    一:openfalcon组件 1.falcon-agent 数据采集组件 agent内置了一个http接口,会自动采集预先定义的各种采集项,每隔60秒,push到transfer. 2.transfe ...

  2. Matplotlib python 基本用法

    1.简单的绘制函数 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1, 1, 50) y1 = x + 1 p ...

  3. # BZOJ5300 [CQOI2018]九连环 题解 | 高精度 FFT

    今天做了传说中的CQOI六道板子题--有了一种自己很巨的错觉(雾 题面 求n连环的最少步数,n <= 1e5. 题解 首先--我不会玩九连环-- 通过找规律(其实是百度搜索)可知,\(n\)连环 ...

  4. php多进程、IPC和事件驱动

    http://www.laruence.com/2008/04/21/101.html http://zhidao.baidu.com/link?url=zXm_12CxqGo-xYvOF4oyBJC ...

  5. luogu4933 大师 (dp)

    记f[i][j]是以i号为结尾的.公差为j的的个数(不包括只有i的情况) 那么就有$f[i][i-i']=\sum{(f[i'][i-i']+1)}$之类的东西 最后再加个n就行啦 而且公差有可能有负 ...

  6. Luogu 1084 NOIP2012 疫情控制 (二分,贪心,倍增)

    Luogu 1084 NOIP2012 疫情控制 (二分,贪心,倍增) Description H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树, 1 号城市是首都, 也是 ...

  7. Windows下安装Python扩展模块提示“Unable to find vcvarsall.bat”的问题

    本文内容 Unable to find vcvarsall.bat的问题描述 问题分析 总结 提示: 如果你只是想知道自己需要安装哪个版本的Visual Studio请直接查看本文最后一个小节的内容. ...

  8. Vue初学者可能不知道的坑

    1.setTimeout/ setInterval 场景一 :this指向改变无法用this访问vue实例 mounted(){ setTimeout( function () { //setInte ...

  9. 02-body标签中相关标签

    今日内容: 字体标签: h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...

  10. springboot集成mybatis-generator时候遇到的问题

    今天在集成mybatis自动生成内容的时候,出现了几个问题,解决了一个小时才搞完,都怪之前没有好好研究研究: 1.mysql-connector-java新驱动带来的问题? 当用比较新的sql驱动的时 ...