1. 程式人生 > >BZOJ 2038: [2009國家集訓隊]小Z的襪子(hose) | 莫隊

BZOJ 2038: [2009國家集訓隊]小Z的襪子(hose) | 莫隊

方案 name sort hose href www 數組 r+ con

題目:

http://www.lydsy.com/JudgeOnline/problem.php?id=2038


題解:

開LongLong!!!!

按照莫隊的方法把詢問拍個序,然後搞cnt數組統計每個顏色出現次數,用cur統計當前方案

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 50010
typedef unsigned long long ll;
using namespace std;
ll n,m,c[N],S,L,R;
ll ans1[N],ans2[N],cnt[N],cur;
struct node { ll l,r,bl,id; bool operator < (const node &x)const { return bl<x.bl || bl==x.bl && r<x.r; } }q[N]; ll gcd(ll x,ll y) { return y?gcd(y,x%y):x; } int main() { scanf("%lld%lld",&n,&m);S=sqrt(n),cnt[0]=1; for (ll i=1;i<=n;i++) scanf(
"%lld",c+i); for (ll i=1,l,r;i<=m;i++) { scanf("%lld%lld",&q[i].l,&q[i].r); q[i].id=i;q[i].bl=(q[i].l-1)/S+1; } sort(q+1,q+1+m); for (ll i=1;i<=m;i++) { ll tl=q[i].l,tr=q[i].r; while (L<tl) cnt[c[L]]--,cur-=cnt[c[L]],L++; while (L>tl) L--,cur+=cnt[c[L]],cnt[c[L]]++;
while (tr>R) R++,cur+=cnt[c[R]],cnt[c[R]]++; while (R>tr) cnt[c[R]]--,cur-=cnt[c[R]],R--; ans1[q[i].id]=cur; ans2[q[i].id]=(tr-tl)*(tr-tl+1LL)/2; } for (int i=1;i<=m;i++) { ll G=gcd(ans1[i],ans2[i]); printf("%lld/%lld\n",ans1[i]/G,ans2[i]/G); } return 0; }

BZOJ 2038: [2009國家集訓隊]小Z的襪子(hose) | 莫隊