#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 5e5+7, M=1e2+1;
const int MOD = 1e9+7;
int n, m, k, t;
struct Node{int v;} st[N<<2];
void update(int idx, int l0, int r0, int p, int v) {
if(l0>p||r0<p) return;
if(l0==r0&&l0==p) {st[idx].v+=v;return;}
int mid=(l0+r0)/2;
update(idx*2, l0, mid, p, v), update(idx*2+1, mid+1, r0, p, v);
st[idx].v=st[idx*2].v+st[idx*2+1].v;
}
int query(int idx, int l0, int r0, int l, int r) {
if(l0>r||r0<l) return 0;
if(l<=l0&&r0<=r) return st[idx].v;
int mid=(l0+r0)/2;
return query(idx*2, l0, mid, l, r)+query(idx*2+1, mid+1, r0, l, r);
}
void upd(int p, int v) {
if(!p) return;
update(1, 1, n, p, v);
}
int qry(int l, int r) {return query(1, 1, n, l, r);}
map<int, int> cnt;
map<int, vector<int> > lst;
int ans[N], a[N];
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]);
for(int i=1;i<=m;i++) {
scanf("%d%d", &l, &r);
q[r].pb(ii(i, l));
}
for(int i=1;i<=n;i++) {
cnt[a[i]]++;
if(cnt[a[i]]>1) {
int vsz=sz(lst[a[i]]);
upd(lst[a[i]][vsz-1], 1);
upd(lst[a[i]][vsz-2], -2);
if(vsz>2) upd(lst[a[i]][vsz-3], 1);
} else lst[a[i]].pb(0);
for(int j=0;j<sz(q[i]);j++) {
k=q[i][j].SS, t=q[i][j].FF;
ans[t]=qry(k, 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 cases (n=1?)
- do smth instead of do nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d", &n, &m);int l, r;
| ~~~~~^~~~~~~~~~~~~~~~
poklon.cpp:50:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | for(int i=1;i<=n;i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
poklon.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d%d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
12096 KB |
Output is correct |
3 |
Correct |
8 ms |
12116 KB |
Output is correct |
4 |
Correct |
14 ms |
12248 KB |
Output is correct |
5 |
Correct |
187 ms |
16488 KB |
Output is correct |
6 |
Correct |
199 ms |
16580 KB |
Output is correct |
7 |
Correct |
460 ms |
21420 KB |
Output is correct |
8 |
Correct |
718 ms |
27488 KB |
Output is correct |
9 |
Correct |
985 ms |
31040 KB |
Output is correct |
10 |
Correct |
1235 ms |
33872 KB |
Output is correct |