Array Transformer
白书p393
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 300010; 4 const int size = 4096; 5 6 int n, m, u, a[maxn], bk[maxn / size + 1][size]; 7 8 void init(){ 9 scanf("%d %d %d", &n, &m, &u); 10 int b = 0, j = 0; 11 for(int i = 0; i < n; i++){ 12 scanf("%d", &a[i]); 13 bk[b][j] = a[i]; 14 if(++j == size) { 15 b++; j = 0; 16 } 17 } 18 for(int i = 0; i < b; i++) sort(bk[i], bk[i] + size); 19 if(j) sort(bk[b], bk[b] + j); 20 } 21 22 int query(int l, int r, int v){ 23 int lb = l / size, rb = r / size; 24 int k = 0; 25 if(lb == rb) { 26 for(int i = l; i <= r; i++) if(a[i] < v) k++; 27 }else{ 28 for(int i = l; i < (lb + 1) * size; i++) if(a[i] < v) k++; 29 for(int i = rb * size; i <= r; i++) if(a[i] < v) k++; 30 for(int b = lb + 1; b < rb; b++){ 31 k += lower_bound(bk[b], bk[b] + size, v) - bk[b]; 32 } 33 } 34 return k; 35 } 36 37 void change(int p, int x){ 38 if(a[p] == x) return; 39 int old = a[p], pos = 0, *B = &bk[p / size][0]; 40 a[p] = x; 41 while(B[pos] < old) pos++; 42 B[pos] = x; 43 if(x > old) while(pos < size - 1 && B[pos] > B[pos + 1]) swap(B[pos], B[pos + 1]), pos++; 44 else while(pos > 0 && B[pos] < B[pos - 1]) swap(B[pos], B[pos - 1]), pos--; 45 } 46 47 int main(){ 48 init(); 49 while(m--){ 50 int l, r, v, p; 51 scanf("%d %d %d %d", &l, &r, &v, &p); 52 l--; r--; p--; 53 int k = query(l, r, v); 54 change(p, (long long)u * k / (r - l + 1)); 55 } 56 for(int i = 0; i < n; i++) printf("%d ", a[i]); 57 return 0; 58 }