ZOJ--3612 -- median [Line Segment tree + discretization]

Source: Internet
Author: User

Link:Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4736

Question:There are a maximum of 10000 operations. add or remove elements in a column with an initial null value and keep the sequence in order. After each operation, if the number of columns is odd, the median value is output, if the value is an even number, the average values of the two values are output.


Ideas:At the beginning, I wrote a Multiset simulation. After Reading Wu Qi TLE, I estimated that he was also writing a Multiset simulation. I gave up this idea. However, I finally proved that this problem can be achieved by the wit of Multiset. The line segment tree and tree array are all taken into consideration. Finally, the line segment tree is used. Although the number range is within int, the operation is only 10000 times at most, so discretization is required. First perform offline, then discretization, and then build a new structure for operations. Each node maintains a number of times that the subscript value corresponding to the new array appears in the series after discretization.

When querying, enter the number of The number of columns to be queried, and then recursively determine the number of left and right Subtrees from the root node of the line segment. If the number of queries is less than or equal to the number of left Subtrees, query in the left subtree. Otherwise, the number to be queried minus the number of digits in the left subtree and query in the right subtree. This recursion is performed until the leaf node returns the node value, at this time, the returned value is the lower mark value of the discretization array, and then the answer is output.

Details:1. The output is quite disgusting. For the double type, it is possible to directly use cout for importing 0 after no output. However, if the output is too large, it may be TLE, So I converted it into a string for output.

2. Although the data is within the int range, the sum of the average value may overflow, so long is used for storage, because this wa has been used for a long time.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 10100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[MAXN<<2],ans[MAXN],fuck[MAXN],cha[MAXN];int n,flag;struct node{    int num;    char op;    int id;}a[10100];void pushup(int rt){    sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void build(int l,int r,int rt){    sum[rt] = 0;    if(l==r)    return ;    int m = (l+r)>>1;    build(lson);    build(rson);}void update(int c,int x,int l,int r,int rt){    if(l==r){        if(sum[rt]==0&&x==-1){            flag = 1;            return ;        }        sum[rt]+=x;        return ;    }    int m = (l+r)>>1;    if(c<=m)    update(c,x,lson);    else    update(c,x,rson);    pushup(rt);}int query(int c,int l,int r,int rt){    if(l==r){        return l;    }    //cout<<sum[rt<<1]<<" "<<l<<endl;    int m = (l+r)>>1;    int ret;    if(sum[rt<<1]>=c){        ret = query(c,lson);    }    else{        ret = query(c-sum[rt<<1],rson);    }    return ret;}void output(double x){    int i;    char ss[200];    sprintf(ss,"%lf",x);    int l = strlen(ss);    for(i=0;i<l;i++){        if(ss[i]=='.')  break;    }    if(i==l){        printf("%lf\n",x);        return ;    }    while(ss[l-1]=='0'){        l--;    }    if(ss[l-1]=='.')    l--;    for(i=0;i<l;i++){        printf("%c",ss[i]);    }    printf("\n");}map<int,int>mp;int main(){    int i,j,q,t,x;    char str[20];    scanf("%d",&t);    while(t--){        mp.clear();        n = 0;        scanf("%d",&q);        for(i=1;i<=q;i++){            scanf("%s%d",str,&a[i].num);            a[i].op = str[0];            a[i].id = i;            fuck[i] = a[i].num;        }        sort(fuck+1,fuck+1+q);        int p = unique(fuck+1,fuck+1+q)-fuck;        for(i=1;i<p;i++){            ans[i] = fuck[i];            mp[fuck[i]] = i;        }        j = p;        build(1,j,1);        for(i=1;i<=q;i++){            if(a[i].op=='a'){                n++;                update(mp[a[i].num],1,1,j,1);                if(n%2){                    printf("%d\n",ans[query(n/2+1,1,j,1)]);                }                else{                    ll temp = 0;                    temp += ans[query(n/2,1,j,1)];                    temp += ans[query(n/2+1,1,j,1)];                    double t2 = temp / 2.0;                    //printf("%lf\n",t2);                    output(t2);                }            }            else{                if(n==0){                    puts("Wrong!");                    continue;                }                flag = 0;                update(mp[a[i].num],-1,1,j,1);                if(flag==1){                    puts("Wrong!");                    continue;                }                n--;                if(n==0){                    puts("Empty!");                    continue;                }                if(n%2){                    printf("%d\n",ans[query(n/2+1,1,j,1)]);                }                else{                    ll temp = 0;                    temp += ans[query(n/2,1,j,1)];                    temp += ans[query(n/2+1,1,j,1)];                    double t2 = temp / 2.0;                    //printf("%lf\n",t2);                    output(t2);                }            }        }    }    return 0;}/*10a 1a 1000000000a 1000000000a -1000000000a 500a 60000r 1000000000r 1r 500a 3000*/


ZOJ--3612 -- median [Line Segment tree + discretization]

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.