Bzoj 3110[zjoi2013]k Large number query whole binary + tree-like array interval modify __AC Road

Source: Internet
Author: User

Well, can only say that the whole of the second is a very magical thing, since it is a second division although added a whole sound become immediately tall up, but still need to start from the most basic two points of thinking to understand. First of all, if you have only one query operation, we can easily think of the two-point writing, the Second division of power and then add cheak operation to determine how many values than the current large, and then constantly narrowing the interval until the last l==r. Similarly, in the whole of the two we also use similar ideas, here a simple simulation of the operation process, hoping to help the group to understand;

First, the structure of a p is used to record all operations, including the type range and K. Then because the title is the query K-large, the number of inserts are also likely to be negative, so each insert operation, inserted K into N+1-k, and then the query K large into the K small can be cleverly avoided negative situation.

Offline processing starts with two points, also two decentralization values, and then for each weighted interval there will be only the answer to this part of the operation to contribute to the action (be sure to understand here, here is the key to the algorithm). For example, now I have an interval that is l->mid and now there is an insert operation that inserts a value greater than mid, which naturally does not contribute to the L->mid range, so it is dropped to the Mid+1->r interval (similar to a sort). And if for a query operation, the number of the query 5th small, and l->mid between the value of 7 then this answer must not be in this interval to the same throw to the right.

So we need to have a quick query a range of how many numbers of data structure, that is, interval modification interval query can use line tree, but I use the tree array so it may not be easy to understand, you just as interval modify interval query is good


#include <cstdio> #include <cstring> #include <iostream> #define MAXN 100020 #define LL long-long using NA
Mespace std;
int N,M,ANS[MAXN];
LL C1[MAXN],C2[MAXN];
	struct que{int id,pos,l,r;
LL K;
}P[MAXN],P1[MAXN],P2[MAXN];
		void Update (int x,ll add) {for (int i=x;i<=n;i+=i& (i)) {c1[i]+=add;
	C2[i]+=x*add;
	} ll query (int x) {ll ans=0;
	for (int i=x;i>0;i-=i& (i)) {ans+= (x+1) *c1[i]-c2[i];
return ans;
	} void Solve (int s,int t,ll l,ll r) {if (s>t) return;
			if (l==r) {for (int i=s;i<=t;i++) if (p[i].pos==2) {ans[p[i].id]=l;
	} return;
	}ll mid=l+r>>1;//Two-point answer int s1=0,s2=0; for (int i=s;i<=t;i++) {//enum operation the current operation is still in order the IF (p[i].pos==1) {//Insert if (P[I].K&LT;=MID) {//Add the number of less than two of the weight value update p
				[i].l,1);
				Update (P[I].R+1,-1);
			p1[++s1]=p[i];//into the first queue}else {p2[++s2]=p[i];
			}else{//Query L to r total number of how many, as mentioned above, these numbers must be less than equal to the mid LL Tmp=query (P[I].R)-query (P[I].L-1);
			if (TMP&GT;=P[I].K) p1[++s1]=p[i]; else p[i].k-=tmp,p2[++s2]=p[i];//minus the smaller one than he has. Then put it in the second queue}//Empty the tree array for (int i=s;i<=t;i++) {if (p[i].pos==1&&p[i).
			K<=mid) {update (P[I].L,-1);
		Update (p[i].r+1,1);
	} bool F1=false,f2=false;
	for (int i=1;i<=s1;i++) p[i+s-1]=p1[i],f1=true;
	for (int i=1;i<=s2;i++) p[i+s+s1-1]=p2[i],f2=true;
	if (F1) solve (s,s+s1-1,l,mid);
if (F2) solve (s+s1,t,mid+1,r);
	int main () {scanf ("%d%d", &n,&m);
	int cnt=0;
		for (int i=1;i<=m;i++) {scanf ("%d%d%d%lld", &AMP;P[I].POS,&AMP;P[I].L,&AMP;P[I].R,&AMP;P[I].K);
		if (p[i].pos==1) {p[i].k=n+1-p[i].k;
	}else p[i].id=++cnt;
	} Solve (1,m,0, (LL) n*2+2);
	for (int i=1;i<=cnt;i++) printf ("%d\n", (LL) n+1-ans[i]);
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.