ural1855 Trade Guilds of Erathia
Trade Guilds of Erathia
Memory limit: 64 MB
Input
- “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
Sample
| input | output |
|---|---|
4 5 |
1.00000000 |
分析:设询问区间为[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的更多相关文章
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- [题解]hdu 1009 FatMouse' Trade(贪心基础题)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)
题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...
- HDU 3401 Trade dp+单调队列优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)Mem ...
- 【HDU 1009】FatMouse' Trade
题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the ware ...
- hdu 1009:FatMouse' Trade(贪心)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 杭电1009-FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- MapReduce,DataJoin,链接多数据源
主要介绍用DataJoin类来链接多数据源,先看一下例子,假设二个数据源customs和orders customer ID Name PhomeNumber 1 ...
- mysql之TIMESTAMP(时间戳)用法详解 [http://www.jb51.net/article/51794.htm]
一.TIMESTAMP的变体 TIMESTAMP时间戳在创建的时候可以有多重不同的特性,如: 1.在创建新记录和修改现有记录的时候都对这个数据列刷新: TIMESTAMP DEFAULT CURREN ...
- 将当天时间转换为unix时间戳
/** * @return * * @Title: getDate * @Description: TODO(时间戳转换为String类型的日期数据) * @param @param unixDate ...
- JS总结之一:字符串的调用方法
字符串的调用方法:var s="hello, world";document.write(s.charAt(0)); //第一个字符document.write(s.charAt( ...
- CSS3秘笈:第三章
1.标签选择器:控制整体. 使用标签选择器,只需要输入标签的名称即可. 2.类选择器:精确控制. 假如你要指定一些相同的元素拥有不同的样式,可以给元素添加不同的类名,然后用类选择器来应用对应的样式. ...
- shell脚本学习(一)
1.hbg@HWM:/$ su root密码:root@HWM:/# $表示普通用户, #表示管理员用户root.root是linux系统中权限最高的用户. 2.在bash中,每个变量的值都是字符串. ...
- Git 删除文件
在Git中,删除也是一种修改的操作,我们验证一下,先在工作目录中添加一个新文件test.txt,并且提交: $ git statusOn branch masterUntracked files: ...
- isPostBack原理
从 到输入用户名,点击提交按钮 这个过程就叫做postback(是两个不同的状态) 利用ispostback原理,实现是否第一次进入处理程序(上一个用用户名判断的不好,会导致在用户名空的情况下 ...
- window.open弹出窗口调用controller
前台图片调用js函数 <img src='${pageContext.request.contextPath}/FlatUI/img/link.png' id='report' alt='&qu ...
- 状压dp找寻环的个数 Codeforces Beta Round #11 D
http://codeforces.com/problemset/problem/11/D 题目大意:给你n个点,m条边,找该图中有几个换 思路:定义dp[i][j]表示i是圈的集合,j表示该集合的终 ...