BZOJ3110 Zjoi2013 k Large number query tree sets tree weight line segment tree sleeve interval segment tree __bzoj3110

Source: Internet
Author: User

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


Exercises

The outer weight line segment tree, the inner interval segment tree can be solved.

Weights are 1~n, so there is no need for discretization.

I wrote a flag for permanent.


Other horse of the Divine Experience:

Born with a tree-shaped data structure without love.

The first time to write tree sets of trees, finally know what is going on.


(for the subject only)

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

And it's also a line tree,

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


And every time you insert it,

are violently putting the weight of the

All outer segment tree nodes

The corresponding intervals of these inner-layer tree segments

Weight value +1 (of course, after all, is a line of trees, certainly a variety of lazy ah what the guarantee Logn)


The Non-leaf node of the outer segment tree is not pushed from its child node at all.

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


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50500 #de
Fine M 5005000 using 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;&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&LT;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<=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);
Pushup (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;
 }



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.