2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学) B - Color it HDU - 6183 线段树

Do you like painting? Little D doesn’t like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The specific format of these operations is as follows.

0 : clear all the points.

1 x y c : add a point which color is c at point (x,y).

2 x y1 y2 : count how many different colors in the square (1,y1) and (x,y2). That is to say, if there is a point (a,b) colored c, that 1≤a≤x and y1≤b≤y2, then the color c should be counted.

3 : exit.
Input
The input contains many lines.

Each line contains a operation. It may be ‘0’, ‘1 x y c’ ( 1≤x,y≤106,0≤c≤50 ), ‘2 x y1 y2’ (1≤x,y1,y2≤106 ) or ‘3’.

x,y,c,y1,y2 are all integers.

Assume the last operation is 3 and it appears only once.

There are at most 150000 continuous operations of operation 1 and operation 2.

There are at most 10 operation 0.

Output
For each operation 2, output an integer means the answer .
Sample Input
0
1 1000000 1000000 50
1 1000000 999999 0
1 1000000 999999 0
1 1000000 1000000 49
2 1000000 1000000 1000000
2 1000000 1 1000000
0
1 1 1 1
2 1 1 2
1 1 2 2
2 1 1 2
1 2 2 2
2 1 1 2
1 2 1 3
2 2 1 2
2 10 1 2
2 10 2 2
0
1 1 1 1
2 1 1 1
1 1 2 1
2 1 1 2
1 2 2 1
2 1 1 2
1 2 1 1
2 2 1 2
2 10 1 2
2 10 2 2
3
Sample Output
2
3
1
2
2
3
3
1
1
1
1
1
1
1

将每一种颜色做成一颗线段树,然后只要保存离y轴的最近距离即可,因为每次询问都是 1~x 的范围(横坐标),每次查询看每种颜色与y轴的最近距离是不是比x小,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 2000005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
#define lson 2*x
#define rson 2*x+1

long long  qupower(int a, int b) {
	long long  ans = 1;
	while (b) {
		if (b & 1)ans = ans * a;
		b >>= 1;
		a = a * a;
	}
	return ans;
}

inline int read() {
	int an = 0, x = 1; char c = getchar();
	while (c > '9' || c < '0') {
		if (c == '-') {
			x = -1; 
		}
		c = getchar();
	}
	while (c >= '0'&&c <= '9') {
		an = an * 10 + c - '0'; c = getchar();
	}
	return an * x;
}

int L[maxn], R[maxn], ans;
int root[60];
int LL, RR, xx;
int v[maxn], cnt;
int flag;

void update(int &k, int l, int r, int a, int b) {
	int m = 0;
	if (k == 0) {
		k = ++cnt;
		v[k] = b;
	}
	if (v[k] > b)v[k] = b;
	if (l == r)return;
	int mid = (l + r) >> 1;
	if (a <= mid)update(L[k], l, mid, a, b);
	else update(R[k], mid + 1, r, a, b);
}

void query(int x,int l,int r) {
	if (flag || x == 0)return;
	if (LL <= l && r <= RR) {
		if (v[x] <= xx) {
			flag = 1;
		}
		return;
	}
	int mid = (l + r) >> 1;
	if (LL <= mid)query(L[x], l, mid);
	if (mid < RR)query(R[x], mid + 1, r);
}

int main() {
	//ios::sync_with_stdio(false);
	while (1) {
		int opt;
		//cin >> opt;
		scanf("%d", &opt);
		if (opt == 3)break;
		else if (opt == 0) {
			ms(v);
			ms(root); 
			for (int i = 0; i <= cnt; i++)L[i] = R[i] = 0;
			cnt = 0;
		}
		else if (opt == 1) {
			int x, y, c;
			scanf("%d%d%d", &x, &y, &c);
			//cin >> x >> y >> c;
			update(root[c], 1, 1000000, y, x);
		}
		else {
			ans = 0;
			//cin >> xx >> LL >> RR;
			scanf("%d%d%d", &xx, &LL, &RR);
			for (int i = 0; i <= 50; i++) {
				flag = 0;
				query(root[i], 1, 1000000);
				ans += flag;
			}
			cout << ans << endl;
		}
	}
}
    原文作者:B树
    原文地址: https://blog.csdn.net/qq_40273481/article/details/81558032
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞