"BZOJ3110" "Zjoi2013" K large number query tree set tree weight segment tree nesting interval segment tree

Source: Internet
Author: User

#include <stdio.h>int main () {puts ("reprint please specify source thank you");p UTS ("http://blog.csdn.net/vmurder/article/details/43020009 ");}


Exercises

The outer weight segment tree and the inner interval segment tree are solvable.

Weights are 1~n, so they don't have to be discretized.

I wrote the tag permanent.


Other Gods and Horses:

Born with no love for tree-shaped data structures.

The first time to write a tree set tree, finally know what is going on.


(only for the subject)

Is that each point in the outer layer represents a weighted value,

And it's also a line tree,

The segment tree records the occurrence interval, the number of times and so on.


And then every time I insert it,

are violent in putting the value of that right

All outer segment tree nodes

The corresponding intervals of these inner segment trees

Weight +1 (of course, after all, is the line tree, it must be a variety of lazy ah what guarantee logn)


The non-leaf nodes of the outer segment tree are not pushed from its child nodes at all.

Anyway, he's not really magical, it's a very violent thing.


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50500# Define M 5005000using namespace std;int root[n<<2],sum[m],son[m][2],lazy[m],cnt;int n,m;inline int query (int note , int l,int r,int l,int R) {if (!note) return 0;if (l==l&&r==r) return Sum[note];int mid=l+r>>1,ans=lazy[note ]* (r-l+1); if (R<=mid) return query (SON[NOTE][0],L,MID,L,R) +ans;else if (l>mid) return query (Son[note][1],mid+1, R,L,R) +ans;else return query (son[note][0],l,mid,l,mid) +query (son[note][1],mid+1,r,mid+1,r) +ans;} inline int QUERY (int l,int r,int k) {int ans=0,l=1,r=n,mid,temp,note=1;do{mid=l+r>>1,temp=query (root[note< &LT;1|1],1,N,L,R); if (temp>=k) L=mid+1,note=note<<1|1;else r=mid,note<<=1,k-=temp;} while (l<r); return L;} inline void pushup (int x,int l,int r) {sum[x]=sum[son[x][0]]+sum[son[x][1]]+lazy[x]* (r-l+1);} inline void Add (int¬e,int l,int r,int l,int R) {if (!note) note=++cnt;if (l==l&&r==r) {Sum[note]+=r-l+1;lazy[noTe]++;return;} int Mid=l+r>>1;if (R&LT;=MID) Add (son[note][0],l,mid,l,r); else if (l>mid) Add (son[note][1],mid+1,r,l,r); else Add (Son[note][0],l,mid,l,mid), add (son[note][1],mid+1,r,mid+1,r);p ushup (note,l,r);} inline void ADD (int l,int r,int x) {int L=1,r=n,note=1;while (l<r) {int mid=l+r>>1;add (ROOT[NOTE],1,N,L,R); if (x <=mid) R=mid,note<<=1;else l=mid+1,note=note<<1|1;} Add (root[note],1,n,l,r);} int main () {freopen ("test.in", "R", stdin), int opt,l,r,k;scanf ("%d%d", &n,&m), while (m--) {scanf ("%d%d%d%d", &opt,&l,&r,&k), if (opt==1) ADD (l,r,k), Else printf ("%d\n", QUERY (L,r,k));} return 0;}



"BZOJ3110" "Zjoi2013" K large number query tree set tree weight segment tree nesting interval segment 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.