Bzoj 3524 [poi2014]couriers can persist line tree

Source: Internet
Author: User

Test Instructions:Link Method:The line tree can be persisted. parsing:Persistent data structure Good God, it feels so mysterious. The purpose of the first achievement is to establish a tree of weights, maintaining the number of certain weights in the l,r. Then, for 1~n each node builds a tree, and is based on the previous tree. Then for each new value we only need to update once, and connect the original tree? God Ben not to say that this structure is a bunch of line-up trees and even came out. What about the time of the query? There are some minor changes that are said to be based on two basic queries. When God Ben invented this data structure, it was discovered that all line segments in this data structure can be subtracted from this nature, so we can split the answer on the left or the right, recursion to go down the final L is the target value. Does it take time to feel like a real understanding? I think I'm just going to get a sense of it. Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 500010using namespace STD;structnode{intLson,rson,sum;} seg[n* -];intSizeintRoot[n];voidUpdateintPint&q,intLintRintV) {seg[++size]=seg[p],q=size; seg[q].sum++;if(L==R)return;intMid= (l+r) >>1;if(v<=mid) update (SEG[P].LSON,SEG[Q].LSON,L,MID,V);ElseUpdate (seg[p].rson,seg[q].rson,mid+1, r,v);}intQueintLintRintLintRintK) {if(L==R)returnLintMid= (l+r) >>1;if(seg[seg[r].lson].sum-seg[seg[l].lson].sum>k) {returnQue (seg[l].lson,seg[r].lson,l,mid,k); }if(seg[seg[r].rson].sum-seg[seg[l].rson].sum>k) {returnQue (seg[l].rson,seg[r].rson,mid+1, r,k); }return 0;}intN,m;intMain () {scanf("%d%d", &n,&m); for(intI=1; i<=n;i++) {intXscanf("%d", &x); Update (root[i-1],root[i],1, n,x); } for(intI=1; i<=m;i++) {intL,r;scanf("%d%d", &l,&r);printf("%d\n", Que (root[l-1],root[r],1, N, (r-l+1) >>1)); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 3524 [poi2014]couriers can persist line tree

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.