标签:tom line over ++ i++ space The class turn
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5 + 10; 8 int a[maxn], sorted[maxn]; 9 int num[20][maxn], val[20][maxn]; 10 11 void build(int l, int r, int dep) { 12 if (l == r) return ; 13 int mid = (l + r) >> 1, same = mid - l + 1 ; 14 for (int i = l ; i <= r ; i++) 15 if (val[dep][i] < sorted[mid]) same--; 16 int lpos = l, rpos = mid + 1; 17 for (int i = l ; i <= r ; i++ ) { 18 if (val[dep][i] < sorted[mid]) val[dep + 1][lpos++] = val[dep][i]; 19 else if ( val[dep][i] == sorted[mid] && same > 0) { 20 val[dep + 1][lpos++] = val[dep][i]; 21 same--; 22 } else val[dep + 1][rpos++] = val[dep][i]; 23 num[dep][i] = num[dep][l - 1] + lpos - l; 24 } 25 build(l, mid, dep + 1); 26 build(mid + 1, r, dep + 1); 27 } 28 29 int query(int L, int R, int l, int r, int dep, int k) { 30 if (l == r) { 31 if (val[dep][l] <= k) return 1; 32 else return 0; 33 } 34 int mid = (L + R) >> 1; 35 int cnt = num[dep][r] - num[dep][l - 1]; 36 if (sorted[mid] <= k) { 37 int newr = r + num[dep][R] - num[dep][r]; 38 int newl = newr - (r - l + 1 - cnt) + 1; 39 return cnt + query(mid + 1, R, newl, newr, dep + 1, k); 40 } else { 41 int newl = L + num[dep][l - 1] - num[dep][L - 1]; 42 int newr = newl + cnt - 1; 43 if (newr >= newl) return query(L, mid, newl, newr, dep + 1, k); 44 else return 0; 45 } 46 } 47 int main() { 48 int t, n, m, l, r, k, cas = 1; 49 scanf("%d", &t); 50 while(t--) { 51 scanf("%d%d", &n, &m); 52 for (int i = 1 ; i <= n ; i++ ) { 53 scanf("%d", &val[0][i]); 54 sorted[i] = val[0][i]; 55 } 56 sort(sorted + 1, sorted + n + 1); 57 build(1, n, 0); 58 printf("Case %d:\n", cas++); 59 while(m--) { 60 scanf("%d%d%d", &l, &r, &k); 61 printf("%d\n", query(1, n, l + 1, r + 1, 0, k)); 62 } 63 } 64 return 0; 65 }
标签:tom line over ++ i++ space The class turn
原文地址:https://www.cnblogs.com/qldabiaoge/p/9107513.html