萌新初学树状数组,为什么wa了2个点?

P1886 滑动窗口 /【模板】单调队列

会打沙包的猫 @ 2018-10-25 09:43:06

RT

用树状数组维护区间极值 可是蜜汁wa了两个点?!

而且数据太大下不了!!! 求救!!

#include<bits/stdc++.h>
#define INF_MAX 1e8+9
#define ss 1000009
#define ll long long
using namespace std;
ll n,k;
ll trmin[ss],trmax[ss],a[ss],v;
ll ansmin[ss],ansmax[ss];
inline ll lowbit(ll x)
{
    return x&-x;
}
inline void addmin(ll x,ll y)
{
   while(x<=n)
   {
       if(trmin[x]>y)
           trmin[x]=y;
        x+=lowbit(x);
   }
}
inline void addmax(ll x,ll y)
{
   while(x<=n)
   {
       if(trmax[x]<y)
           trmax[x]=y;
        x+=lowbit(x);
   }
}
inline ll getmin(ll x,ll r)
{
    ll ff=r;
    ll maxx=INF_MAX;
    while(ff>=x)
    {
        if(ff-lowbit(ff)>x)
        {
            maxx=min(maxx,trmin[ff]);
            ff-=lowbit(ff);
        }
        else 
        {
            maxx=min(maxx,a[ff]);
            ff--;
        }
    }
    return maxx;
}
inline ll getmax(ll x,ll r)
{
    ll ff=r;
    ll maxx=-INF_MAX;
    while(ff>=x)
    {
        if(ff-lowbit(ff)>x)
        {
            maxx=max(maxx,trmax[ff]);
            ff-=lowbit(ff);
        }
        else 
        {
            maxx=max(maxx,a[ff]);
            ff--;
        }
    }
    return maxx;
}
int main()
{
    memset(trmin,127,sizeof(trmin));
    memset(trmax,0,sizeof(trmax));
    scanf("%lld%lld",&n,&k);
    for(register ll i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        addmin(i,a[i]);
        addmax(i,a[i]);
    }
    for(register ll i=1;i+k-1<=n;i++)
    {
        if(i+k-1>n)break;
        ansmin[++v]=getmin(i,i+k-1);
        ansmax[v]=getmax(i,i+k-1);
    }
    for(register ll i=1;i<=v;i++)
      printf("%lld ",ansmin[i]);
    puts("");
    for(register ll i=1;i<=v;i++)
      printf("%lld ",ansmax[i]);
    return 0;
}

by Jhameel @ 2018-10-25 09:44:22

前排兜售烤绿鸟


by 会打沙包的猫 @ 2018-10-25 09:44:46

蒟蒻自认为码风海星(逃)


by The_Power_of_Pigeon @ 2018-10-25 09:44:55

您TQL!orz


by lz2018 @ 2018-10-25 09:46:04

TQL!!!


by x义x @ 2018-10-25 12:52:58

TQL!!!


|