# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
481026 | LptN21 | Poklon (COCI17_poklon) | C++14 | 5055 ms | 35524 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;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define PI acos(-1.0)
#define FF first
#define SS second
#define eps 1e-9
// vector
#define pb push_back
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define lb lower_bound
#define ub upper_bound
// bit
#define CNTBIT(x) __builtin_popcount(x)
#define ODDBIT(x) __builtin_parity(x)
#define MASK(i) (1ll<<(i))
#define BIT(x, i) (((x)>>(i))&1)
#define SUBSET(big, small) (((big)&(small))==(small))
#define MINBIT(x) (x)&(-x)
// typedef
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, int> ii;
typedef pair<int, ii> i3;
/* CODE BELOW */
const int N=500000+7, M=447; // M=SQRT(N) ~ 447 + const
const int MOD=1e9+7;
const ll oo=1e18+9;
int n, m, k, t;
int fen[N];
void upd(int i, int v) {
for(;i<=n;i+=i&-i) fen[i]+=v;
}
int get(int i) {
int res=0;
for(;i>0;i-=i&-i) res+=fen[i];
return res;
}
int qry(int l, int r) {
return get(r)-get(l-1);
}
int ans[N], a[N];
map<int, vector<int> > lst;
map<int, int> cnt;
vector<ii> q[N];
signed main() {
//freopen("test.INP", "r", stdin);
//freopen("test.OUT", "w", stdout);
//fastIO;
scanf("%d%d", &n, &m); int l, r;
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
const int SUB1=3000;
if(max(n, m)<=SUB1) {
for(int i=1;i<=m;i++) {
scanf("%d%d", &l, &r);
map<int, int> _cnt;
int _ans=0;
for(int j=l;j<=r;j++) _cnt[a[j]]++;
for(auto it=_cnt.begin();it!=_cnt.end();it++)
if(it->SS==2) _ans++;
printf("%d\n", _ans);
}
return 0;
}
for(int i=1;i<=m;i++) {
scanf("%d%d", &l, &r);
q[r].pb(ii(l, i));
}
for(int i=1;i<=n;i++) {
cnt[a[i]]++;
if(cnt[a[i]]>1) {
int lsz=sz(lst[a[i]]);
upd(lst[a[i]][lsz-1], 1);
upd(lst[a[i]][lsz-2], -2);
if(lsz>2) upd(lst[a[i]][lsz-3], 1);
} else lst[a[i]].pb(0);
for(ii qr:q[i]) {
tie(l, r)=qr;
ans[r]=qry(l, i);
}
lst[a[i]].pb(i);
}
for(int i=1;i<=m;i++) printf("%d\n", ans[i]);
return 0;
}
/* stuff you should look for
- int overflow, array bounds
- special/edge cases (n=1?)
- do smth instead of nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |