Chang'an(Black Box-线段树)

线段树

#include <bits/stdc++.h>

using namespace std;

struct rect{
	int l, r, c, x;
};
struct rec{
	int x, p, ans, rank;
};
rect tree[800004];
rec a[200004], get[20000];
bool cmpx(const rec &a, const rec &b){
	return a.x<b.x;
}
bool cmpp(const rec &a, const rec &b){
	return a.p<b.p;
}
void build(int i, int l, int r){
	tree[i].l = l;
	tree[i].r = r;
	tree[i].c = 0;
	if (l!=r) {
		build(2*i, l, (l+r)/2);
		build(2*i+1, (l+r)/2+1, r);
	} else{
		tree[i].x = a[r].x;
	}
}
void change(int i, int p){
	if (tree[i].l==tree[i].r){
		tree[i].c=1;
		return;
	}
	if (p<=tree[2*i].r) {change(2*i, p);} else {change(2*i+1, p);	}
	tree[i].c= tree[2*i].c + tree[2*i+1].c;
}
int find(int i, int x){
	if (tree[i].l==tree[i].r){
		return tree[i].x;
	}
	if (x<=tree[2*i].c) {return find(2*i, x);} else {return find(2*i+1, x-tree[2*i].c);}
}
int main(){
//	freopen("d.in", "r", stdin);
	int T, l, n, m;
	cin>>T;
	for(int I=1; I<=T; I++){
		cout<<"Case #"<<I<<":"<<endl;
		cin>>n>>m;
		for(int i=1; i<=n; i++){
			cin>>a[i].x;
			a[i].p=i;
		}
		for(int i=1; i<=m; i++){
			cin>>get[i].x;
			get[i].p=i;
		}
		sort(a+1, a+1+n, cmpx);
		for(int i=1; i<=n; i++){
			a[i].ans = i;
		}
		build(1, 1, n);
		sort(a+1, a+1+n, cmpp);
		sort(get+1, get+1+m, cmpx);
		l = 1;
		get[m+1].x=-1;
		for(int i=1; i<=n; i++){
//			cout<<a[i].ans<<endl;
			change(1, a[i].ans);
			while(i==get[l].x) {
				get[l].ans=find(1, l);
				l++;
			}
		}
		sort(get+1, get+1+m, cmpp);
		for(int i=1; i<=m; i++){
			cout<<get[i].ans<<endl;
		}
	}
	return 0;
}

Chang'an(Black Box-线段树)

时间: 2024-10-13 11:34:55

Chang'an(Black Box-线段树)的相关文章

ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树上分块+dfs序+线段树)

题意 链接:https://nanti.jisuanke.com/t/A1998 给出一个有根树(根是1),有n个结点.初始的时候每个结点的值都是0.下面有q个操作,操作有两种,操作1.将深度为L(根节点深度为0)的点的值全部增加X.操作2.查询以x为根的子树的结点值得和.其中N,Q<=1e5. 思路 因为这题是对某一深度的所有点加x,所以不是树链剖分. 我们可以先预处理一下dfs序,顺带把d[u]:u的深度.dd[x]:深度为x的点集求出来. 考虑分块,对某一深度分两种情况:1.这一深度的点的

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

hdu 4893 (多校1007)Wow! Such Sequence!(线段树&amp;二分&amp;思维)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 352    Accepted Submission(s): 104 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

hdu 3973 字符串hash+线段树

http://acm.hdu.edu.cn/showproblem.php?pid=3973 Problem Description You are given some words {Wi}. Then our stupid AC will give you a very long string S. AC is stupid and always wants to know whether one substring from S exists in the given words {Wi}

HDU 4893 Wow! Such Sequence! (线段树)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 838    Accepted Submission(s): 245 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

Codeforces 834D The Bakery - 动态规划 - 线段树

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery. Soon the expenses started to overcome the income, so Slastyona decid

HDU 4893 线段树的 点更新 区间求和

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2067    Accepted Submission(s): 619 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

HDU4893:Wow! Such Sequence!(线段树lazy)

Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's a mysterious blackbox. After some research, Doge found that the box is maintaining a sequence an of n nu