码迷,mamicode.com
首页 > 其他好文 > 详细

第一周 7.10-7.16

时间:2016-07-10 16:31:22      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

假装自己复活辣。

 

7.10

CF 689 D Friends and Subsequences

二分。

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 const int maxn = 2e5 + 10;
 7 int n, a[maxn], b[maxn];
 8 
 9 // RMQ
10 int M[maxn][20], m[maxn][20];
11 void init()
12 {
13     for(int i = 1; i <= n; i++) M[i][0] = a[i];
14     for(int j = 1; (1 << j) <= n; j++)
15         for(int i = 1; i + ( 1 << j ) - 1 <= n; i++)
16             M[i][j] = max(M[i][j-1] , M[i+(1<<j-1)][j-1]);
17 
18     for(int i = 1; i <= n; i++) m[i][0] = b[i];
19     for(int j = 1; (1 << j) <= n; j++)
20         for(int i = 1; i + ( 1 << j ) - 1 <= n; i++)
21             m[i][j] = min(m[i][j-1] , m[i+(1<<j-1)][j-1]);
22 }
23 
24 int M_query(int l, int r)
25 {
26     int k = 0;
27     while( ( 1 << (k + 1) ) <= r - l + 1 ) k++;
28     return max(M[l][k], M[r-(1<<k)+1][k]);
29 }
30 
31 int m_query(int l, int r)
32 {
33     int k = 0;
34     while( ( 1 << (k + 1) ) <= r - l + 1 ) k++;
35     return min(m[l][k], m[r-(1<<k)+1][k]);
36 }
37 
38 int main(void)
39 {
40     scanf("%d", &n);
41     for(int i = 1; i <= n; i++) scanf("%d", a + i);
42     for(int i = 1; i <= n; i++) scanf("%d", b + i);
43     init();
44 
45     LL ans = 0LL;
46     for(int l = 1; l <= n; l++)
47     {
48         int t1, t2;
49         int L = l, R = n, mid;
50 
51         if(M_query(l, n) < m_query(l, n)) R = n + 1;
52         else while(L < R)
53         {
54             mid = L + (R - L) / 2;
55             if(M_query(l, mid) < m_query(l, mid)) L = mid + 1;
56             else R = mid;
57         }
58         t1 = R;
59 
60         L = l, R = n;
61         if(M_query(l, n) - 1 < m_query(l, n)) R = n + 1;
62         else while(L < R)
63         {
64             mid = L + (R - L) / 2;
65             if(M_query(l, mid) - 1 < m_query(l, mid)) L = mid + 1;
66             else R = mid;
67         }
68         t2 = R;
69 
70         ans = ans + t2 - t1;
71     }
72 
73     printf("%I64d\n", ans);
74 
75     return 0;
76 }
Aguin

 

第一周 7.10-7.16

标签:

原文地址:http://www.cnblogs.com/Aguin/p/5657861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!