题目链接

Problem Description

Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one day Klay comes up with a problem and ask him about it.

Given two integer sequences {ai} and {bi} with the same length n, you are to find the next n numbers of {ai}: an+1…a2n. Just like always, there are some restrictions on an+1…a2n: for each number ai, you must choose a number bk from {bi}, and it must satisfy ai≤max{aj-j│bk≤j<i}, and any bk can’t be chosen more than once. Apparently, there are a great many possibilities, so you are required to find max{∑2nn+1ai} modulo 109+7 .

Now Steph finds it too hard to solve the problem, please help him.

Input

The input contains no more than 20 test cases.

For each test case, the first line consists of one integer n. The next line consists of n integers representing {ai}. And the third line consists of n integers representing {bi}.

1≤n≤250000, n≤a_i≤1500000, 1≤b_i≤n.

Output

For each test case, print the answer on one line: max{∑2nn+1ai} modulo 109+7。

Sample Input

4

8 11 8 5

3 1 4 2

Sample Output

27

Hint

For the first sample:

  1. Choose 2 from {bi}, then a_2…a_4 are available for a_5, and you can let a_5=a_2-2=9;
  2. Choose 1 from {bi}, then a_1…a_5 are available for a_6, and you can let a_6=a_2-2=9;

分析:

每次需要从已有的a数组(a数组保存的就是本来的值减去下标)里面找到一个最大值,然后将这个值加入到a数组后面,接着在重复上面的过程。每次在找到一个值之后,线段树都要更新。

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #define lchild left,mid,root<<1
  5. #define rchild mid+1,right,root<<1|1
  6. #define inf 0x3f3f3f3f
  7. using namespace std;
  8. const int maxn = 600000;
  9. const int mod = 1e9+7;
  10. int Max[maxn<<2];
  11. int a[maxn];
  12. int b[maxn];
  13. void push_up(int root)
  14. {
  15. Max[root] = max(Max[root<<1],Max[root<<1|1]);
  16. }
  17. ///构建线段树
  18. void build(int left,int right,int root)
  19. {
  20. if(left == right)
  21. {
  22. Max[root] = a[left];
  23. return;
  24. }
  25. int mid = (left+right)>>1;
  26. build(lchild);
  27. build(rchild);
  28. push_up(root);
  29. }
  30. int query(int L,int R,int left,int right,int root)///[L,R]是需要查找的区间
  31. {
  32. if(L<=left && right<=R)
  33. return Max[root];
  34. int mid = (left+right)>>1;
  35. int ans = -inf;
  36. if(L<=mid) ans = max(ans,query(L,R,lchild));
  37. if(R>mid) ans = max(ans,query(L,R,rchild));
  38. return ans;
  39. }
  40. void Insert(int pos,int left,int right,int root)
  41. {
  42. if(left == right)
  43. {
  44. Max[root] = a[pos]-pos;
  45. return;
  46. }
  47. int mid = (left+right)>>1;
  48. if(pos<=mid) Insert(pos,lchild);
  49. else Insert(pos,rchild);
  50. push_up(root);
  51. }
  52. int main()
  53. {
  54. int n;
  55. while(~scanf("%d",&n))
  56. {
  57. for(int i = 1; i <= n; i++)
  58. {
  59. scanf("%d",&a[i]);
  60. a[i] = a[i]-i;///a数组中直接保存为它本身减去下标的值,从而每次从里面取得最大值
  61. }
  62. for(int i = 1; i <= n; i++)
  63. {
  64. scanf("%d",&b[i]);
  65. a[i+n] = -inf;
  66. }
  67. sort(b,b+n);
  68. build(1,2*n,1);///因为在从后面取的时候要从整个数组里面取包括后面的部分
  69. int ans = 0;
  70. int range = n;
  71. for(int i = 1; i <= n; i++)
  72. {
  73. int num = query(b[i],range,1,2*n,1);
  74. range++;
  75. a[i+n] = num;
  76. Insert(n+i,1,2*n,1);
  77. ans = (ans+num)%mod;
  78. }
  79. printf("%d\n",ans);
  80. }
  81. return 0;
  82. }

2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 6 1003 HDU 6098 Inversion (模拟)

    题目链接 Problem Description Give an array A, the index starts from 1. Now we want to know Bi=maxi∤jAj , ...

  2. 2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)

    题目链接 Problem Description In mathematics, the function d(n) denotes the number of divisors of positiv ...

  3. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  4. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  5. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  6. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  7. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  8. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  9. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

随机推荐

  1. 学生导师互选系统(php代码规范)

    学生导师互选系统(php代码规范) php编码规范 组名:一不小心就火了 负责项目:学生导师互选系统(安卓端) 编写目的 为了更好的提高团队的的合作效率,保证开发的有效性和合理性,并可最大程度的提高程 ...

  2. PAT 甲级 1144 The Missing Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...

  3. 【Nginx】nginx为目录或网站加上密码认证

    第一步生成pwd用户名密码文件 工具:http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py 步骤: chmod 777 htp ...

  4. 超强汇总!110 道 Python 面试笔试题

    https://mp.weixin.qq.com/s/hDQrimihoaHSbrtjLybZLA 今天给大家分享了110道面试题,其中大部分是巩固基本python知识点,希望刚刚入手python,对 ...

  5. [翻译]Android官方文档 - 通知(Notifications)

    翻译的好辛苦,有些地方也不太理解什么意思,如果有误,还请大神指正. 官方文档地址:http://developer.android.com/guide/topics/ui/notifiers/noti ...

  6. js & right click menu

    js & right click menu https://stackoverflow.com/questions/4909167/how-to-add-a-custom-right-clic ...

  7. 【UNIX环境编程、操作系统】孤儿进程和僵尸进程

    基本概念: 在类UNIX系统中,僵尸进程是指完成执行(通过exit系统调用,或运行时发生致命错误或收到终止信号所致)但在操作系统的进程表中仍然有一个进程表表项(进程控制块PCB),处于"终止 ...

  8. node+express搭建个人网站(2)

    node+express搭建个人网站(1)这一节中成功启动了一个网站但还很简陋,仅仅打印了一个helloworld的网页 作为个人网站,我们当然想输出自己设计好的网页, 我们借助 Express 应用 ...

  9. bootstrap-datapicker 时间约束

    <div class="input-group date date-picker" id="StartTime"> <input type=& ...

  10. Arrays.toString 如果传入的是对象 那么调用的是此对象的toString

    Arrays.toString(Object[] obj) 如果传入参数的是对象 那么调用的是此对象的toString