Nbut 1457 Sona (Mo team algorithm)

Source: Internet
Author: User

Question:

Returns the sum of three power numbers that appear in a range.


Train of Thought Analysis:

It is really difficult to optimize the problems that need to be updated.

Map cannot be used. lowerbound must be processed after discretization. Optimized input...

The time card is very tight ..

You can use the Mosuo team to draw questions.

If you have timed out, try the above three optimizations.


#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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.