ural1855 Trade Guilds of Erathia

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+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k;
char op[10];
ll ans[3];
ll gao(int p)
{
    return (ll)p*(p+1)*(2*p+1)/6;
}
struct Node
{
    ll sum,sum1,sum2,lazy;
} T[maxn<<2];

void PushUp(int rt)
{
    T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum;
    T[rt].sum1 = T[rt<<1].sum1 + T[rt<<1|1].sum1;
    T[rt].sum2 = T[rt<<1].sum2 + T[rt<<1|1].sum2;
}

void PushDown(int L, int R, int rt)
{
    int mid = (L + R) >> 1;
    ll t = T[rt].lazy;

    T[rt<<1].sum += t * (mid - L + 1);
    T[rt<<1|1].sum += t * (R - mid);

    T[rt<<1].sum1 += t * (mid - L + 1)*(mid + L)/2;
    T[rt<<1|1].sum1 += t * (R - mid)*(R + mid +1)/2;

    T[rt<<1].sum2 += t * (gao(mid)-gao(L-1));
    T[rt<<1|1].sum2 += t * (gao(R)-gao(mid));

    T[rt<<1].lazy += t;
    T[rt<<1|1].lazy += t;
    T[rt].lazy = 0;
}

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 + 1);
        T[rt].sum1 += v * (R - L + 1)*(R + L)/2;
        T[rt].sum2 += v * (gao(R)-gao(L-1));
        return ;
    }
    int mid = (L + R) >> 1;
    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+1, r, v, Rson);
    }
    PushUp(rt);
}

void Query(int l, int r, int L, int R, int rt)
{
    if(l==L && r== R)
    {
        ans[0]+=T[rt].sum;
        ans[1]+=T[rt].sum1;
        ans[2]+=T[rt].sum2;
        return;
    }
    int mid = (L + R) >> 1;
    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 + 1, r, Rson);
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int a,b,c;
        scanf("%s",op);
        if(op[0]==‘c‘)
        {
            scanf("%d%d%d",&a,&b,&c);
            Update(a+1,b,(ll)c,1,n,1);
        }
        else
        {
            scanf("%d%d",&a,&b);
            ans[0]=ans[1]=ans[2]=0;
            Query(a+1,b,1,n,1);
            printf("%.10f\n",(double)(-ans[2]+(a+b+1)*ans[1]-(a+(ll)a*b)*ans[0])/(b-a)/(b-a+1)*2);
        }
    }
    //system("Pause");
    return 0;
}
时间: 2024-10-12 17:45:53

ural1855 Trade Guilds of Erathia的相关文章

线段树题目总结

一.单点更新 1.hdu1166 敌兵布阵:有N个兵营,每个兵营都给出了人数ai(下标从1开始),有四种命令,(1)"Addij",表示第i个营地增加j人.(2)"Sub i j",表示第i个营地减少j人.(3)"Query ij",查询第i个营地到第j个营地的总人数.(4)"End",表示命令结束.解题报告Here. 2.hdu1754 I Hate It:给你N个数,M个操作,操作分两类.(1)"QAB"

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

转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnlySuccess的博文“完全版线段树”里的大部分题目,其博文地址Here,然后也加入了自己做过的一些题目.整理时,更新了之前的代码风格,不过旧的代码仍然保留着. 同样分成四类,不好归到前四类的都分到了其他.树状数组能做,线段树都能做(如果是内存限制例外),所以也有些树状数组的题目,会标示出来,并且放

(并查集+DFS) poi guilds

Guilds Memory limit: 64 MB King Byteasar faces a serious matter. Two competing trade organisations, The Tailors Guild and The Sewers Guild asked, at the same time, for permissions to open their offices in each town of the kingdom. There are  towns in

Trade and the Ancient Middle East

Trade was the mainstay of the urban economy in the Middle East, as caravans negotiated the surrounding desert, restricted only by access to water and by mountain ranges. This has been so since ancient times, partly due to the geology of the area, whi

FatMouse&#39; Trade hdu1009

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 66570    Accepted Submission(s): 22635 Problem Description FatMouse prepared M

HDU 1009 FatMouse&#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 59851    Accepted Submission(s): 20095 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gu

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

贪心/hdu 1009 FatMouse&#39; Trade

题意 有n种物品,每一种需要不同的消费,现在手里有m块钱,求问最多可以买多少 分析 贪心 把每一种物品的价格算出来,然后sort一下,按照价格从便宜到贵排序,能买多少买多少,买买买! Accepted Code 1 /* 2 PROBLEM:hdu1009 3 AUTHER:Nicole Lam 4 MEMO:贪心 5 */ 6 7 #include<cstdio> 8 #include<algorithm> 9 using namespace std; 10 11 12 stru

HDU FatMouse&#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20968    Accepted Submission(s): 6501 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua