Bzoj 3514 codechef MARCH14 GERALD07 Enhanced Edition lct+ Chairman tree

Source: Internet
Author: User

Test Instructions:Link Method:Lct parsing:It was disgusting for a night to be the name of the disgusting thing. But I finally a drop him hot haha haha haha ha ha haha hahaha first time read the question I do not know what to say, this TM what thing. How do you ask for that? Then also by the way Baidu the next connected block =-= problem too god do not know how to do ah. Then there is a special strange ♂ NTR algorithm. Every time you add a side, if you ntr yourself, you will not have an impact on the answer. Sure it is, the equivalent of no edge. What about non-self-loops? First of all, judge whether this side plus go after the answer has any effect. If you add a loop that does not produce a ring, it is obviously making the connected block-1. If there is a ring, it is obvious that it has no effect on the answer. If a ring is produced, the NTR value of the edge is the value of the first edge on the ring. It bounces off the edge, and of course we need to bounce it off. This proves what! The side that appears after this bounced edge, and the edge that appears before it bounces off his side, is clearly unable to form a ring with the side that bounced him! That is, if the left end of the interval is larger than the bouncing edge, the appearance of bouncing off his side will definitely reduce the answer by 1. If the interval contains the side of the bounce off his side, then it is clear that the answer is not affected by bouncing off his side! His appearance is not to make the connected block-1. The brain does not fill out on the drawing is very spicy! It's not spicy, it's hard! And what did the problem turn into? All NTR values are preprocessed with LCT first. Then on the NTR array [l,r] The interval asks the number of NTR value is less than L! Chairman of the Tree naked! And then I got t. T_T summarize the experience of T. I t in two places. Just beginning space Open is enough, then find_root too violent to be stuck. And then somehow shrunk down the space? Find_root rewritten the next and got stuck. And I've been working on strange things. Later found that the right to pat why too wrong? ... the chair of the tree space Ah, bastard! NM makes me somehow shrink into a nm/2. Good question! Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 4001000#define INF 0x3f3f3f3fusing namespace STD;intN,m,k,type,size;intrev[n],ch[n][2],fa[n],root[n];intMi[n],val[n],rt[n],sum[n],lson[n],rson[n];intNtr[n];structnode{intx, y;} Edge[n];inline intRead () {intx=0;CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 ') {Ch=getchar ();} while(ch>=' 0 '&&ch<=' 9 ') {x= (x<<3) + (x<<1) +ch-' 0 '; Ch=getchar ();}returnx;}inline voidPushup (intx) {if(!x)return; Mi[x]=x;if(ch[x][0]!=0&&val[mi[ch[x][0]]]&LT;VAL[MI[X]]) mi[x]=mi[ch[x][0]];if(ch[x][1]!=0&&val[mi[ch[x][1]]]&LT;VAL[MI[X]]) mi[x]=mi[ch[x][1]];}inline voidReverseintx) {if(!x)return; Swap (ch[x][0],ch[x][1]); rev[x]^=1;}inline voidPushdown (intx) {if(!x)return;if(Rev[x]) {Reverse (ch[x][0]); Reverse (ch[x][1]); rev[x]^=1; }}inline voidDownintx) {if(!x)return;if(!rt[x]) down (fa[x]); Pushdown (x);}inline voidRotateintx) {inty=fa[x],kind=ch[y][1]==x;    Ch[y][kind]=ch[x][!kind];    Fa[ch[y][kind]]=y;    Ch[x][!kind]=y;    Fa[x]=fa[y]; Fa[y]=x;if(Rt[y]) rt[y]=0, rt[x]=1;Elsech[fa[x]][ch[fa[x]][1]==y]=x; Pushup (y);}inline voidSplay (intx) {down (x); while(!rt[x]) {intY=fa[x],z=fa[y];if(Rt[y]) rotate (x);Else if((ch[y][1]==x) = = (ch[z][1]==y)) rotate (y), rotate (x);ElseRotate (x), rotate (x); } pushup (x);}inline voidAccessintx) {inty=0; while(x) {splay (x); rt[ch[x][1]]=1, rt[y]=0; ch[x][1]=y;        Pushup (x);    Y=X,X=FA[X]; }}inline voidMoveToRoot (intx) {access (x);    Splay (x); reverse (x);}inline intFind_root (intx) {access (x), splay (x); while(ch[x][0]) x=ch[x][0];returnx;}inline voidLinkintXintY) {movetoroot (x); Fa[x]=y;}inline voidCutintXintY) {movetoroot (x);    Access (y);    Splay (y); ch[y][0]=0; fa[x]=0; rt[x]=1; Pushup (y); }inline voidInit () { for(intI=1; i<=n+m;i++) rt[i]=1, Val[i]=inf;}inline voidInsertintLintRintXint&y,intval) {y=++size; sum[y]=sum[x]+1;if(L==R)return; LSON[Y]=LSON[X],RSON[Y]=RSON[X];intMid= (l+r) >>1;if(val<=mid) Insert (l,mid,lson[x],lson[y],val);ElseInsert (mid+1, r,rson[x],rson[y],val);}inline intQueryintLintRintXintYintVal) {if(R==val)returnSUM[Y]-SUM[X];intMid= (l+r) >>1;if(Val<=mid)returnQuery (L,mid,lson[x],lson[y],val);Else returnSum[lson[y]]-sum[lson[x]]+query (mid+1, r,rson[x],rson[y],val);}intMain () {scanf("%d%d%d%d", &n,&m,&k,&type); Init (); for(intI=1; i<=m;i++) {intx, y;        Edge[i].x=read (); Edge[i].y=read (); X=EDGE[I].X,Y=EDGE[I].Y;if(x==y) {ntr[i]=i;}Else if(Find_root (x) ==find_root (y)) {movetoroot (x), Access (y), splay (y);intTmp=mi[y];            Ntr[i]=tmp-n;            Cut (edge[tmp-n].x,tmp), cut (TMP,EDGE[TMP-N].Y);            Val[n+i]=i,mi[n+i]=n+i;        Link (edge[i].x,i+n), link (i+n,edge[i].y); }Else{val[n+i]=i,mi[n+i]=n+i;        Link (edge[i].x,i+n), link (i+n,edge[i].y); }    } for(intI=1; i<=m;i++) {Insert (0, m,root[i-1],root[i],ntr[i]); }intans=0; for(intI=1; i<=k;i++) {intL,r; L=read (); R=read ();if(type) L^=ans,r^=ans; Ans=n-query (0, m,root[l-1],root[r],l-1);printf("%d\n", ans); }}

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

Bzoj 3514 codechef MARCH14 GERALD07 Enhanced Edition lct+ Chairman 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.