【HAOI2012】高速公路 线段树区间维护

来源:互联网 发布:淘宝在哪开店 编辑:程序博客网 时间:2024/04/30 12:28

题目描述

Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站。

Y901高速公路是一条由N-1段路以及N个收费站组成的东西向的链,我们按照由西向东的顺序将收费站依次编号为1~N,从收费站i行驶到i+1(或从i+1行驶到i)需要收取Vi的费用。高速路刚建成时所有的路段都是免费的。

政府部门根据实际情况,会不定期地对连续路段的收费标准进行调整,根据政策涨价或降价。

无聊的小A同学总喜欢研究一些稀奇古怪的问题,他开车在这条高速路上行驶时想到了这样一个问题:对于给定的l,r (l < r),在第l个到第r个收费站里等概率随机取出两个不同的收费站a和b,那么从a行驶到b将期望花费多少费用呢?

输入输出格式

输入格式:
第一行2个正整数N,M,表示有N个收费站,M次调整或询问

接下来M行,每行将出现以下两种形式中的一种

C l r v 表示将第l个收费站到第r个收费站之间的所有道路的通行费全部增加v

Q l r 表示对于给定的l,r,要求回答小A的问题

所有C与Q操作中保证1<=l < r<=N

输出格式:
对于每次询问操作回答一行,输出一个既约分数

若答案为整数a,输出a/1

输入输出样例

输入样例#1:
4 5
C 1 4 2
C 1 2 -1
Q 1 2
Q 2 4
Q 1 4
输出样例#1:
1/1
8/3
17/6
说明

所有C操作中的v的绝对值不超过10000

在任何时刻任意道路的费用均为不超过10000的非负整数

思路:首先把每个区间右端点减一,因为观察发现最右边的收费站是没有用的,只需要在区间长度上注意一下就好。
这里写图片描述
推式子后返现答案便是上面的公式,观察发现只需要维护vi,vi*i,vi*i*i即可。注意区间合并的方式。
注意事项:
long long卡了我好久,首先lazy标记一定要longlong,传入的val也要longlong,一开始循环的时候,有i参与的地方,i需要开longlong

#include<iostream>#include<cstdio>#define MAXN 500010typedef long long LL;using namespace std;struct node{    LL lazy,sum1,sum2,sum3;    int l,r,len;}tree[MAXN*4];LL sumi[MAXN],sumii[MAXN];int n,m;inline LL get(){    LL x=0,p=1;    char c;    c=getchar();    while (c<'0'||c>'9') {if (c=='-') p=-1;c=getchar();}    while (c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}    return x*p;}void pushup(int root){    tree[root].sum1=tree[root*2].sum1+tree[root*2+1].sum1;    tree[root].sum2=tree[root*2].sum2+tree[root*2+1].sum2;    tree[root].sum3=tree[root*2].sum3+tree[root*2+1].sum3;}void build(int root,int l,int r){    tree[root].l=l,tree[root].r=r;tree[root].len=r-l+1;    if (l==r) return;    int mid=(l+r)/2;    build(root*2,l,mid);    build(root*2+1,mid+1,r);    pushup(root);}void pushdown(int root){    LL lazy=tree[root].lazy;    tree[root*2].sum1+=tree[root*2].len*lazy;    tree[root*2].sum2+=lazy*(sumi[tree[root*2].r]-sumi[tree[root*2].l-1]);    tree[root*2].sum3+=lazy*(sumii[tree[root*2].r]-sumii[tree[root*2].l-1]);    tree[root*2+1].sum1+=tree[root*2+1].len*lazy;    tree[root*2+1].sum2+=lazy*(sumi[tree[root*2+1].r]-sumi[tree[root*2+1].l-1]);    tree[root*2+1].sum3+=lazy*(sumii[tree[root*2+1].r]-sumii[tree[root*2+1].l-1]);    tree[root*2].lazy+=lazy;    tree[root*2+1].lazy+=lazy;    tree[root].lazy=0;}void update(int root,int l,int r,LL val){    if (tree[root].lazy) pushdown(root);    if (tree[root].l>r||tree[root].r<l) return;    if (tree[root].l>=l&&tree[root].r<=r)     {        tree[root].sum1+=tree[root].len*val;        tree[root].sum2+=val*(sumi[tree[root].r]-sumi[tree[root].l-1]);        LL tmp11=sumii[tree[root].r],tmp22=sumii[tree[root].l-1],tmp33=val;    LL tmpp=tmp33*(tmp11-tmp22);        tree[root].sum3+=val*(sumii[tree[root].r]-sumii[tree[root].l-1]);        tree[root].lazy+=val;   LL tmp=tree[root].sum3;        return ;    }    update(root*2,l,r,val);    update(root*2+1,l,r,val);    pushup(root);}LL query1(int root,int l,int r){    if (tree[root].lazy) pushdown(root);    if (tree[root].l>r||tree[root].r<l) return 0;    if (tree[root].l>=l&&tree[root].r<=r)         return tree[root].sum1;    return query1(root*2,l,r)+query1(root*2+1,l,r);}LL query2(int root,int l,int r){    if (tree[root].lazy) pushdown(root);    if (tree[root].l>r||tree[root].r<l) return 0;    if (tree[root].l>=l&&tree[root].r<=r)         return tree[root].sum2;    return query2(root*2,l,r)+query2(root*2+1,l,r);}LL query3(int root,int l,int r){    if (tree[root].lazy) pushdown(root);    if (tree[root].l>r||tree[root].r<l) return 0;    if (tree[root].l>=l&&tree[root].r<=r)         return tree[root].sum3;    return query3(root*2,l,r)+query3(root*2+1,l,r);}LL gcd(LL a,LL b){    if (!b) return a;    else return gcd(b,a%b);}LL x,y,z,v,ans;LL tmp1,tmp2,tmp3,fenmu,yue;int main(){    char c[100];    scanf("%d%d",&n,&m);    for (LL i=1;i<=n;i++)    {        sumi[i]=sumi[i-1]+i;        sumii[i]=sumii[i-1]+i*i;//这里的i要开LL,否则乘到最后会爆     }    build(1,1,n-1);    for (int i=1;i<=m;i++)    {        scanf("%s",c);        if (c[0]=='C')        {            x=get();y=get();v=get();            update(1,x,y-1,v);        }        else        {            x=get();y=get();            tmp1=(y-x*y)*query1(1,x,y-1);            tmp2=(x+y-1)*query2(1,x,y-1);            tmp3=-query3(1,x,y-1);            ans=tmp1+tmp2+tmp3;            fenmu=((y-x+1)*(y-x))/2;            yue=gcd(ans,fenmu);            fenmu=fenmu/yue;            printf("%lld/%lld\n",ans/yue,fenmu);            }    }}
原创粉丝点击