Array Transformer

Time Limit: 5000ms
Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 12003
64-bit integer IO format: %lld      Java class name: Main

 

Write a program to transform an array A[1], A[2],..., A[n] according to m instructions. Each instruction (LRvp) means: First, calculate how many numbers from A[L] to A[R](inclusive) are strictly less than v, call this answer k. Then, change the value of A[p] to u*k/(R - L + 1), here we use integer division (i.e. ignoring fractional part).

Input

The first line of input contains three integer nmu ( 1UVA 12003 Array Transformer-LMLPHPnUVA 12003 Array Transformer-LMLPHP300, 000, 1UVA 12003 Array Transformer-LMLPHPmUVA 12003 Array Transformer-LMLPHP50, 000, 1UVA 12003 Array Transformer-LMLPHPuUVA 12003 Array Transformer-LMLPHP1, 000, 000, 000). Each of the next n lines contains an integer A[i] ( 1UVA 12003 Array Transformer-LMLPHPA[i]UVA 12003 Array Transformer-LMLPHPu). Each of the next m lines contains an instruction consisting of four integers LRvp ( 1UVA 12003 Array Transformer-LMLPHPLUVA 12003 Array Transformer-LMLPHPRUVA 12003 Array Transformer-LMLPHPn, 1UVA 12003 Array Transformer-LMLPHPvUVA 12003 Array Transformer-LMLPHPu, 1UVA 12003 Array Transformer-LMLPHPpUVA 12003 Array Transformer-LMLPHPn).

Output

Print n lines, one for each integer, the final array.

Sample Input

10 1 11
1
2
3
4
5
6
7
8
9
10
2 8 6 10

Sample Output

1
2
3
4
5
6
7
8
9
6

Explanation: There is only one instruction: L = 2, R = 8, v = 6, p = 10. There are 4 numbers (2,3,4,5) less than 6, so k = 4. The new number in A[10] is 11*4/(8 - 2 + 1) = 44/7 = 6.

解题:分块

参考lrj同学的那本大白

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = + ;
const int SIZE = ;
int n,m,u,A[maxn],block[maxn/SIZE+][SIZE];
void init() {
scanf("%d%d%d",&n,&m,&u);
int b = ,j = ;
for(int i = ; i < n; ++i) {
scanf("%d",A+i);
block[b][j] = A[i];
if(++j == SIZE) {
b++;
j = ;
}
}
for(int i = ; i < b; ++i)
sort(block[i],block[i] + SIZE);
if(j) sort(block[b],block[b]+j);
}
int query(int L,int R,int v) {
int lb = L/SIZE,rb = R/SIZE,k = ;
if(lb == rb) {
for(int i = L; i <= R; ++i)
k += (A[i] < v);
} else {
for(int i = L; i < (lb+)*SIZE; ++i)
if(A[i] < v) ++k;
for(int i = rb*SIZE; i <= R; ++i)
if(A[i] < v) ++k;
for(int i = lb+; i < rb; ++i)
k += lower_bound(block[i],block[i]+SIZE,v) - block[i];
}
return k;
}
void update(int p,int x) {
if(A[p] == x) return;
int old = A[p],pos = ,*B = &block[p/SIZE][];
A[p] = x;
while(B[pos] < old) ++pos;
B[pos] = x;
while(pos < SIZE- && B[pos] > B[pos + ]) {
swap(B[pos],B[pos+]);
++pos;
}
while(pos > && B[pos] < B[pos - ]) {
swap(B[pos],B[pos-]);
--pos;
}
}
int main() {
init();
while(m--) {
int L,R,v,p;
scanf("%d%d%d%d",&L,&R,&v,&p);
--L;
--R;
--p;
int k = query(L,R,v);
update(p,(LL)u*k/(R - L + ));
}
for(int i = ; i < n; ++i)
printf("%d\n",A[i]);
return ;
}
05-11 17:55