1. 程式人生 > >hdu 6058 連結串列

hdu 6058 連結串列

傳送門

Kanade's sum

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1135    Accepted Submission(s): 470


Problem Description Give you an array A[1..n]of length n

Let f(l,r,k) be the k-th largest element of A[l..r].

Specially , f(l,r,k)=0 if rl+1<k.

Give you k
 , you need to calculate nl=1nr=lf(l,r,k)

There are T test cases.

1T10

kmin(n,80)

A[1..n]isapermutationof[1..n]

n5105
Input There is only one integer T on first line.

For each test case,there are only two integers n,k on first line,and the second line consists of n integers which means the array A
[1..n]


Output For each test case,output an integer, which means the answer.
Sample Input 1 5 2 1 2 3 4 5
Sample Output 30
Source
Recommend liuyiding   |   We have carefully selected several similar problems for you:  6066 6065 6064 6063 6062  題意:找區間第K大的是誰,然後+=這個數 。最後問sum。

做法:比賽的時候想到了每個點左右k個位置看貢獻有多少。就是沒想到用連結串列維護每個點的位置。太菜了。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=5e5+10;
const int maxx=1e7+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline int Scan()
{
    int Res=0,ch,Flag=0;
    if((ch=getchar())=='-')Flag=1;
    else if(ch>='0' && ch<='9')Res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
    return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int pre[maxn],nex[maxn],n,k,a[maxn],pos[maxn],num1[maxn],num2[maxn];
LL solve(int x)
{
    int x1=0,x2=0;
    for(int i=x;i&&x1<=k;i=pre[i])
        num1[++x1]=i-pre[i];
    for(int i=x;i<=n&&x2<=k;i=nex[i])
        num2[++x2]=nex[i]-i;
    LL ans=0;
    for(int i=1;i<=x1;i++)
        if(k-i+1<=x2&&k-i>=0)
            ans+=num1[i]*num2[k-i+1];
    return ans;
}

void del(int x)
{
    pre[nex[x]]=pre[x];
    nex[pre[x]]=nex[x];
}
int main()
{
    int t;
    t=Scan();
    while(t--)
    {
        n=Scan();k=Scan();
        for(int i=1;i<=n;i++)
        {
            a[i]=Scan();
            pos[a[i]]=i;
        }
        for(int i=0;i<=n+1;i++)
        {
            pre[i]=i-1;
            nex[i]=i+1;
        }
        pre[0]=0;
        nex[n+1]=n+1;
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=solve(pos[i])*i;
            del(pos[i]);
        }
        cout<<ans<<endl;
    }
}