WA0 求调
查看原帖
WA0 求调
749301
AmiyaCast楼主2024/7/19 20:18

我把二位数点打包起来了,做之前的题使用都没问题

#include<bits/stdc++.h>
#define ll long long
#define pii make_pair
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=b;i>=a;--i)
using namespace std;
inline ll read(){
    ll x=0,f=1;
    char c=getchar();
    while (c<'0' || c>'9'){
        if (c=='-')  f=-1;
        c=getchar();
    }
    while (c>='0' && c<='9'){
        x=x*10+c-'0';
         c=getchar();
    }
    return x*f;
}
inline void print(ll x){
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) print(x / 10);
	putchar(x % 10 + '0');
	return ;
}
inline void pprint(ll x){print(x); puts("");}
const int N = 5e5 + 7;
const int M1 = 1.5e6 + 7;//点和讯问中出现的单个坐标的个数,可以理解成点+2*询问数目
const int M2 = 2.5e6 + 7;//点的数数目+询问*4
struct SN {//完成二维数点 
	int c[M1];  // 树状数组
	int lowbit(int x) { return x & (-x);}
	void add(int x, int k) { while (x <= M1) { c[x] = c[x] + k;	x = x + lowbit(x); } }
	int getsum(int x) { int ret = 0; while (x > 0) { ret = ret + c[x]; x = x - lowbit(x); }	return ret;}
	struct Q {
		int a, b, c, d;
	};//a b左下角 c d 右上角 a-c中有多少在l r之间的
	int n = 0, m = 0;
	int x[N], y[N], ans[N];
	Q q[N];	
	int ax[M1], ay[M1], tx, ty, tot = 0;
	struct ope {
		int type, x, y, id;
		inline ope(int type = 0, int x = 0, int y = 0, int id = 0) {
			this->type = type, this->x = x, this->y = y, this->id = id;
		}
		inline bool operator<(const ope& rhs) const {
			if (x == rhs.x) return type < rhs.type;
			return x < rhs.x;
		}
	} op[M2];
	void addn(int _x, int _y){ x[++n] = _x; y[n] = _y; }
	void addm(int l, int r, int down, int up){q[++m].a = l; q[m].c = r; q[m].b = down; q[m].d = up;}
	void easy_work() {//easy_work传入的时候,纵坐标都要+1 
		for(int i = 1; i <= m; ++i) c[i] = ans[i] = 0; 
		for (int i = 1; i <= n; i++) {
			op[++tot] = ope(0, x[i], y[i], i);  // 加点操作
		}
		for (int i = 1; i <= m; i++) {
			op[++tot] = ope(1, q[i].c, q[i].d, i);  // 将查询差分
			op[++tot] = ope(1, q[i].a - 1, q[i].b - 1, i);
			op[++tot] = ope(2, q[i].a - 1, q[i].d, i);
			op[++tot] = ope(2, q[i].c, q[i].b - 1, i);
		}
		sort(op + 1, op + tot + 1);  // 将操作按横坐标排序,且优先执行加点操作
		for (int i = 1; i <= tot; i++) {
			if (op[i].type == 0)
				add(op[i].y, 1);
			else if (op[i].type == 1)
				ans[op[i].id] += getsum(op[i].y);
			else
				ans[op[i].id] -= getsum(op[i].y);
		}
	}	
	void work() {
		for(int i = 1; i <= m; ++i) c[i] = ans[i] = 0; 
		for (int i = 1; i <= n; i++)
			ax[++tx] = x[i], ay[++ty] = y[i];
		for (int i = 1; i <= m; i++) {
			ax[++tx] = q[i].a, ay[++ty] = q[i].b, ax[++tx] = q[i].c, ay[++ty] = q[i].d;
		}
		sort(ax + 1, ax + tx + 1), std::sort(ay + 1, ay + ty + 1);
		tx = std::unique(ax + 1, ax + tx + 1) - ax - 1, ty = std::unique(ay + 1, ay + ty + 1) - ay - 1;
		for (int i = 1; i <= n; i++) {
			x[i] = std::lower_bound(ax + 1, ax + tx + 1, x[i]) - ax;
			y[i] = std::lower_bound(ay + 1, ay + ty + 1, y[i]) - ay;
			op[++tot] = ope(0, x[i], y[i], i);  // 加点操作
		}
		for (int i = 1; i <= m; i++) {
			q[i].a = lower_bound(ax + 1, ax + tx + 1, q[i].a) - ax;
			q[i].b = lower_bound(ay + 1, ay + ty + 1, q[i].b) - ay;
			q[i].c = lower_bound(ax + 1, ax + tx + 1, q[i].c) - ax;
			q[i].d = lower_bound(ay + 1, ay + ty + 1, q[i].d) - ay;
			op[++tot] = ope(1, q[i].c, q[i].d, i);  // 将查询差分
			op[++tot] = ope(1, q[i].a - 1, q[i].b - 1, i);
			op[++tot] = ope(2, q[i].a - 1, q[i].d, i);
			op[++tot] = ope(2, q[i].c, q[i].b - 1, i);
		}
		sort(op + 1, op + tot + 1);  // 将操作按横坐标排序,且优先执行加点操作
		for (int i = 1; i <= tot; i++) {
			if (op[i].type == 0)
				add(op[i].y, 1);
			else if (op[i].type == 1)
				ans[op[i].id] += getsum(op[i].y);
			else
				ans[op[i].id] -= getsum(op[i].y);
		}
	}
	void print(){ for(int i = 1; i <= m; ++i) cout << ans[i] << endl; }
	void clear(){ n = m = tx = ty = tot = 0;}//多组数据记得clear 
	void debug(){ puts("Debug:--"); cout << "Node: " << endl; for(int i = 1; i <= n; ++i) cout << x[i] << " " << y[i] << endl; cout << "Question: " << endl; for(int i = 1; i <= m; ++i) cout << q[i].a << " " << q[i].c << " " << q[i].b << " " << q[i].d << endl;}
}node;
ll nxt[N], to[N], head[N], cnt = 1, dfn[N], dep[N], siz[N];
void add(int x, int y){ nxt[++cnt] = head[x]; to[cnt] = y; head[x] = cnt; }
int stp = 0;
void dfs(int x, int y){
	siz[x] = 1;
	dep[x] = y;
	dfn[x] = ++stp;
	for(int i = head[x]; i; i = nxt[i]){
		if(dep[to[i]]) continue;
		dfs(to[i], y + 1);
		siz[x] += siz[to[i]];
	}
	return ;
}
int main() {
	int n = read(), m = read();
	for(int i = 1; i < n; ++i){
		int x = read(), y = read();
		add(x, y); add(y, x);
	}
	dfs(1, 1);
	for(int i = 1; i <= n; ++i){
		node.addn(dfn[i], dep[i]);
	}
	int lst = 0;
	for(int i = 1; i <= m; ++i){
		int op = read(), x = read();
		if(op == 1){
			lst = x;
		}else{
			node.addm(dfn[x], dfn[x] + siz[x] - 1, lst, n);
		}
	}
	node.easy_work();
	node.print();
	return 0;
}


2024/7/19 20:18
加载中...