[POI2013] LAN-Colorful Chain

给定一个序列,求有多少个子串使得 (c_i) 这个数在这个序列中出现了 (l_i) 次,且不存在其它的数。

Solution

滑动窗口搞一下

#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;

#define int long long
const int N  =2000005;

unordered_multiset <int> s;

int l[N],c[N],a[N],n,m,buc[N],ans;

signed main() {
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=m;i++) cin>>c[i];
    for(int i=1;i<=m;i++) cin>>l[i];
    for(int i=1;i<=m;i++) buc[l[i]]=-c[i];
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++) s.insert(l[i]);
    int tot=0;
    for(int i=1;i<=m;i++) tot+=c[i];
    for(int i=1;i<=tot;i++) {
        buc[a[i]]++;
        if(buc[a[i]]==0) s.erase(a[i]);
    }
    if(s.size()==0) ++ans;
    for(int i=tot+1;i<=n;i++) {
        if(buc[a[i-tot]]==0) s.insert(a[i-tot]);
        buc[a[i-tot]]--;
        if(buc[a[i-tot]]==0) s.erase(a[i-tot]);
        if(buc[a[i]]==0) s.insert(a[i]);
        buc[a[i]]++;
        if(buc[a[i]]==0) s.erase(a[i]);
        if(s.size()==0) ++ans;
    }
    cout<<ans;
}

原文地址:https://www.cnblogs.com/mollnn/p/12340395.html