【NOI 2010】能量採集&&超級鋼琴

其實這兩個題一點關係都沒有,同一天做的,寫在一起= =
能量採集

ans=x=1ny=1m2gcd(x,y)+1

x=1ny=1m[d|gcd(x,y)]=ndmd

code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long n,m,maxn;
long long f[100001];
int main()
{
    long long ans=0,i,j,x,t=0;
    scanf("%lld%lld",&n,&m);
    maxn=min(n,m);
    for (i=maxn;i>=1;--i)
      {
        t=(n/i)*(m/i);
        for (j=2*i;j<=maxn;j+=i)
          t-=f[j];
        f[i]=t;
        ans+=t*(2*i-1);
      }
    printf("%lld\n",ans);
}

超級鋼琴
堆加線段樹亂搞 code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 2100000000LL
#define mid (l+r)/2
#define lch i<<1,l,mid
#define rch i<<1|1,mid+1,r 
using namespace std;
long long n,k,l,r;
struct hp{
    long long maxn,maxi;
}seg[2000001];
struct heapnode{
    long long i,l,r,maxn,maxi;
    bool operator < (const heapnode &a) const {return maxn<a.maxn;}
};
priority_queue<heapnode> heap;
long long a[500001],s[500001],ans,ansi;
void updata(int i)
{
    if (seg[i<<1].maxn>=seg[i<<1|1].maxn)
      {
        seg[i].maxn=seg[i<<1].maxn;
        seg[i].maxi=seg[i<<1].maxi;
      }
    else
      {
        seg[i].maxn=seg[i<<1|1].maxn;
        seg[i].maxi=seg[i<<1|1].maxi;
      }
}
void build(int i,int l,int r)
{
    if (l==r)
      {
        seg[i].maxn=s[l];
        seg[i].maxi=l; 
        return; 
      }
    build(lch); build(rch);
    updata(i);
}
void query(int i,int l,int r,int x,int y)
{
    if (x<=l&&y>=r)
      {
        if (seg[i].maxn>ans)
          {
            ans=seg[i].maxn;
            ansi=seg[i].maxi;
          }
        return;
      }
    if (x<=mid) query(lch,x,y);
    if (y>mid) query(rch,x,y);
}
int main()
{
    long long i,tot=0;
    heapnode temp,t;
    scanf("%lld%lld%lld%lld",&n,&k,&l,&r);
    s[0]=0; 
    for (i=1;i<=n;++i)
      {
        scanf("%lld",&a[i]);
        s[i]=s[i-1]+a[i];
      }  
    build(1,1,n);
    for (i=1;i<=n;++i)
      {
        temp.i=i;
        if (i+l-1>n) continue;
        temp.l=i+l-1;
        temp.r=min(i+r-1,n);
        ans=-inf; ansi=0;
        query(1,1,n,temp.l,temp.r);
        temp.maxn=ans-s[i-1]; temp.maxi=ansi;
        heap.push(temp); 
        //cout<<temp.l<<' '<<temp.r<<':'<<ans<<endl;
      }
    for (i=1;i<=k;++i)
      {
        temp=heap.top(); heap.pop();
        tot+=temp.maxn;
        //cout<<temp.i<<' '<<temp.maxi<<endl;
        if (temp.l<=temp.maxi-1)
          {
            t.i=temp.i; t.l=temp.l; t.r=temp.maxi-1;
            ans=-inf; ansi=0;
            query(1,1,n,t.l,t.r);
            t.maxn=ans-s[temp.i-1]; t.maxi=ansi;
            heap.push(t);  
          }
        if (temp.r>=temp.maxi+1)
          {
            t.i=temp.i; t.l=temp.maxi+1; t.r=temp.r;
            ans=-inf; ansi=0;
            query(1,1,n,t.l,t.r);
            t.maxn=ans-s[temp.i-1]; t.maxi=ansi;
            heap.push(t);
          }
      }
    printf("%lld\n",tot);
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章