• The 2022 ICPC Asia Regionals Online Contest (II) G


    G Good Permutation

    For a permutation P of length n, we define mxl,r​=maxi=lr​Pi​,mnl,r​=mini=lr​Pi​

    For a permutation, we call it the good interval if and only if mxl,r​−mnl,r​=r−l for a interval [l,r](1≤l≤r≤n).

    You have some requirements for permutations, you hope that the generated permutations have some good intervals, Specifically, you have m restrictions, and the i th restriction requires the interval [l,r] to be a good interval ,and you want to know the number of such permutations. The answer can be very large, you need to output the result of the answer modulo 10^9+7. The input guarantees that for any two restrictions, there are only inclusive and disjoint relations.

    输入格式:

    The first line contains two positive integer n,m (1≤n,m≤10^6), indicating the length of the permutation and the number of restrictions.

    The next m lines, each line contains two positive integers li​,ri​(1≤li​≤ri​≤n), indicating the ith restriction.

    For any two restrictions 1≤i,j≤m, if li​>lj​, then ri​≤rj​ or rj​

    输出格式:

    Output a line with a positive integer indicating the number of permutations that meet the requirements.

    输入样例:

    1. 5 3
    2. 1 5
    3. 1 4
    4. 1 3

    输出样例:

    24
    

    代码长度限制

    16 KB

    时间限制

    1000 ms

    内存限制

    256 MB

    树形dp

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. typedef double db;
    5. const int N=1e6+10;
    6. const int mod=1e9+7;
    7. int n,m;
    8. int fac[N];
    9. vector<int>st[N],ed[N],g[N];
    10. int len[N];
    11. int dfs(int u)
    12. {
    13. int res = 1;
    14. int s = 0;
    15. for (int v : g[u])
    16. {
    17. res *= dfs(v);
    18. s += len[v];
    19. res %= mod;
    20. }
    21. res *= fac[len[u] - s + g[u].size()];
    22. res %= mod;
    23. return res;
    24. }
    25. void solve()
    26. {
    27. fac[0]=1;
    28. for(int i=1;i<=N;i++)
    29. {
    30. fac[i]=i*fac[i-1]%mod;
    31. }
    32. cin>>n>>m;
    33. vector<pair<int,int>>pp;
    34. pp.push_back({ 0,0 });
    35. for(int i=1;i<=m;i++)
    36. {
    37. int l,r;
    38. cin>>l >>r;
    39. pp.push_back({ l,r });
    40. }
    41. sort(pp.begin(),pp.end());
    42. for(int i=1;i<pp.size();i++)
    43. {
    44. if (pp[i]!=pp[i-1])
    45. {
    46. int L=pp[i].first,R=pp[i].second;
    47. len[i]=R-L+1;
    48. st[L].push_back(i);
    49. ed[R].push_back(i);
    50. }
    51. }
    52. for(int i = 1; i <= n; i++)
    53. {
    54. sort(st[i].begin(), st[i].end(), [](int a, int b)
    55. {
    56. return len[a] > len[b];
    57. });
    58. sort(ed[i].begin(), ed[i].end(), [](int a, int b)
    59. {
    60. return len[a] < len[b];
    61. });
    62. }
    63. stack<int>stk;
    64. stk.push(0);
    65. for (int i=1;i<=n;i++)
    66. {
    67. for (int x : st[i])
    68. {
    69. stk.push(x);
    70. }
    71. for (int x : ed[i])
    72. {
    73. if (x == stk.top())
    74. {
    75. stk.pop();
    76. int fa = stk.top();
    77. g[fa].push_back(x);
    78. // cout<<fa<<" "<<x<<"\n";
    79. }
    80. }
    81. }
    82. len[0] = n;
    83. cout<<dfs(0)<<"\n";
    84. }
    85. signed main()
    86. {
    87. ios::sync_with_stdio(false);
    88. cin.tie(0);
    89. cout.tie(0);
    90. solve();
    91. return 0;
    92. }

     

  • 相关阅读:
    PXE网络批量装机(centos7)
    Linux打包发布常用命令
    Java 网络编程
    单基因泛癌+实验简单验证,要素丰富,没研究方向的赶紧上车
    GemBox.Bundle 47.0.1227 Crack
    2023 年诺贝尔物理学奖-阿秒光谱学
    设计模式之一单一职责原则(东方化)
    【算法练习Day2】有序数组的平方&&长度最小子数组&&螺旋矩阵II
    异步编程 - 09 Spring框架中的异步执行_@Async注解异步执行原理&源码解析
    DELL服务器,CPU一直会提示温度超过阈值。针对CPU temperature is greater than the upper crit
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/127068955