Submission #40938

# Submission time Handle Problem Language Result Execution time Memory
40938 2018-02-09T23:25:12 Z Hassoony Poklon (COCI17_poklon) C++14
84 / 140
5000 ms 10324 KB
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef double D;
const ll inf=(1ll<<61);
const int mod=1e9+7;
const int MX=5e5+9;
const int SQ=600;
int n,q,ans,res[MX],a[MX],xblock,yblock;
pair<int,pair<int,int> >Q[MX];
unordered_map<int,int>cnt;
bool cmp(pair<int,pair<int,int> > A,pair<int,pair<int,int> > B){
    xblock=A.first/SQ;
    yblock=B.first/SQ;
    if(xblock!=yblock)return xblock<yblock;
    return A.second.first<B.second.first;
}
void add(int x){
    ans-=(cnt[x]==2);
    cnt[x]++;
    ans+=(cnt[x]==2);
}
void rem(int x){
    ans-=(cnt[x]==2);
    cnt[x]--;
    ans+=(cnt[x]==2);
}
int main(){
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<q;i++){
        scanf("%d%d",&Q[i].first,&Q[i].second.first);
        Q[i].second.second=i;
    }
    sort(Q,Q+q,cmp);
    int mol=1,mor=0,L,R;
    for(int i=0;i<q;i++){
        L=Q[i].first,R=Q[i].second.first;
        while(mor<R){
            add(a[++mor]);
        }
        while(mor>R){
            rem(a[mor--]);
        }
        while(mol<L){
            rem(a[mol++]);
        }
        while(mol>L){
            add(a[--mol]);
        }
        res[Q[i].second.second]=ans;
    }
    for(int i=0;i<q;i++)printf("%d\n",res[i]);
}

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:32:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&a[i]);
                          ^
poklon.cpp:35:53: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&Q[i].first,&Q[i].second.first);
                                                     ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 5 ms 352 KB Output is correct
3 Correct 9 ms 424 KB Output is correct
4 Correct 49 ms 648 KB Output is correct
5 Correct 1776 ms 2524 KB Output is correct
6 Correct 1743 ms 2668 KB Output is correct
7 Execution timed out 5052 ms 4640 KB Time limit exceeded
8 Execution timed out 5056 ms 6568 KB Time limit exceeded
9 Execution timed out 5052 ms 8500 KB Time limit exceeded
10 Execution timed out 5040 ms 10324 KB Time limit exceeded