HDU 2688

这题同一个代码可能过也可能不过,网上的题解基本上是同一种做法。个人写的细节差异会容易TLE。


测试了一晚上,终于找出了问题所在。

方法大家一定都知道,但是测试发现加inline会ac,平均提交2-3次能ac一次,运气好一次就能ac 。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <set>

using namespace std;

const int maxn=3e6+10;
int n,m;
char ch;
int arr[maxn],num[10010];

inline int lowerbit(int x)
{
    return (x&(-x));
}

inline void add(int pos,int x)
{
    while(pos<=10000)
    {
        num[pos]+=x;
        pos+=lowerbit(pos);
    }
}

inline int sum(int pos)
{
    int ret=0;
    while(pos>0)
    {
        ret+=num[pos];
        pos-=lowerbit(pos);
    }
    return ret;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(num,0,sizeof(num));
        long long int ans=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&arr[i]);
            ans+=sum(arr[i]-1);//注意这个地方要从小于第i个开始求和,避免重复。
            add(arr[i],1);
        }
        scanf("%d",&m);
        while(m--)
        {
            getchar();
            scanf("%c",&ch);
            if(ch=='Q') printf("%lld\n",ans);
            else
            {
                int l,r;
                scanf("%d%d",&l,&r);
                int temp=arr[l];
                for(int i=l+1; i<=r; i++)
                {
                    if(arr[i]>temp) ans--;
                    else if(arr[i]<temp) ans++;
                    arr[i-1]=arr[i];
                }
                arr[r]=temp;
            }
        }
    }
    return 0;
};


下面附带测试图,同样的代码几次才会ac,说明HDU的不稳定性。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值