记录编号 453504 评测结果 AAAAAAAAAA
题目名称 [HAOI 2012]高速公路 最终得分 100
用户昵称 GravatarCooook 是否通过 通过
代码语言 C++ 运行时间 0.761 s
提交时间 2017-09-21 16:55:15 内存使用 26.25 MiB
显示代码纯文本
#pragma GCC optimize("O3")
#include <stdio.h>
#include <cstring>
#include <iostream>
const int MAXN = 100005;
typedef long long ll;
int n,m;
  
  
inline int read(){
    int x=0,f=1;char ch=getchar();
    for(;ch>'9'||ch<'0';ch=getchar())if(ch=='-')f=-f;
    for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+(ch^48);
    return x*f;
}
  
inline ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
ll sum[MAXN<<3],f[MAXN<<3],g[MAXN<<3],tag[MAXN<<3];
ll w[MAXN][2],tot;
// sum --> a[i], f --> i * a[i], g --> i * i * a[i];
#define ls o<<1
#define rs o<<1|1
  
inline void Maintain(int o,int l,int r){
    if(l == r) return;
    sum[o] = sum[ls] + sum[rs] + tag[o] * (r-l+1);
    f[o] = f[ls] + f[rs] + tag[o] * (w[r][0] - w[l-1][0]); 
    g[o] = g[ls] + g[rs] + tag[o] * (w[r][1] - w[l-1][1]);
}
  
inline void Update(int o,int l,int r,int x,int y,ll v){
    if(x<=l&&r<=y){
        tag[o] += v; sum[o] += v * (r-l+1);
        f[o] += v * (w[r][0]-w[l-1][0]);
        g[o] += v * (w[r][1]-w[l-1][1]);
        return;
    }
    register int mid = l + r >> 1;
    if(x<=mid) Update(o<<1,l,mid,x,y,v);
    if(mid<y)  Update(o<<1|1,mid+1,r,x,y,v);
    Maintain(o,l,r);
}
  
inline void Query(int o,int l,int r,int x,int y,ll val){
    if(x<=l&&r<=y) { 
        ++ y;
        tot -= (g[o] + (w[r][1]-w[l-1][1]) * val);
        tot += (f[o] + (w[r][0]-w[l-1][0]) * val)*(x+y-1);
        tot += (sum[o] + 1ll*(r-l+1)*val)*(y-1ll*x*y);
        return;
    }
    register int mid = l + r >> 1;
    if(x<=mid) Query(ls,l,mid,x,y,val+tag[o]);
    if(mid<y) Query(rs,mid+1,r,x,y,val+tag[o]);  
}
  
inline void read_char(int &op){
    char ch = getchar();
    while(ch != 'C' && ch != 'Q') ch = getchar();
    op = (ch == 'Q');
}
  
  
int main(){
    freopen("roadxw.in","r",stdin);
    freopen("roadxw.out","w",stdout);
    n = read();m = read();
    for(register int i = 1;i<=n;i++) {
        w[i][0] = w[i-1][0] + i;
        w[i][1] = w[i-1][1] + 1ll*i*i;
    }
    while(m--){
        register int op;
        read_char(op);
        if(!op){
            register int x = read(), y = read(),v = read();
            Update(1,1,n-1,x,y-1,v);
        } 
        else{
            register int l = read(), r = read();
            register ll f = 1ll * (r-l) * (r-l+1) / 2;  tot = 0;
            Query(1,1,n-1,l,r-1,0);register ll g = gcd(tot,f);
            printf("%lld/%lld\n",tot/g,f/g);
        }
    }
    // getchar(); getchar();
    return 0;
}