QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#153343#5459. Goose, goose, DUCK?invernoWA 1ms30144kbC++142.2kb2023-08-29 22:36:572023-08-29 22:36:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-29 22:36:57]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:30144kb
  • [2023-08-29 22:36:57]
  • 提交

answer

#include<bits/stdc++.h>
#define lch (w<<1)
#define rch ((w<<1)|1)
using namespace std;

const int N = 1e6+5;
const int INF = N;
int n, k;
int a[N],c[N];
vector<int> b[N];
struct rec{
    int val, cnt;
    int resi;
}tr[N<<2];
long long ans;

inline rec mymin(rec a, rec b) {
    if (a.val != b.val) {
        return (a.val < b.val)?a:b;
    }
    return (rec){a.val,a.cnt+b.cnt};
}

void pushdown(int w, int l, int r){
    tr[lch].resi += tr[w].resi;
    tr[lch].val += tr[w].resi;
    tr[rch].resi += tr[w].resi;
    tr[rch].val += tr[w].resi;
    tr[w].resi = 0;
    return ;
}

void add(int w, int l, int r, int L, int R, int y) {
    if (L <= l && r <= R) {
        tr[w].val += y;
        tr[w].resi += y;
        return ;
    }
    if (tr[w].resi) pushdown(w,l,r);
    int mid = (l+r)>>1;
    if (L > R) return;
    if (L <= mid) add(lch,l,mid,L,R,y);
    if (mid < R) add(rch,mid+1,r,L,R,y);
    tr[w] = mymin(tr[lch], tr[rch]);
    return ;
}

rec query(int w,int l, int r, int L, int R) {
    if (L <= l && r <= R) {
        return tr[w];
    }
    if (tr[w].resi) pushdown(w,l,r);
    int mid = (l+r)>>1;
    rec t1 = (rec){INF,0}, t2 = (rec){INF,0};
    if (L>R) return t1;
    if (L <= mid) t1 = query(lch,l,mid,L,R);
    if (mid < R) t2 = query(rch,mid+1,r,L,R);
    return mymin(t1,t2);
}

void build(int w, int l, int r){
    int mid = (l+r)>>1;
    tr[w] = (rec){0,r-l+1};
    if (l>=r) return;
    build(lch,l,mid);
    build(rch,mid+1,r);
    return;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

    cin >> n >> k;
    for (int i = 1;i <= n;++ i) {
        cin >> a[i];
        b[a[i]].push_back(i);
    }
    build(1,1,n);
    for (int r = 1;r <= n;++ r){
        int m = (c[a[r]]++);
        if (m >= k - 1) {
           vector<int> &tmp = b[a[r]];
           if (m == k-1) {
              add(1,1,n,1,tmp[0],+1);
           }
           else {
             add(1,1,n,tmp[m-k]+1,tmp[m-k+1],+1);
             if (m > k) add(1,1,n,tmp[m-k-1]+1,tmp[m-k],-1);
           }
        }
        rec x = query(1,1,n,1,r);
        if (x.val == 0) {
            ans = ans + x.cnt;
        }
    }
    cout << ans << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 30144kb

input:

6 2
1 2 2 1 3 3

output:

10

result:

ok 1 number(s): "10"

Test #2:

score: 0
Accepted
time: 1ms
memory: 30028kb

input:

6 1
1 2 3 4 5 6

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 30076kb

input:

100 10
142826 764475 142826 986320 764475 142826 142826 986320 764475 986320 764475 764475 764475 142826 142826 986320 764475 986320 764475 764475 142826 764475 142826 764475 986320 986320 764475 142826 764475 764475 142826 764475 764475 986320 142826 142826 142826 142826 764475 986320 986320 764475...

output:

854

result:

wrong answer 1st numbers differ - expected: '4309', found: '854'