#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) {
if(!i) return;
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
poklon.cpp: In function 'int main()':
poklon.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &n, &m); int l, r;
| ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:59:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | for(int i=1;i<=n;i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
poklon.cpp:65:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d%d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
12 ms |
11952 KB |
Output is correct |
3 |
Correct |
33 ms |
11980 KB |
Output is correct |
4 |
Correct |
13 ms |
12236 KB |
Output is correct |
5 |
Correct |
98 ms |
16452 KB |
Output is correct |
6 |
Correct |
97 ms |
16580 KB |
Output is correct |
7 |
Correct |
238 ms |
21236 KB |
Output is correct |
8 |
Correct |
389 ms |
26372 KB |
Output is correct |
9 |
Correct |
535 ms |
30888 KB |
Output is correct |
10 |
Correct |
696 ms |
34776 KB |
Output is correct |