You are given a string with length n, and the size of the alphabet also is n.
There are q queries. For a query, you are given two integers l,r and you need to answer the number of different strings (which can be empty) that can be obtained by removing a substring containing [l,r].
输入描述:
The first line contains one integer n (3≤n≤10^5), denoting the length of the string a.
The second line contains nnn integers a1,a2,⋯,an (1≤ai≤n).
The third line contains one integer q (1≤q≤10^5), denoting the number of queries.
For the 4-th to (q+3)-th lines, each line contains two integers l,r(1≤l≤r≤n), denoting a query.
输出描述:
For each query, output a number in a line indicating the answer.
示例1
输入
4 1 2 3 1 6 1 1 3 3 2 3 2 4 1 3 1 4输出
4 5 3 2 2 1说明
The string in the sample is equal to 'abca'.
For the first query 1,1:
'bca' can be obtained by removing substring [1,1].
'ca' can be obtained by removing substring [1,2].
'a' can be obtained by removing substring [1,3].
Empty string can be obtained by removing substring [1,4].
So the answer is 4.
For the third query 2,3:
'aa' can be obtained by removing substring [2,3].
'a' can be obtained by removing substring [1,3] or [2,4].
Empty string can be obtained by removing substring [1,4].
So the answer is 3.
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef double db;
const int N=1e5+10;
int n,q1,ans;
int a[N],Ans[N];
int cntl[N],cntr[N];
struct Q
{
int l,r,id,pos;
} q[N];
int cmp(Q a,Q b)
{
return a.pos==b.pos ? a.r<b.r : a.pos<b.pos;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
int siz=(int)sqrt(n);
cin>>q1;
for(int i=1; i<=q1; i++)
{
cin>>q[i].l>>q[i].r;
q[i].id=i;
q[i].pos=(q[i].l-1)/siz+1;
}
sort(q+1,q+1+q1,cmp);
int l=1,r=n;
for(int i=1; i<=q1; i++)
{
while (r < q[i].r) ans -= cntl[a[++r]], cntr[a[r]]--;
while (l > q[i].l) ans -= cntr[a[--l]], cntl[a[l]]--;
while (r > q[i].r) ans += cntl[a[r]], cntr[a[r--]]++;
while (l < q[i].l) ans += cntr[a[l]], cntl[a[l++]]++;
Ans[q[i].id] = q[i].l * (n - q[i].r + 1ll) - ans;
}
for(int i=1; i<=q1; i++)
{
cout<<Ans[i]<<"\n";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef double db;
const int N=1e5+10;
int n,q1,ans;
int a[N],Ans[N];
int cntl[N],cntr[N];
struct Q
{
int l,r,id,pos;
} q[N];
int cmp(Q a,Q b)
{
return a.pos==b.pos ? a.r<b.r : a.pos<b.pos;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
cntr[a[i]]++;
}
int siz=(int)sqrt(n);
cin>>q1;
for(int i=1; i<=q1; i++)
{
cin>>q[i].l>>q[i].r;
q[i].id=i;
q[i].pos=(q[i].l-1)/siz+1;
}
sort(q+1,q+1+q1,cmp);
int l=1,r=0;
for(int i=1; i<=q1; i++)
{
while (r < q[i].r) ans -= cntl[a[++r]], cntr[a[r]]--;
while (l > q[i].l) ans -= cntr[a[--l]], cntl[a[l]]--;
while (r > q[i].r) ans += cntl[a[r]], cntr[a[r--]]++;
while (l < q[i].l) ans += cntr[a[l]], cntl[a[l++]]++;
Ans[q[i].id] = q[i].l * (n - q[i].r + 1ll) - ans;
}
for(int i=1; i<=q1; i++)
{
cout<<Ans[i]<<"\n";
}
return 0;
}