Ultraviolet A 12003 array Transformer (block array)

Source: Internet
Author: User

Page 1 of the White Paper.


Always messing around on the original array. In fact, we need to use another array record block.

The original array cannot be changed.

Pay attention to the relationship between the original array and the block array, and carefully process the boundary. It is not difficult.


#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#define maxn 300005#define SIZE 600using namespace std;int a[maxn];int block[maxn];int b[maxn];void work(int p,int x){    int old=b[p];    b[p]=x;    int BLOCK=p/SIZE;    int pos=p;    for(int i=BLOCK*SIZE;i<BLOCK*SIZE+SIZE;i++)    {        if(a[i]==old)        {            pos=i;            a[i]=x;            break;        }    }    while(block[pos+1]==block[p] && a[pos+1]<a[pos])    {        swap(a[pos+1],a[pos]);        pos++;    }    while(pos-1>=0 && block[pos-1]==block[p] && a[pos-1]>a[pos])    {        swap(a[pos-1],a[pos]);        pos--;    }}int main(){    int n,m,u;    while(scanf("%d%d%d",&n,&m,&u)!=EOF)    {        memset(a,0x3f,sizeof a);        memset(block,0x3f,sizeof block);        for(int i=0;i<n;i++)        {            scanf("%d",&a[i]);            b[i]=a[i];            block[i]=i/SIZE;        }        for(int i=0;i<(n-1)/SIZE;i++)        {            sort(a+i*SIZE,a+(i+1)*SIZE);        }        sort(a+((n-1)/SIZE*SIZE),a+n);        while(m--)        {            int l,r,v,p;            scanf("%d%d%d%d",&l,&r,&v,&p);            l--,r--,p--;            int ans=0;            if(block[l]==block[r])            {                for(int i=l;i<=r;i++)                if(b[i]<v)ans++;            }            else            {                for(int i=l;block[i]==block[l];i++)                {                    if(b[i]<v)ans++;                }                for(int i=r;block[i]==block[r];i--)                if(b[i]<v)ans++;                for(int i=SIZE*(l/SIZE+1);i<r/SIZE*SIZE;i+=SIZE)                {                    ans+=lower_bound(a+i,a+i+SIZE,v)-(a+i);                }            }            work(p,(long long)u*ans/(r-l+1));        }        for(int i=0;i<n;i++)        printf("%d\n",b[i]);    }    return 0;}/*10 3 510 9 8 7 6 5 4 3 2 12 5 9 42 9 6 54 10 5 6*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.