• 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.

     

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. typedef double db;
    5. const int N=1e5+10;
    6. int n,q1,ans;
    7. int a[N],Ans[N];
    8. int cntl[N],cntr[N];
    9. struct Q
    10. {
    11. int l,r,id,pos;
    12. } q[N];
    13. int cmp(Q a,Q b)
    14. {
    15. return a.pos==b.pos ? a.r<b.r : a.pos<b.pos;
    16. }
    17. signed main()
    18. {
    19. ios::sync_with_stdio(false);
    20. cin.tie(0);
    21. cout.tie(0);
    22. cin>>n;
    23. for(int i=1; i<=n; i++)
    24. {
    25. cin>>a[i];
    26. }
    27. int siz=(int)sqrt(n);
    28. cin>>q1;
    29. for(int i=1; i<=q1; i++)
    30. {
    31. cin>>q[i].l>>q[i].r;
    32. q[i].id=i;
    33. q[i].pos=(q[i].l-1)/siz+1;
    34. }
    35. sort(q+1,q+1+q1,cmp);
    36. int l=1,r=n;
    37. for(int i=1; i<=q1; i++)
    38. {
    39. while (r < q[i].r) ans -= cntl[a[++r]], cntr[a[r]]--;
    40. while (l > q[i].l) ans -= cntr[a[--l]], cntl[a[l]]--;
    41. while (r > q[i].r) ans += cntl[a[r]], cntr[a[r--]]++;
    42. while (l < q[i].l) ans += cntr[a[l]], cntl[a[l++]]++;
    43. Ans[q[i].id] = q[i].l * (n - q[i].r + 1ll) - ans;
    44. }
    45. for(int i=1; i<=q1; i++)
    46. {
    47. cout<<Ans[i]<<"\n";
    48. }
    49. return 0;
    50. }

     

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. typedef double db;
    5. const int N=1e5+10;
    6. int n,q1,ans;
    7. int a[N],Ans[N];
    8. int cntl[N],cntr[N];
    9. struct Q
    10. {
    11. int l,r,id,pos;
    12. } q[N];
    13. int cmp(Q a,Q b)
    14. {
    15. return a.pos==b.pos ? a.r<b.r : a.pos<b.pos;
    16. }
    17. signed main()
    18. {
    19. ios::sync_with_stdio(false);
    20. cin.tie(0);
    21. cout.tie(0);
    22. cin>>n;
    23. for(int i=1; i<=n; i++)
    24. {
    25. cin>>a[i];
    26. cntr[a[i]]++;
    27. }
    28. int siz=(int)sqrt(n);
    29. cin>>q1;
    30. for(int i=1; i<=q1; i++)
    31. {
    32. cin>>q[i].l>>q[i].r;
    33. q[i].id=i;
    34. q[i].pos=(q[i].l-1)/siz+1;
    35. }
    36. sort(q+1,q+1+q1,cmp);
    37. int l=1,r=0;
    38. for(int i=1; i<=q1; i++)
    39. {
    40. while (r < q[i].r) ans -= cntl[a[++r]], cntr[a[r]]--;
    41. while (l > q[i].l) ans -= cntr[a[--l]], cntl[a[l]]--;
    42. while (r > q[i].r) ans += cntl[a[r]], cntr[a[r--]]++;
    43. while (l < q[i].l) ans += cntr[a[l]], cntl[a[l++]]++;
    44. Ans[q[i].id] = q[i].l * (n - q[i].r + 1ll) - ans;
    45. }
    46. for(int i=1; i<=q1; i++)
    47. {
    48. cout<<Ans[i]<<"\n";
    49. }
    50. return 0;
    51. }

     

  • 相关阅读:
    使用阿里云无影云电脑能干什么?
    Linux网络-UDP/TCP协议详解
    【ES8】新特性
    【力扣 Hot100 | 第六天】4.21(字母异位词分组)
    deepin-anything 源码刨析
    【编程题】【Scratch四级】2020.12 绘图程序优化
    跨平台密码管理器KeePassX停止开发,你用过吗?
    Feign远程调用丢失请求头问题
    vue学习之组件化开发
    产品经理如何进行需求管理
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/126986377