QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#734500 | #9584. 顾影自怜 | Zhou_JK# | WA | 92ms | 19068kb | C++23 | 1.3kb | 2024-11-11 11:34:42 | 2024-11-11 11:34:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=1000005;
int n,k;
int a[N];
int lst[N],nxt[N];
vector<int>pos[N];
void solve()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
pos[i].clear();
for(int i=1;i<=n;i++)
{
cin>>a[i];
pos[a[i]].emplace_back(i);
}
stack<int>s;
for(int i=1;i<=n;i++)
{
while(!s.empty()&&a[s.top()]<=a[i]) s.pop();
if(s.empty()) lst[i]=0;
else lst[i]=s.top();
s.emplace(i);
}
while(!s.empty()) s.pop();
for(int i=n;i>=1;i--)
{
while(!s.empty()&&a[s.top()]<=a[i]) s.pop();
if(s.empty()) nxt[i]=n+1;
else nxt[i]=s.top();
s.emplace(i);
}
long long 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>=0)
{
int ll=lst[i]+1,lr=pos[a[i]][p-k+1];
int rl=i,rr=nxt[i]-1;
if(p+1<(int)pos[a[i]].size()) rr=min(rr,pos[a[i]][p+1]-1);
if(ll<=rr&&rl<=rr) ans+=(long long)(lr-ll+1)*(rr-rl+1);
}
}
cout<<ans<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9660kb
input:
2 5 2 1 3 3 2 2 4 3 1 4 2 1
output:
7 0
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 92ms
memory: 19068kb
input:
1 1000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
500000500000
result:
ok single line: '500000500000'
Test #3:
score: -100
Wrong Answer
time: 69ms
memory: 9784kb
input:
158921 1 1 1 2 1 1 1 2 2 1 1 2 1 1 2 2 2 1 2 2 1 2 1 2 2 2 1 2 1 2 2 2 2 2 2 3 1 1 1 1 3 2 1 1 1 3 3 1 1 1 3 1 1 1 2 3 2 1 1 2 3 3 1 1 2 3 1 1 1 3 3 2 1 1 3 3 3 1 1 3 3 1 1 2 1 3 2 1 2 1 3 3 1 2 1 3 1 1 2 2 3 2 1 2 2 3 3 1 2 2 3 1 1 2 3 3 2 1 2 3 3 3 1 2 3 3 1 1 3 1 3 2 1 3 1 3 3 1 3 1 3 1 1 3 2 3 2...
output:
1 3 1 3 0 3 0 3 1 6 3 1 6 1 0 6 1 0 6 -1 0 6 2 0 6 0 0 6 -1 0 6 0 0 6 2 0 6 1 0 6 1 0 6 0 0 6 2 0 6 3 1 6 1 0 6 0 0 6 -1 0 6 2 0 6 1 0 6 0 0 6 1 0 6 0 0 6 1 0 6 1 0 6 2 0 6 2 0 6 3 1 10 6 3 1 10 3 1 0 10 3 1 0 10 3 1 0 10 0 -2 0 10 4 0 0 10 1 0 0 10 1 0 0 10 0 -2 0 10 1 0 0 10 4 0 0 10 1 0 0 10 0 -2...
result:
wrong answer 20th lines differ - expected: '0', found: '-1'