#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T &a, T b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T &a, T b){if(a > b) return a = b, true; return false;}
const int N = 5e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
const int SZ = 750;
struct Query{
int l,r,id;
};
int a[N], ans[N], mp[N], cnt[N], res = 0;
Query Q[N];
vector<int> tmp;
void add(int x){
if(cnt[mp[x]] == 2) res--;
cnt[mp[x]]++;
if(cnt[mp[x]] == 2) res++;
return;
}
void rem(int x){
if(cnt[mp[x]] == 2) res--;
cnt[mp[x]]--;
if(cnt[mp[x]] == 2) res++;
return;
}
void solve()
{
int n,q; cin >> n >> q;
FOR(i, 1, n){
cin >> a[i];
tmp.pb(a[i]);
}
tmp.pb(0);
sort(all(tmp));
tmp.erase(unique(all(tmp)), tmp.end());
FOR(i, 0, n){
int p = lower_bound(all(tmp), a[i]) - tmp.begin();
mp[i] = p;
}
FOR(i, 1, q){
cin >> Q[i].l >> Q[i].r;
Q[i].id = i;
}
sort(1 + Q, 1+q+Q, [&](Query x, Query y){
if(x.l/SZ != y.l/SZ) return x.l < y.l;
else return ((x.l/SZ)&1 ? (x.r < y.r) : (x.r > y.r));
});
res = 0;
int cl = 0, cr = 0;
add(0);
FOR(i, 1, q){
while(cr < Q[i].r) add(++cr);
while(cl < Q[i].l) rem(cl++);
while(cr > Q[i].r) rem(cr--);
while(cl > Q[i].l) add(--cl);
ans[Q[i].id] = res;
}
FOR(i, 1, q) cout << ans[i] <<"\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
poklon.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8668 KB |
Output is correct |
2 |
Correct |
2 ms |
8664 KB |
Output is correct |
3 |
Correct |
3 ms |
8532 KB |
Output is correct |
4 |
Correct |
7 ms |
8788 KB |
Output is correct |
5 |
Correct |
112 ms |
15292 KB |
Output is correct |
6 |
Correct |
150 ms |
15456 KB |
Output is correct |
7 |
Correct |
272 ms |
26816 KB |
Output is correct |
8 |
Correct |
481 ms |
32956 KB |
Output is correct |
9 |
Correct |
703 ms |
37060 KB |
Output is correct |
10 |
Correct |
1035 ms |
39356 KB |
Output is correct |