假装自己复活辣。
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
时间: 2024-11-08 18:32:52