bzoj2038 小Z的袜子 序列莫队模板

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2038
思路:orz 莫涛,自己写模板一下A掉就是开心是不是啊,,,就是大模拟吧,莫队算法还是有很多应用的要好好学习,,好好分块,不要去log,,

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#define N 50000
using namespace std;
typedef long long LL;
struct node { LL l,r,id,A,B;};
LL n,m,maxi,a[N+5],c[N+5],pos[N+5],sum;
node e[N+5];
inline bool cmp(node a,node b){ return (pos[a.l] < pos[b.l])||(pos[a.l] == pos[b.l]&&a.r < b.r);}
inline bool cmp1(node a,node b){ return (a.id < b.id);}
inline int getnum(){
    int num; char c;
    while (!isdigit(c = getchar()));
    num = c - '0';
    while (isdigit(c = getchar())) num = 10 * num + c - '0';
    return num;
}

void init(){
    n = getnum(); m = getnum(); maxi = sqrt(n);
    for (int i = 1;i <= n; ++i) a[i] = getnum();
    for (int i = 1;i <= m; ++i) e[i].l = getnum(),e[i].r = getnum(),e[i].id = i;
    for (int i = 1;i <= n; ++i) pos[i] = i/maxi;
    sort(e + 1,e + m + 1, cmp);
}

inline LL gcd(LL a,LL b){ if (!b) return a; return gcd(b,a%b);}

inline void add(LL x){
    sum += 2*c[a[x]] + 1;
    c[a[x]]++;
}

inline void dele(LL x){
    sum += -2*c[a[x]] + 1;
    c[a[x]]--; 
}

void DO_IT(){
    sum = 1;
    memset(c,0,sizeof(c));
    e[0].l =  e[0].r = e[1].l; c[a[e[1].l]] = 1;
    for (int i = 1;i <= m; ++i){
        for (int j = e[i].l; j < e[i-1].l; ++j) add(j);
        for (int j = e[i].r; j > e[i-1].r; --j) add(j);
        for (int j = e[i-1].l;j < e[i].l; ++j) dele(j);
        for (int j = e[i-1].r;j > e[i].r; --j) dele(j);
        LL s = e[i].r - e[i].l + 1;
        e[i].A = sum - s; e[i].B = s * (s-1);
        if (!e[i].A||!(e[i].B)) e[i].A = 0, e[i].B = 1;
        else {
        LL gcdi = gcd(e[i].A,e[i].B);
        e[i].A = e[i].A/ gcdi; e[i].B = e[i].B / gcdi;}
    }
}

void print(){
    sort(e + 1,e + m + 1,cmp1);
    for (int i = 1;i <= m; ++i) printf("%lld/%lld\n",e[i].A,e[i].B);
}

int main(){
    init();
    DO_IT();
    print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值