2022.4昆明 E Easy String Problem


 

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值