您的位置:首页 > 其它

UVA 12003 Array Transformer

2015-08-01 10:00 302 查看

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 (L, R, v, p) 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 n, m, u ( 1

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 300000 + 10;
const int SIZE = 4096;
int n,m,u,A[maxn],block[maxn/SIZE+1][SIZE];
void init() {
scanf("%d%d%d",&n,&m,&u);
int b = 0,j = 0;
for(int i = 0; i < n; ++i) {
scanf("%d",A+i);
block[b][j] = A[i];
if(++j == SIZE) {
b++;
j = 0;
}
}
for(int i = 0; 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 = 0;
if(lb == rb) {
for(int i = L; i <= R; ++i)
k += (A[i] < v);
} else {
for(int i = L; i < (lb+1)*SIZE; ++i)
if(A[i] < v) ++k;
for(int i = rb*SIZE; i <= R; ++i)
if(A[i] < v) ++k;
for(int i = lb+1; 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 = 0,*B = &block[p/SIZE][0];
A[p] = x;
while(B[pos] < old) ++pos;
B[pos] = x;
while(pos < SIZE-1 && B[pos] > B[pos + 1]) {
swap(B[pos],B[pos+1]);
++pos;
}
while(pos > 0 && B[pos] < B[pos - 1]) {
swap(B[pos],B[pos-1]);
--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 + 1));
}
for(int i = 0; i < n; ++i)
printf("%d\n",A[i]);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: