Bzoj 3110 zjoi 2013 K large number query tree cover (weight line segment tree cover interval line segment tree)

Source: Internet
Author: User

There are some locations where several numbers can be placed. There are two types of operations.

1. Add a number X to R in the range L.

2. Find the k-th digit from L to R.


Train of Thought: This question is a tree, the key is how to set, how to write. (I won't do that either ..) The easiest way to think of it is to set an weighted line segment tree in the interval line segment tree, but the tags on the interval line segment tree become very complex. So we use the weight line segment tree to set the interval line segment tree. In this way, the modification operation becomes a single point Modification on the outer line segment tree, and the outer line segment tree does not need to maintain the flag. It is much easier to maintain tags on the interval line tree. For specific implementation, see the code.


Code:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 50010#define CNT (r - l + 1)using namespace std;int total,asks;struct ValSegTree{ValSegTree *son[2];int cnt,c;ValSegTree() {son[0] = son[1] = NULL;cnt = c = 0;}void PushDown(int k) {if(son[0] == NULL)son[0] = new ValSegTree();if(son[1] == NULL)son[1] = new ValSegTree();if(c) {son[0]->cnt += c * (k - (k >> 1));son[0]->c += c;son[1]->cnt += c * (k >> 1);son[1]->c += c;c = 0;}}void Modify(int l,int r,int x,int y) {if(l == x && r == y) {cnt += CNT;c++;return ;}PushDown(CNT);int mid = (l + r) >> 1;if(y <= mid)son[0]->Modify(l,mid,x,y);else if(x > mid)son[1]->Modify(mid + 1,r,x,y);else {son[0]->Modify(l,mid,x,mid);son[1]->Modify(mid + 1,r,mid + 1,y);}cnt = son[0]->cnt + son[1]->cnt;}int Ask(int l,int r,int x,int y) {if(!cnt)return 0;if(l == x && r == y)return cnt;PushDown(CNT);int mid = (l + r) >> 1;if(y <= mid)return son[0]->Ask(l,mid,x,y);if(x > mid)return son[1]->Ask(mid + 1,r,x,y);int left = son[0]->Ask(l,mid,x,mid);int right = son[1]->Ask(mid + 1,r,mid + 1,y);return left + right;}};struct IntSegTree{IntSegTree *son[2];ValSegTree *root;IntSegTree() {son[0] = son[1] = NULL;root = new ValSegTree();}void Modify(int l,int r,int _l,int _r,int x) {root->Modify(1,total,_l,_r);if(l == r)return ;int mid = (l + r) >> 1;if(son[0] == NULL)son[0] = new IntSegTree();if(son[1] == NULL)son[1] = new IntSegTree();if(x <= mid)son[0]->Modify(l,mid,_l,_r,x);elseson[1]->Modify(mid + 1,r,_l,_r,x);}int Ask(int l,int r,int _l,int _r,int k) {if(l == r)return l;int mid = (l + r) >> 1;if(son[0] == NULL)son[0] = new IntSegTree();if(son[1] == NULL)son[1] = new IntSegTree();int temp = son[1]->root->Ask(1,total,_l,_r);if(k > temp)return son[0]->Ask(l,mid,_l,_r,k - temp);elsereturn son[1]->Ask(mid + 1,r,_l,_r,k);}}root;int main(){cin >> total >> asks;for(int flag,x,y,z,i = 1;i <= asks; ++i) {scanf("%d%d%d%d",&flag,&x,&y,&z);if(flag == 1)root.Modify(1,total,x,y,z);elseprintf("%d\n",root.Ask(1,total,x,y,z));}return 0;}


Bzoj 3110 zjoi 2013 K large number query tree cover (weight line segment tree cover interval line 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.