bzoj2038莫队模板

题解rt。

#include
#include
#include
#include
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+5;
int n,m,pos[N],c[N];
typedef long long ll;
ll s[N],ans;
struct node
{
    int l,r,id;
    ll a,b;
}a[N];
ll gcd(ll a,ll b)
{
    if (a%b==0)return b;
    else return gcd(b,a%b);
}
bool cmpid(node a,node b)
{
    return a.idif (pos[a.l]==pos[b.l])return a.rreturn a.lsqr(ll x)
{
    return x*x;
}
inline void update(int p,int add)
{
    ans-=sqr(s[c[p]]);
    s[c[p]]+=add;
    ans+=sqr(s[c[p]]);
}
int main()
{
    scanf("%d%d",&n,&m);
    fo(i,1,n)scanf("%d",&c[i]);
    int block=int(sqrt(n));
    fo(i,1,n)pos[i]=(i-1)/block+1;
    fo(i,1,m)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
        a[i].id=i;
    }
    sort(a+1,a+m+1,cmp);
    for(int i=1,l=1,r=0;i<=m;i++)
    {
        while (r1,1);
            r++;
        }
        while (r>a[i].r)
        {
            update(r,-1);
            r--;
        }
        while (l1);
            l++;
        }
        while (l>a[i].l)
        {
            update(l-1,1);
            l--;
        }
        if (a[i].l==a[i].r)
        {
            a[i].a=0;
            a[i].b=1;
            continue;
        }
        a[i].a=ans-(a[i].r-a[i].l+1);
        a[i].b=(ll)(a[i].r-a[i].l+1)*(a[i].r-a[i].l);
        ll k=gcd(a[i].a,a[i].b);
        a[i].a/=k;
        a[i].b/=k;
    }
    sort(a+1,a+1+m,cmpid);
    fo(i,1,m)
    printf("%lld/%lld\n",a[i].a,a[i].b);
    return 0;
}

你可能感兴趣的:(bzoj,莫队,模板,模板-生成word)