NBUT 1457 Sona (莫隊演算法)

來源:互聯網
上載者:User

標籤:style   blog   strong   os   2014   for   

題目大意:

求一段區間內 出現的數位次數的三次方的和


思路分析:

這要水過去的題目真是難,各種最佳化。

不能用map , 要離散化之後 先處理lowerbound。最佳化輸入。。。

時間卡的很緊。。

題目直接用莫隊水過去。

如果你逾時的話,不妨試試上面三種最佳化。


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <map>#include <cmath>#define maxn 100005using namespace std;typedef long long LL;int app[maxn];int pos[maxn];int save[maxn];int x[maxn];map<int,int>mymap;struct foo{    int l,r,index;    LL ans;    bool operator < (const foo &cmp) const    {        if(pos[l]==pos[cmp.l])return r<cmp.r;        return pos[l]<pos[cmp.l];    }}Q[maxn];int n;inline void scanf_(int &num){    char in;    bool neg=false;    while(((in=getchar()) > '9' || in<'0') && in!='-') ;    if(in=='-'){        neg=true;        while((in=getchar()) >'9' || in<'0');    }    num=in-'0';    while(in=getchar(),in>='0'&&in<='9')        num*=10,num+=in-'0';    if(neg)        num=0-num;}bool cmp_id(const foo &a,const foo &b){    return a.index<b.index;}int sz;void modify(int p,LL &ans,int add){    ans-=(LL)app[x[p]]*app[x[p]]*app[x[p]];    app[x[p]]+=add;    ans+=(LL)app[x[p]]*app[x[p]]*app[x[p]];}int main(){    while(scanf("%d",&n)!=EOF)    {        memset(app,0,sizeof app);        int SIZE = (int)sqrt(n*1.0);        for(int i=1;i<=n;i++)        {            scanf_(save[i]);            x[i]=save[i];            pos[i]=(i-1)/SIZE+1;        }        int m;        scanf_(m);        for(int i=0;i<m;i++)        {            scanf_(Q[i].l);            scanf_(Q[i].r);            Q[i].index=i;        }        sort(save+1,save+n+1);        sz=unique(save+1,save+n+1)-save;        for(int i=1;i<=n;i++)        {            x[i] = lower_bound(save+1,save+sz,x[i])-save;        }        sort(Q,Q+m);        LL ans=0;        for(int i=0,l=1,r=0;i<m;i++)        {            if(r<Q[i].r)            {                for(r=r+1;r<=Q[i].r;r++)                    modify(r,ans,1);                r--;            }            if(r>Q[i].r)            {                for(;r>Q[i].r;r--)                {                    modify(r,ans,-1);                }            }            if(l<Q[i].l)            {                for(;l<Q[i].l;l++)                    modify(l,ans,-1);            }            if(l>Q[i].l)            {                for(l=l-1;l>=Q[i].l;l--)                    modify(l,ans,1);                l++;            }            if(Q[i].l==Q[i].r)            {                Q[i].ans=1;                continue;            }            Q[i].ans=ans;        }        sort(Q,Q+m,cmp_id);        for(int i=0;i<m;i++)            printf("%I64d\n",Q[i].ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.