#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
poklon.cpp: In function 'int main()':
poklon.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &n, &m); int l, r;
| ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:58:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | for(int i=1;i<=n;i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
poklon.cpp:64:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d%d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
12 ms |
11980 KB |
Output is correct |
3 |
Correct |
33 ms |
12056 KB |
Output is correct |
4 |
Execution timed out |
5049 ms |
12236 KB |
Time limit exceeded |
5 |
Execution timed out |
5048 ms |
16452 KB |
Time limit exceeded |
6 |
Execution timed out |
5050 ms |
16452 KB |
Time limit exceeded |
7 |
Execution timed out |
5004 ms |
21424 KB |
Time limit exceeded |
8 |
Execution timed out |
5055 ms |
26216 KB |
Time limit exceeded |
9 |
Execution timed out |
5035 ms |
30932 KB |
Time limit exceeded |
10 |
Execution timed out |
5053 ms |
35524 KB |
Time limit exceeded |