• D - Linear Probing- 并查集


    D - Linear Probing Editorial

     / 


    Time Limit: 4 sec / Memory Limit: 1024 MB

    Score : 400400 points

    Problem Statement

    There is a sequence A = (A_0, A_1, \dots, A_{N - 1})A=(A0​,A1​,…,AN−1​) with N = 2^{20}N=220 terms. Initially, every term is -1−1.

    Process QQ queries in order. The ii-th query (1 \leq i \leq Q)(1≤i≤Q) is described by an integer t_iti​ such that t_i = 1ti​=1 or t_i = 2ti​=2, and another integer x_ixi​, as follows.

    • If t_i = 1ti​=1, do the following in order.
      1. Define an integer hh as h = x_ih=xi​.
      2. While A_{h \bmod N} \neq -1AhmodN​=−1, keep adding 11 to hh. We can prove that this process ends after finite iterations under the Constraints of this problem.
      3. Replace the value of A_{h \bmod N}AhmodN​ with x_ixi​.
    • If t_i = 2ti​=2, print the value of A_{x_i \bmod N}Axi​modN​ at that time.

    Here, for integers aa and bb, a \bmod bamodb denotes the remainder when aa is divided by bb.

    Constraints

    • 1 \leq Q \leq 2 \times 10^51≤Q≤2×105
    • t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)ti​∈{1,2}(1≤i≤Q)
    • 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)0≤xi​≤1018(1≤i≤Q)
    • There is at least one ii (1 \leq i \leq Q)(1≤i≤Q) such that t_i = 2ti​=2.
    • All values in input are integers.

    Input

    Input is given from Standard Input in the following format:

    QQ
    t_1t1​ x_1x1​
    \vdots⋮
    t_{Q}tQ​ x_{Q}xQ​
    

    Output

    For each query with t_i = 2ti​=2, print the response in one line. It is guaranteed that there is at least one such query.


    Sample Input 1 Copy

    Copy

    4
    1 1048577
    1 1
    2 2097153
    2 3
    

    Sample Output 1 Copy

    Copy

    1048577
    -1
    

    We have x_1 \bmod N = 1x1​modN=1, so the first query sets A_1 = 1048577A1​=1048577.

    In the second query, initially we have h = x_2h=x2​, for which A_{h \bmod N} = A_{1} \neq -1AhmodN​=A1​=−1, so we add 11 to hh. Now we have A_{h \bmod N} = A_{2} = -1AhmodN​=A2​=−1, so this query sets A_2 = 1A2​=1.

    In the third query, we print A_{x_3 \bmod N} = A_{1} = 1048577Ax3​modN​=A1​=1048577.

    In the fourth query, we print A_{x_4 \bmod N} = A_{3} = -1Ax4​modN​=A3​=−1.

    Note that, in this problem, N = 2^{20} = 1048576N=220=1048576 is a constant and not given in input.

    =========================================================================非常巧妙的一个题目,直接循环外加优化的话只会TLE一个点,本题正解是并查集,每当修改一个位置,就把这个位置的父亲节点和父亲节点+1位置合并,注意合并顺序,这样我们就完美的定位了下一个位置,十分精巧

    1. # include
    2. # include
    3. # include
    4. # include
    5. # include
    6. # include
    7. # include
    8. # pragma optimize(2)
    9. # define mod 1048576
    10. using namespace std;
    11. typedef long long int ll;
    12. int f[(1<<20)+10];
    13. ll ans[(1<<20)+10];
    14. bool book[1048576+10];
    15. ll getf(int x)
    16. {
    17. if(x==f[x])
    18. return x;
    19. else
    20. {
    21. f[x]=getf(f[x]);
    22. return f[x];
    23. }
    24. }
    25. void hebing(int x,int y)
    26. {
    27. int t1=getf(x);
    28. int t2=getf(y);
    29. if(t1!=t2)
    30. f[t1]=t2;
    31. }
    32. int main ()
    33. {
    34. for(int i=0; i<(1<<20); i++)
    35. {
    36. f[i]=i;
    37. }
    38. int t;
    39. cin>>t;
    40. while(t--)
    41. {
    42. ll x,y;
    43. scanf("%lld%lld",&x,&y);
    44. if(x==1)
    45. {
    46. ll pre=y;
    47. y%=1048576;
    48. ll now=getf(y);
    49. ans[now]=pre;
    50. book[now]=1;
    51. hebing(now,(now+1)%1048576);
    52. }
    53. else
    54. {
    55. ll now=y%1048576;
    56. if(!book[now])
    57. {
    58. cout<<-1<<'\n';
    59. }
    60. else
    61. {
    62. cout<'\n';
    63. }
    64. }
    65. }
    66. return 0;
    67. }

     

  • 相关阅读:
    Maven笔记
    C# MQTT通讯
    笔试面试相关记录(7)
    淘宝平台API接口,爬虫数据(数据参数汇总)
    天线材质介绍--FPC天线
    两个列表的最小索引总和
    Spring源码:SpringBean 的注册-XML源码解析
    惊艳!Linux 中迷人的 Shell 脚本工具
    MySQL——常见约束
    Hive-启动与操作(2)
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126094788