QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#149450#5459. Goose, goose, DUCK?black_iceWA 6ms30284kbC++143.0kb2023-08-24 16:55:502023-08-24 16:55:51

Judging History

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

  • [2023-08-24 16:55:51]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:30284kb
  • [2023-08-24 16:55:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N = 1e6 + 10,INF = 1e9;
int T,n,k;
int a[N];
vector<int> pos[N];
struct Node
{
    int l,r;
    int cnt,mn,add;
}tr[N * 4];

void pushup(int u)
{
    tr[u].mn = min(tr[u << 1].mn,tr[u << 1 | 1].mn);
    tr[u].cnt = 0;
    if(tr[u].mn == tr[u << 1].mn) tr[u].cnt += tr[u << 1].cnt;
    if(tr[u].mn == tr[u << 1 | 1].mn) tr[u].cnt += tr[u << 1 | 1].cnt;
}

void pushdown(int u)
{
    auto &root = tr[u], &left = tr[u << 1],&right = tr[u << 1 | 1];
    if(root.l == root.r) return ;
    if(root.add)
    {
        left.add += root.add;
        left.mn += root.add;
        right.add += right.add;
        right.mn += root.add;
        root.add = 0;
    }
}

void build(int u,int l,int r)
{
    if(l == r)
    {
        tr[u] = {l,r,1,0,0};
        return ;
    }
    else{
        tr[u] = {l,r,r - l + 1,0,0};
        int mid = (l + r) >> 1;
        build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
        pushup(u);
    }
}

void modify(int u,int l,int r,int d)
{
    if(tr[u].l >= l && tr[u].r <= r)
    {
        tr[u].mn += d;
        tr[u].add += d;
    }
    else{
        pushdown(u);
        int mid = (tr[u].l + tr[u].r) >> 1;
        if(l <= mid) modify(u << 1,l,r,d);
        if(r > mid) modify(u << 1 | 1,l,r,d);
        pushup(u);
    }
}

int query1(int u,int l,int r)
{
    if(tr[u].l >= l && tr[u].r <= r) 
    {
        return tr[u].cnt;
    }
    pushdown(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    int res = 0;
    if(l <= mid && tr[u].mn == tr[u << 1].mn) res += query1(u << 1,l,r);
    if(r > mid && tr[u].mn == tr[u << 1 | 1].mn) res += query1(u << 1 | 1,l,r);
    return res;
}

int query2(int u,int l,int r)
{
    if(tr[u].l >= l && tr[u].r <= r) 
    {
        return tr[u].mn;
    }
    pushdown(u);
    int mid = (tr[u].l + tr[u].r) >> 1;
    int res = 1e9;
    if(l <= mid) res = min(res,query2(u << 1,l,r));
    if(r > mid) res = min(res,query2(u << 1 | 1,l,r));
    return res;
}

void solve()
{
    cin >> n >> k;
    for(int i = 1;i <= n;i++) 
    {
        cin >> a[i];
        pos[a[i]].push_back(i);
    }
    build(1,1,n);
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        int p = lower_bound(pos[a[i]].begin(),pos[a[i]].end(),i) - pos[a[i]].begin();
        if(p == k - 1)
        {
            modify(1,1,pos[a[i]][0],1);
        }
        else if(p == k)
        {
            modify(1,1,pos[a[i]][0],-1);
            modify(1,pos[a[i]][0] + 1,pos[a[i]][1],1);
        }
        else if(p > k)
        {
            modify(1,pos[a[i]][p - k - 1] + 1,pos[a[i]][p - k],-1);
            modify(1,pos[a[i]][p - k] + 1,pos[a[i]][p - k + 1],1);
        }
        if(!query2(1,1,i)) ans += i - query1(1,1,i);
        else ans += i;
    }
    cout << n * (n + 1) / 2 - ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    T = 1;
    while(T--)
    {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 2
1 2 2 1 3 3

output:

10

result:

ok 1 number(s): "10"

Test #2:

score: 0
Accepted
time: 6ms
memory: 28208kb

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: 30284kb

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:

4360

result:

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