[codevs1135]选择客栈

题目←

完全没想到这复杂度能过……

思路:
枚举每种颜色对答案的贡献

首先枚举合法的咖啡店p,则发现同种颜色的客栈对答案贡献为sum[k][pre ~ P]*sum[k][P+1~n]
sum为颜色k在该区间出现的个数,前缀和实现
pre为上一家合法咖啡店的位置
需要注意枚举的咖啡店颜色与枚举颜色相同时,统计以咖啡店为端点的区间答案
复杂度O(k*n)

#include
#include
#include
#define LL long long
using namespace std;
const int MAXN = 200000 + 50;
int qz[51][MAXN],n,k,p;
struct zt{
    int c,v;
}l[MAXN << 1];
LL ans;
void solve(int u){
    int pre = 0;
    LL tot = 0;
    for(int i = 1;i <= n;i ++){
        if(l[i].v <= p){
            tot += (qz[u][i] - qz[u][pre])*(qz[u][n] - qz[u][i]);
            if(l[i].c == u)tot += max(0,qz[u][i] - qz[u][pre] - 1);//咖啡店为端点的情况
            pre = i;
        }
    }
    ans += tot;
}
int main(){
    scanf("%d%d%d",&n,&k,&p);

    for(int i = 1;i <= n;i ++){
        scanf("%d%d",&l[i].c,&l[i].v);
    }
    for(int u = 0;u <= k - 1;u ++){
        for(int i = 1;i <= n;i ++){
            qz[u][i] = qz[u][i - 1] + (l[i].c == u);
        }
    }
    for(int i = 0;i <= k - 1;i ++){
        solve(i);
    }
    printf("%lld",ans);
    return 0;
}

再贴一份wyh大佬的思路,很有意思:
统计每个点作为左边客栈时右边可以配对的客栈个数
枚举点i
1、记录点i后最近的、<= P的客栈位置为loc
2、则loc后所有与i同色的点的个数即为i对答案贡献

如何实现操作1:
首先通过ST表log的查询区间最小值
二分区间i+1~n,发现若i+1~mid区间最小值 <= p则r左移,否则l右移

操作2:
前缀和

代码from wyh↓

#include
#include
#include
#include
#include

#define RI register int
using namespace std;
typedef long long ll;

const int INF = 1e9 + 7;
const int MAXN = 200000 + 5;

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))

inline void read(int &x)
{
    x = 0;
    bool flag = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')   flag = 1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    if(flag)    x *= -1;
}

ll ans = 0;
int n,k,p,pos,t[25],lg[MAXN];
int c[MAXN],num[MAXN],s[51][MAXN],f[MAXN][21];

void init_rmq()
{
    t[0] = 1;
    for(int i = 1;i <= n;i ++)
        lg[i] = log2(i);
    for(int i = 1;i <= 20;i ++)
        t[i] = (t[i - 1] << 1);
    for(RI j = 1;j <= 20;j ++)
        for(RI i = 1;i <= n;i ++)
        {
            if(i + t[j] - 1 <= n)
                f[i][j] = min(f[i][j - 1],f[i + t[j - 1]][j - 1]);
            else    break;
        }

}

int ask_min(int l,int r)
{
    int k = lg[r - l + 1];
    return min(f[l][k],f[r - t[k] + 1][k]);
}

int ask_pos(int l,int r)
{
    if(num[l] <= p) return l;
    if(ask_min(l,r - 1) > p)    return r;
    while(r - l > 1)
    {
        int mid = l + r >> 1;
        if(ask_min(l,mid) <= p)
            r = mid;
        else
            l = mid;
    }
    return r;
}

int main()
{
    read(n),read(k),read(p);
    for(RI i = 1;i <= n;i ++)
    {
        read(c[i]),read(num[i]);
        s[c[i]][i] ++,f[i][0] = num[i];
        for(int j = 0;j < k;j ++)
            s[j][i] += s[j][i - 1];
    }
    init_rmq();
    for(RI i = 1;i <= n;i ++)
    {
        if(ask_min(i,n) > p)break;
        pos = ask_pos(i + 1,n);
        if(num[i] <= p) pos = i + 1;
        ans += s[c[i]][n] - s[c[i]][pos - 1];
    }
    printf("%lld\n",ans);
    return 0;
}

你可能感兴趣的:([codevs1135]选择客栈)