2017多校第3场 HDU 6058 Kanade's sum 双链表,思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058

题外话:这场多校,真心感觉自己的无力,全队都发挥的很差,结束的时候排名掉到了90多,后期没做出字典树那个题,直到现在看到标程也依然不懂那个题。。。状态非常不好,力求可以好好调整一下。

题意:题目给了一个求和式子,求所有l,r区间的第K大的和。K<=80。

解法:

全队,开始讨论如何维护这K个比他大的值,然后队友去写主席树,我实在没想到这题的做法,之后队友想到了直接用双链表来求出对于一个数X左边最近的kk个比他大的和右边最近k个比他大的,扫一下就可以知道有几个区间的k大值是X,然后我们从小到大枚举X,每次维护一个链表,链表里面只有大于等于X的数,然后从左往右扫一遍就可以了。链表的删除是O(1)的。


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct FastIO
{
    static const int S = 1310720;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar()
    {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) return -1;
        return buf[pos ++];
    }
    inline int xuint()
    {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(LL x)
    {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
const int maxn = 5e5+10;
int T,n,k,pre[maxn],nxt[maxn],pos[maxn];
inline void del(int i){
    pre[nxt[i]] = pre[i];
    nxt[pre[i]] = nxt[i];
}
inline LL solve(int x){
    static int a[82], b[82];
    int ca=0,cb=0;
    for(int i=x;i;i=pre[i]){
        a[++ca]=i-pre[i];
        if(ca==k) break;
    }
    for(int i=x;i<=n;i=nxt[i]){
        b[++cb]=nxt[i]-i;
        if(cb==k) break;
    }
    LL ret=0;
    for(int i=1; i<=ca; i++)if(k-i+1<=cb) ret+=(LL)a[i]*b[k-i+1];
    return ret;
}
int main()
{
    T = io.xint();
    while(T--){
        n = io.xint();
        k = io.xint();
        for(int i=1; i<=n; i++){
            int x;
            x = io.xint();
            pos[x]=i;
        }
        for(int i=0; i<=n+1; i++) pre[i]=i-1,nxt[i]=i+1;
        pre[0]=0;
        nxt[n+1]=n+1;
        LL ans=0;
        for(int i=1; i<=n; i++){
            int x = pos[i];
            ans += (LL)solve(x)*i;
            del(x);
        }
        printf("%lld\n", ans);
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值