Trade Guilds of Erathia

Time limit: 2.0 second
Memory limit: 64 MB
The continent of Antagarich was colonized slowly. Long ago its northern part was inhabited by the elves of Avlee. Later, the hot southern desert of Bracada was occupied by the white mages. At the same time, necromancers settled in Deyja, a land to the north of Bracada and to the south-west of Avlee. Although white and dark mages didn't really like each other, each group had some artifacts that the other group would be happy to buy. As a result, the trading relationship between Bracada and Deyja grew stronger, and soon the mages built a very busy trade route between these lands.
Erathia was founded later, and at first it was stretched along this route. At that time Erathia's economy was based solely on trading, so new trading guilds appeared all the time. Each of the guilds was present in a few cities which were consecutively situated along the route. Caravans of each guild travelled between all pairs of cities of that guild equally often.
The state's treasury was replenished by fees collected from all the caravans moving along the trade route. There was a fee for each route segment connecting two neighboring cities, and this fee could change over time. For example, the fee could be decreased in the areas of frequent goblin attacks, or increased in the areas with high traffic.
Loins, the royal treasurer, studies Erathia's economy and tries to predict the profit of trade guilds. He wants to know the amount of money paid in fees by each guild. He has a chronologically ordered list of documents that contains all the royal orders changing the fee and all the papers establishing new guilds. This data should be used to calculate the average fee paid by a caravan of a given trade guild.

Input

The first line contains the number n of cities in Erathia and the number m of documents collected by Loins (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The following m lines describe the documents of two possible types:

  • “change a b d”: the fee for travelling along each route segment between cities a and bchanged by d gold coins (if d is positive, the fee increased; if d is negative, the fee decreased);
  • “establish a b”: a new guild which is present in all cities between a and b was established.

All numbers are integers; 1 ≤ a < b ≤ n; −10 000 ≤ d ≤ 10 000. Cities are numbered in the order they are located along the route: from Bracada to Deyja. The fee for travelling along a segment was never larger than 10 000 gold coins, otherwise merchants would protest. Of course, the fee was always non-negative. Before the first royal order changing the fee, it is equal to zero for all route segments.

Output

After each document establishing the new guild, output in a single line the average amount of fee paid by a caravan of this guild. The absolute or relative error should not exceed 10−6.

Sample

input output
  1. 4 5
  2. change 1 4 2
  3. change 1 2 -1
  4. establish 1 2
  5. establish 2 4
  6. establish 1 4
  1. 1.00000000
  2. 2.66666667
  3. 2.83333333

分析:设询问区间为[l,r],第k条路编号为k+1;

   则第k条路的贡献为(k-l)*(r-k+1)*cost[k];

   展开后即[-k*k+(l+1+r)*k-l-l*r]*cost[k];

   线段树单点维护好cost[k],k*cost[k],k*k*cost[k]即可;

   注意爆int;

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  17. #define mod 1000000007
  18. #define inf 0x3f3f3f3f
  19. #define vi vector<int>
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fi first
  23. #define se second
  24. #define ll long long
  25. #define pi acos(-1.0)
  26. #define pii pair<int,int>
  27. #define Lson L, mid, rt<<1
  28. #define Rson mid+1, R, rt<<1|1
  29. const int maxn=1e5+;
  30. using namespace std;
  31. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  32. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  33. int n,m,k;
  34. char op[];
  35. ll ans[];
  36. ll gao(int p)
  37. {
  38. return (ll)p*(p+)*(*p+)/;
  39. }
  40. struct Node
  41. {
  42. ll sum,sum1,sum2,lazy;
  43. } T[maxn<<];
  44.  
  45. void PushUp(int rt)
  46. {
  47. T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
  48. T[rt].sum1 = T[rt<<].sum1 + T[rt<<|].sum1;
  49. T[rt].sum2 = T[rt<<].sum2 + T[rt<<|].sum2;
  50. }
  51.  
  52. void PushDown(int L, int R, int rt)
  53. {
  54. int mid = (L + R) >> ;
  55. ll t = T[rt].lazy;
  56.  
  57. T[rt<<].sum += t * (mid - L + );
  58. T[rt<<|].sum += t * (R - mid);
  59.  
  60. T[rt<<].sum1 += t * (mid - L + )*(mid + L)/;
  61. T[rt<<|].sum1 += t * (R - mid)*(R + mid +)/;
  62.  
  63. T[rt<<].sum2 += t * (gao(mid)-gao(L-));
  64. T[rt<<|].sum2 += t * (gao(R)-gao(mid));
  65.  
  66. T[rt<<].lazy += t;
  67. T[rt<<|].lazy += t;
  68. T[rt].lazy = ;
  69. }
  70.  
  71. void Update(int l, int r, ll v, int L, int R, int rt)
  72. {
  73. if(l==L && r==R)
  74. {
  75. T[rt].lazy += v;
  76. T[rt].sum += v * (R - L + );
  77. T[rt].sum1 += v * (R - L + )*(R + L)/;
  78. T[rt].sum2 += v * (gao(R)-gao(L-));
  79. return ;
  80. }
  81. int mid = (L + R) >> ;
  82. if(T[rt].lazy) PushDown(L, R, rt);
  83. if(r <= mid) Update(l, r, v, Lson);
  84. else if(l > mid) Update(l, r, v, Rson);
  85. else
  86. {
  87. Update(l, mid, v, Lson);
  88. Update(mid+, r, v, Rson);
  89. }
  90. PushUp(rt);
  91. }
  92.  
  93. void Query(int l, int r, int L, int R, int rt)
  94. {
  95. if(l==L && r== R)
  96. {
  97. ans[]+=T[rt].sum;
  98. ans[]+=T[rt].sum1;
  99. ans[]+=T[rt].sum2;
  100. return;
  101. }
  102. int mid = (L + R) >> ;
  103. if(T[rt].lazy) PushDown(L, R, rt);
  104. if(r <= mid) Query(l, r, Lson);
  105. else if(l > mid) Query(l, r, Rson);
  106. else Query(l, mid, Lson) , Query(mid + , r, Rson);
  107. }
  108. int main()
  109. {
  110. int i,j;
  111. scanf("%d%d",&n,&m);
  112. while(m--)
  113. {
  114. int a,b,c;
  115. scanf("%s",op);
  116. if(op[]=='c')
  117. {
  118. scanf("%d%d%d",&a,&b,&c);
  119. Update(a+,b,(ll)c,,n,);
  120. }
  121. else
  122. {
  123. scanf("%d%d",&a,&b);
  124. ans[]=ans[]=ans[]=;
  125. Query(a+,b,,n,);
  126. printf("%.10f\n",(double)(-ans[]+(a+b+)*ans[]-(a+(ll)a*b)*ans[])/(b-a)/(b-a+)*);
  127. }
  128. }
  129. //system("Pause");
  130. return ;
  131. }

ural1855 Trade Guilds of Erathia的更多相关文章

  1. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  2. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Hdu 1009 FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  5. ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)

    题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...

  6. HDU 3401 Trade dp+单调队列优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)Mem ...

  7. 【HDU 1009】FatMouse' Trade

    题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the ware ...

  8. hdu 1009:FatMouse' Trade(贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. 杭电1009-FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 文本注释系统 + MarkDown

    标记系统: 笔记的要点 题材 缘起 目标 等级: 细节性 事实性 规律 法则 适用范围: 时间.地点.人物.起因.经过.结果,who what where when why how whom 6W1H ...

  2. (响应式PC端媒体查询)电脑屏幕分辨率尺寸大全

    (响应式PC端媒体查询)电脑屏幕分辨率尺寸大全 时间:2015-08-17 16:50:40      阅读:3961      评论:0      收藏:0      [点我收藏+] 标签:styl ...

  3. HDU 5795 A Simple Nim

    打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

  4. wmic应用实例

    实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...

  5. 《JS权威指南学习总结--第三章类型、值和变量》

    第三章 类型.值和变量 内容要点 一.数据类型 1.在编程语言中,能够表示并操作的值的类型称做数据类型 2.JS的数据类型分为两类: 原始类型:数字.字符串和布尔值 对象类型 3.JS中有两个特殊的原 ...

  6. .Net TransactionScope事务

    使用TransactionScope类 正如名称所暗示,TransactionScope类用于限定事务代码块,其具有一些明显优点,例如范围与应用程序对象模型无关,同时提供了一个简单直观的编程模型等等. ...

  7. JSP基本语法--实例演练

    基本语法概括:<%@page>,<%@include>,<jsp:include>,<jsp:forward> 加上数据库操作,可以开发动态web了. ...

  8. HTML+CSS D08浮动

    1. <html> <head> <title>div浮动</title> <style type="text/css"> ...

  9. hdu5269 ZYB loves Xor I

    分治法和字典树都可以,都是递归,但字典树耗内存 从第一bit开始,若相同则xor为0,分到同一部分,不相同则统计,且此时lowbit为这一bit,最后结果要乘以2 /*分治法*/ #include&l ...

  10. 解决编译时出现的警告:format string is not a string literal (potentially insecure)

    NSLog([NSString stringWithFormat:@"%@/%@B.jpg", createDir, uuid]);//这是我的写法 应该写成 NSString * ...