# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
137131 | KLPP | Poklon (COCI17_poklon) | C++14 | 1975 ms | 15116 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long int lld;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define block 700
int n,q;
int arr[1000000];
multiset<int> m;
struct queries{
int l,r;
int index;
};
bool operator <(queries a, queries b){
if(a.l/block<b.l/block)return true;
if(a.l/block>b.l/block)return false;
return (a.r<b.r);
}
queries Q[1000000];
int L,R;
int freq[1000000];
int ans;
void Rplus(){
R++;
ans-=(freq[arr[R]]==2);
freq[arr[R]]++;
ans+=(freq[arr[R]]==2);
}
void Lminus(){
L--;
ans-=(freq[arr[L]]==2);
freq[arr[L]]++;
ans+=(freq[arr[L]]==2);
}
void Rminus(){
R--;
ans-=(freq[arr[R+1]]==2);
freq[arr[R+1]]--;
ans+=(freq[arr[R+1]]==2);
}
void Lplus(){
L++;
ans-=(freq[arr[L-1]]==2);
freq[arr[L-1]]--;
ans+=(freq[arr[L-1]]==2);
}
void adjust(int l, int r){
while(R<r){
Rplus();
}
while(l<L){
Lminus();
}
while(r<R){
Rminus();
}
while(L<l){
Lplus();
}
//cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
}
int main(){
scanf("%d %d",&n,&q);
vector<int> compress;
rep(i,0,n){
scanf("%d",&arr[i]);
compress.push_back(arr[i]);
}
sort(compress.begin(),compress.end());
rep(i,0,n){
int lo,hi;
lo=-1;
hi=n;
while(hi-lo>1){
int mid=(hi+lo)/2;
if(arr[i]>=compress[mid])lo=mid;
else hi=mid;
}
arr[i]=lo;
//cout<<arr[i]<<" ";
}//cout<<endl;
rep(i,0,n)freq[i]=0;
ans=0;
rep(i,0,q){
scanf("%d %d",&Q[i].l,&Q[i].r);
Q[i].l--;
Q[i].r--;
Q[i].index=i;
}
sort(Q,Q+q);
int answer[n];
L=0;
R=-1;
rep(i,0,q){
adjust(Q[i].l,Q[i].r);
/*cout<<L<<" "<<R<<endl;
rep(i,0,n)cout<<freq[i]<<" ";
cout<<endl;*/
answer[Q[i].index]=ans;
}
rep(i,0,q){
printf("%d\n",answer[i]);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |