Development Value

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 609    Accepted Submission(s): 118

Problem Description
StarCraft 2 (SC2) is a famous game. More and more people fall in love with this game.




As a crazy fan of SC2, Ahua (flower fairy) play it day and night. Recently, he found that the most important part of being a top player of SC2 is economic development, which means you should get as much mine as possible by training SCVs (space construction
vehicle) to collect mine. Train a SCV at ith second costs Ci units of mine. After training, this SCV can collect Di units of mine each second. Training a SCV needs one second of time.



Based on that, he composes a formula to evaluate the development in a time span from xth second to yth second. Assume at xth second, Ahua has no SCV and mine. He trains one SCV at each second during xth second and yth second (the mount of mine can be negative,
so that he always can train SCV). Each SCV will collect some amount of mines for Ahua in each second after it was trained. At ith second Ahua has Mi units of mine in total. The development value is defined as sum(Mi) (x ≤ i ≤ y). Now he asks you to help him
calculate the development value. To make it more interesting, Ahua can apply following operations:



Cost x y z: the cost of training a SCV between xth second to yth second will increase by z units of mine. i.e. Ci for x ≤ i ≤ y will increase by z.



Collect x y z: each SCV trained between xth second and yth second can collect z more mines every second after it has been trained. i.e. Di for x ≤ i ≤ y will increase by z.



Query x y: output the development value between xth second and yth second.
 
Input
First line of the input is a single integer T (T ≤ 10), indicates there are T test cases.

For each test case, the first line is an integer N (1 ≤ N ≤ 100000), means the maximum time you should deal with.



Following N lines, each contain two integers Ci and Di (0 ≤ Ci, Di ≤ 100000), the cost and collect speed of SCV training in ith second initially as described above.



The next line is an integer Q (1 ≤ Q ≤ 10000), the number of operations you should deal with. Then Q lines followed, each line will be “Cost x y z”, "Collect x y z” or “Query x y”.

1 ≤ x ≤ y ≤ N, 0 ≤ z ≤ 100000
 
Output
For each test case, first output “Case k: “ in a single line, k is the number of the test case, from 1 to T. Then for each "Q x y", you should output a single line contains the answer mod 20110911.
 
Sample Input
1
5
1 3
2 3
3 1
2 2
3 3
5
Query 1 3
Cost 2 2 1
Query 1 3
Collect 1 1 5
Query 1 3
 
Sample Output
Case 1:
2
0
15
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4038 4036 4039 4032 4033 
 

题意:

有一个游戏。里面要造矿兵。在第i秒造矿兵须要花费c[i]。然后之后的时间每秒该矿兵都会採d[i]的矿。然后询问。从x秒到y秒。

每秒造一个矿兵.(在x秒的时候矿兵和矿都为0.可是矿能够为负数)。然后定义了一个mi。

表示第i秒时的总矿数。然后要你输出.Σmi(x<=i<=y)。

思路:

先推公式。

1,考虑花费

时刻j  从x时刻到j时刻造农民的总花费

x  C(x)

x+1  C(x)+C(x+1)

x+2  C(x)+C(x+1)+C(x+2)

......

y  C(x)+C(x+1)+...+C(y)

对第二栏求和。每一列是C(i)*(y-i+1),再对这个从x到y求和。得sigma(C(i)*(y+1-i))

分成两项(y+1)*sigma(C(i))-sigma(C(i)*i)



2,考虑採矿

对于i时刻被造出的农民。到j时刻总共採的矿数是D(i)*(j-i),对这个从x到j求和就是j时刻之前造的农民到j时刻为止总共採的矿数,即sigma(D(i)*(j-i))(对i从x到j求和)。再对j从x到y求和就是答案。

可是这个形式的求和式不适合用线段树维护。做些变形:

时刻j sigma(D(i)*(j-i))

x   D(x)*0

x+1  D(x)*1+D(x+1)*0

x+2  D(x)*2+D(x+1)*1+D(x+2)*0

......

y  D(x)*(y-x)+D(x+1)*(y-x-1)+......+D(y-1)*1+D(y)*0

对第二栏求和,每列是D(i)*(y-i)*(y-i+1)/2。再对这个从x到y求和。sigma(D(i)*(y-i)*(y-i+1)/2).

把和式拆成几项方便维护:1/2*( y*(y+1)sigma(D(i)) - (2*y+1)sigma(D(i)*i) + sigma(D(i)*i^2))

然后最后的答案就是採矿-花费。

然后仅仅须要用一颗线段树来维护。

sigma(ci),sigma(i*ci),sigma(di),sigma(i*di),sigma(i*i*di).

然后按一般的更新查询就完了。

对于除二取模的问题。

(1)模数乘2。全部中间过程直接取模。最后得数/2

(2)直接取模。最后答案是ret,假设ret是偶数,答案是ret/2,假设是奇数,答案是(ret + mod) / 2

具体见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
const ll mod=20110911*2;
#define lson L,mid,ls
#define rson mid+1,R,rs
ll sm[maxn],ss[maxn],sc[maxn<<2],sd[maxn<<2],siid[maxn<<2];
ll sic[maxn<<2],sid[maxn<<2],ac[maxn<<2],ad[maxn<<2];
ll asc,aic,asd,aid,aiid;
void addc(int L,int R,int rt,ll d)
{
ac[rt]=(ac[rt]+d)%mod;
sc[rt]=(sc[rt]+(R-L+1)*d)%mod;
sic[rt]=(sic[rt]+(sm[R]-sm[L-1])*d)%mod;
}
void addd(int L,int R,int rt,ll d)
{
ad[rt]=(ad[rt]+d)%mod;
sd[rt]=(sd[rt]+(R-L+1)*d)%mod;
sid[rt]=(sid[rt]+(sm[R]-sm[L-1])*d)%mod;
siid[rt]=(siid[rt]+(ss[R]-ss[L-1])*d)%mod;
}
void PushDown(int L,int R,int rt)
{
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
if(ad[rt])
addd(lson,ad[rt]),addd(rson,ad[rt]),ad[rt]=0;
if(ac[rt])
addc(lson,ac[rt]),addc(rson,ac[rt]),ac[rt]=0;
}
void PushUp(int rt)
{
int ls=rt<<1,rs=ls|1;
sc[rt]=(sc[ls]+sc[rs])%mod;
sic[rt]=(sic[ls]+sic[rs])%mod;
sd[rt]=(sd[ls]+sd[rs])%mod;
sid[rt]=(sid[ls]+sid[rs])%mod;
siid[rt]=(siid[ls]+siid[rs])%mod;
}
void build(int L,int R,int rt)
{
ac[rt]=ad[rt]=0;
if(L==R)
{
scanf("%I64d%I64d",&sc[rt],&sd[rt]);
sic[rt]=(L*sc[rt])%mod;
sid[rt]=(L*sd[rt])%mod;
siid[rt]=(sid[rt]*L)%mod;
return;
}
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int rt,int l,int r,ll d,int op)
{
if(l<=L&&R<=r)
{
if(op)
addd(L,R,rt,d);
else
addc(L,R,rt,d);
return;
}
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
PushDown(L,R,rt);
if(l<=mid)
update(lson,l,r,d,op);
if(r>mid)
update(rson,l,r,d,op);
PushUp(rt);
//printf("%d->%d sc")
}
void qu(int L,int R,int rt,int l,int r)
{
if(l<=L&&R<=r)
{
asc=(asc+sc[rt])%mod;
aic=(aic+sic[rt])%mod;
asd=(asd+sd[rt])%mod;
aid=(aid+sid[rt])%mod;
aiid=(aiid+siid[rt])%mod;
return;
}
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
PushDown(L,R,rt);
if(l<=mid)
qu(lson,l,r);
if(r>mid)
qu(rson,l,r);
PushUp(rt);
}
int main()
{
int i,t,n,q,x,y,z,cas=1;
char cmd[20];
for(i=1;i<maxn;i++)
{
sm[i]=(sm[i-1]+i)%mod;
ss[i]=(ss[i-1]+(ll)i*i)%mod;
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&q);
printf("Case %d:\n",cas++);
while(q--)
{
asc=aic=asd=aid=aiid=0;
scanf("%s%d%d",cmd,&x,&y);
if(cmd[0]!='Q')
scanf("%d",&z);
if(cmd[2]=='s')
update(1,n,1,x,y,z,0);
else if(cmd[2]=='l')
update(1,n,1,x,y,z,1);
else
{
qu(1,n,1,x,y);
ll ans=((ll)y*(y+1)*asd-(2*y+1)*aid+aiid)%mod;
ans-=2*((y+1)*asc-aic);
ans%=mod;
ans=(ans+mod)%mod;
//printf("asc %I64d aic %I64d asd %I64d aid %I64d aiid %I64d\n",asc,aic,asd,aid,aiid);
printf("%I64d\n",ans/2);
}
}
}
return 0;
}

hdu 4037 Development Value(线段树维护数学公式)的更多相关文章

  1. HDU 6155 Subsequence Count 线段树维护矩阵

    Subsequence Count Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Oth ...

  2. HDU 2795 Billboard 【线段树维护区间最大值&&查询变形】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others) ...

  3. hdu 5068 线段树维护矩阵乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...

  4. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  5. HDU 6406 Taotao Picks Apples 线段树维护

    题意:给个T,T组数据: 每组给个n,m:n个数,m个操作: (对序列的操作是,一开始假设你手上东西是-INF,到i=1时拿起1,之后遍历,遇到比手头上的数量大的数时替换(拿到手的算拿走),问最后拿走 ...

  6. HDU 5861 Road (线段树)

    Road 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Description There are n villages alo ...

  7. HDU 3265 Posters(线段树)

    HDU 3265 Posters pid=3265" target="_blank" style="">题目链接 题意:给定一些矩形海报.中间有 ...

  8. hdu 3954 Level up(线段树)

    题目链接:hdu 3954 Level up 题目大意:N个英雄,M个等级,初始等级为1,给定每一个等级须要的经验值,Q次操作,操作分两种,W l r x:表示l~r之间的英雄每一个人杀了x个怪物:Q ...

  9. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

随机推荐

  1. WPF 漏斗控件 等待沙漏效果

    由于WPF中不支持gif图片因此要实现一个漏斗沙漏效果有点小麻烦. 网上有一款开源的控件 理论上完全开源 官网 http://wpfspark.codeplex.com/贴一下效果图 大家感觉需要就在 ...

  2. First step in troubleshooting complex issues: Define and scope your issue properly

    最近在查调试相关资料的时候,无意看到Tess的一篇关于如何快速分析复合场景问题的博文,感觉很实用,Mark备忘. My 9 questions for a pretty thorough proble ...

  3. html+css布局整理笔记

    基本概念 布局模型 流动模型(Flow) 浮动模型(Float) 层模型(Layer) 流动模型 默认的网页布局模式,流动布局模型有两个比较典型的特征: 第一,块级元素都会在所处的包含元素内自上而下按 ...

  4. 避免关注底层硬件,Nvidia将机器学习与GPU绑定

    Nvidia释放的一组cuDNN的库,有效的实现了其与多种深度学习框架的整合.基于cuDNN,加速了代码的运行,同时让研究员避免去关心底层硬件性能. 关键字: 编程语言语音识别Nvidia 原文链接: ...

  5. uva 1658 Admiral 【 最小费用最大流 】

    拆点,每个点拆成 i,i' 在i 和i‘之间连一条费用为0,容量为1的边,就可以保证每个点只经过一次 特殊的点,1和n之间,,,n和2*n之间连一条费用为0,容量为2的边,可以求出两条路径 #incl ...

  6. RabbitMQ学习之spring配置文件rabbit标签的使用

    下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...

  7. apiCloud组件:swiper

    一.apicloud中基于swiper封装了一个模块供调用.就是swiper.js 页面引入js就行 <script type="text/javascript" src=& ...

  8. xshell登录centos7很慢解决办法

    使用xshell登录到centos系统虚拟机,可以登录上去,但是认证速度特别慢. 因为在登录时,需要反向解析dns,因此,修改linux配置文件,vi /etc/ssh/sshd_config,将其注 ...

  9. 安装`lrzsz`包及其报错解决办法

    rz命令的安装包名是lrzsz. 安装lrzsz包时报错Failed to mount cd:///?devices=/dev/sr1,/dev/sr0 on /var/adm/mount/AP_0x ...

  10. 互联网组织的未来:剖析 GitHub 员工的任性之源

    转自:http://innolauncher.com/github/ 互联网组织的未来:剖析 GitHub 员工的任性之源 This entry was posted in Blogon 一月 4, ...