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
4 5
change 1 4 2
change 1 2 -1
establish 1 2
establish 2 4
establish 1 4
1.00000000
2.66666667
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;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k;
char op[];
ll ans[];
ll gao(int p)
{
return (ll)p*(p+)*(*p+)/;
}
struct Node
{
ll sum,sum1,sum2,lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
T[rt].sum1 = T[rt<<].sum1 + T[rt<<|].sum1;
T[rt].sum2 = T[rt<<].sum2 + T[rt<<|].sum2;
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
ll t = T[rt].lazy; T[rt<<].sum += t * (mid - L + );
T[rt<<|].sum += t * (R - mid); T[rt<<].sum1 += t * (mid - L + )*(mid + L)/;
T[rt<<|].sum1 += t * (R - mid)*(R + mid +)/; T[rt<<].sum2 += t * (gao(mid)-gao(L-));
T[rt<<|].sum2 += t * (gao(R)-gao(mid)); T[rt<<].lazy += t;
T[rt<<|].lazy += t;
T[rt].lazy = ;
} void Update(int l, int r, ll v, int L, int R, int rt)
{
if(l==L && r==R)
{
T[rt].lazy += v;
T[rt].sum += v * (R - L + );
T[rt].sum1 += v * (R - L + )*(R + L)/;
T[rt].sum2 += v * (gao(R)-gao(L-));
return ;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) Update(l, r, v, Lson);
else if(l > mid) Update(l, r, v, Rson);
else
{
Update(l, mid, v, Lson);
Update(mid+, r, v, Rson);
}
PushUp(rt);
} void Query(int l, int r, int L, int R, int rt)
{
if(l==L && r== R)
{
ans[]+=T[rt].sum;
ans[]+=T[rt].sum1;
ans[]+=T[rt].sum2;
return;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) Query(l, r, Lson);
else if(l > mid) Query(l, r, Rson);
else Query(l, mid, Lson) , Query(mid + , r, Rson);
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
while(m--)
{
int a,b,c;
scanf("%s",op);
if(op[]=='c')
{
scanf("%d%d%d",&a,&b,&c);
Update(a+,b,(ll)c,,n,);
}
else
{
scanf("%d%d",&a,&b);
ans[]=ans[]=ans[]=;
Query(a+,b,,n,);
printf("%.10f\n",(double)(-ans[]+(a+b+)*ans[]-(a+(ll)a*b)*ans[])/(b-a)/(b-a+)*);
}
}
//system("Pause");
return ;
}

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. iOS特殊字符的转义字符

    所有的ASCII码都可以用“\”加数字(一般是8进制数字)来表示.而C中定义了一些字母前加"\"来表示常见的那些不能显示的ASCII字符,如\0,\t,\n等,就称为转义字符,因为 ...

  2. 编译cvaux错误的原因

    引用:   http://www.cnblogs.com/oskycar/archive/2009/08/30/1556920.html VS2013 在debug模式下编译cvaux时会提示三个错误 ...

  3. hdu_1950_Bridging signals(LIS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 题意:实际就是求最长递增子序列 题解:有两种解法,一种是利用二分,一种是用线段树 这个是这题的二 ...

  4. Entity Framework技巧系列之十二 - Tip 46 - 50

    提示46. 怎样使用Code-Only排除一个属性  这次是一个真正简单的问题,由StackOverflow上这个问题引出.  问题:  当我们使用Code-Only把一个类的信息告诉Entity F ...

  5. Django:之CMDB资源系统

    渐谈CMDB需要内容,ITIL.CMDB介绍.Django自定义用户认证.Restful规范.资产管理功能开发. ITIL介绍 TIL即IT基础架构库(Information Technology I ...

  6. Ansible4:Ad-hoc与命令执行模块【转】

    Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbook.Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块. 如:command ...

  7. centos 6.5常见lib库安装

    在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现. # yum install -y gcc gdb strace g ...

  8. 转 由一次磁盘告警引发的血案:du 和 ls 的区别

    如果你完全不明白或者完全明白图片含义, 那么你不用继续往下看了. 否则, 这篇文章也许正是你需要的. 背景 确切地说,不是收到的自动告警短信或者邮件告诉我某机器上的磁盘满了,而是某同学人肉发现该机器写 ...

  9. 12c meet sysdba meet ORA-01017: invalid username/password; logon denied

    checklist: 1.12c: threaded_execution=true Prevents OS Login As Sysdba 2. The following database para ...

  10. Oracle表和表数据恢复

    Oracle数据库表及表数据的恢复 1. 表恢复 对误删的表,只要没有使用 purge 永久删除选项,那么基本上是能从 flashback table 区恢复回来的. 数据表和其中的数据都是可以恢复回 ...