p4148 简单题 kdtree

(9 mins to read)

维护矩形,支持加点,询问矩形和,强制在线

做法

不强制在线可以cdq,强制在线就只能kdtree了每个节点维护最大矩形,以及子树内所有点权值的和对于询问,如果当前节点的最大矩形在询问矩形内部,直接返回sum,如果不相交,返回0,否则继续递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define fi first
#define se second
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 5e5 + 5;
const ll LINF = 1e18 + 5;
constexpr int mod = 1e9 + 7;
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
inline char gc()
{
return (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin),p1==p2) ? EOF : *p1++);
}
template <class T>
inline void read(T& t)
{
t = 0; int f = 1;
char c = gc();
while(c>'9'||c<'0') {if(c=='-') f = -f; c = gc();}
while(c>='0'&&c<='9') t = t*10 + c - 48, c = gc();
t *= f;
}
int n;
constexpr int k = 2;
constexpr double alph = 0.75;
int rt, pool[N], top, tot, D;
struct point
{
int x[k], w;
bool operator < (point &oth) {
return x[D] < oth.x[D];
}
}a[N], cur;
struct node
{
int l, r, mx[k], mn[k], sum, sz;
point tp;
}t[N];
int newnode()
{
if(top) return pool[top--];
return ++tot;
}
void pushup(int p)
{
int l = t[p].l, r = t[p].r;
for(int i=0; i<k; i++)
{
t[p].mn[i] = t[p].mx[i] = t[p].tp.x[i];
if(l) t[p].mn[i] = min(t[p].mn[i], t[l].mn[i]), t[p].mx[i] = max(t[p].mx[i], t[l].mx[i]);
if(r) t[p].mn[i] = min(t[p].mn[i], t[r].mn[i]), t[p].mx[i] = max(t[p].mx[i], t[r].mx[i]);
}
t[p].sz = t[l].sz + t[r].sz + 1;
t[p].sum = t[l].sum + t[r].sum + t[p].tp.w;
}
int build(int l, int r, int d)
{
if(l>r) return 0;
int p = newnode(), mid = (l+r)>>1;
D = d;
nth_element(a+l, a+mid, a+r+1);
t[p].tp = a[mid];
t[p].l = build(l, mid-1, (d+1)%k);
t[p].r = build(mid+1, r, (d+1)%k);
pushup(p);
return p;
}
void rebuild(int p, int num)
{
if(t[p].l) rebuild(t[p].l, num);
a[num+t[t[p].l].sz+1] = t[p].tp, pool[++top] = p;
if(t[p].r) rebuild(t[p].r, num+t[t[p].l].sz+1);
}
void test(int &p, int d)
{
if(alph*t[p].sz<t[t[p].l].sz||alph*t[p].sz<t[t[p].r].sz)
rebuild(p, 0), p = build(1, t[p].sz, d);
}
bool in(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2)
{
return X1>=x1&&Y1>=y1&&X2<=x2&&Y2<=y2;
}
bool out(int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2)
{
return X2<x1||X1>x2||Y1>y2||Y2<y1;
}
int ask(int p, int x1, int y1, int x2, int y2)
{
if(!p) return 0;
int ans = 0;
if(in(x1, y1, x2, y2, t[p].mn[0], t[p].mn[1], t[p].mx[0], t[p].mx[1])) return t[p].sum;
if(out(x1, y1, x2, y2, t[p].mn[0], t[p].mn[1], t[p].mx[0], t[p].mx[1])) return 0;
if(in(x1, y1, x2, y2, t[p].tp.x[0], t[p].tp.x[1], t[p].tp.x[0], t[p].tp.x[1])) ans += t[p].tp.w;
ans += ask(t[p].l, x1, y1, x2, y2) + ask(t[p].r, x1, y1, x2, y2);
return ans;
}
void insert(int &p, int d)
{
if(!p)
{
p = newnode();
t[p].tp = cur;
t[p].l = t[p].r = 0;
pushup(p);
return;
}
if(cur.x[d]>t[p].tp.x[d]) insert(t[p].r, (d+1)%k);
else insert(t[p].l, (d+1)%k);
pushup(p); test(p, d);
}
int main()
{
read(n);
int lstans = 0;
while(1)
{
int op;
read(op);
if(op==1)
{
read(cur.x[0]), read(cur.x[1]), read(cur.w);
cur.x[0] ^= lstans, cur.x[1] ^= lstans, cur.w ^= lstans;
insert(rt, 0);
}
else if(op==2)
{
int a, b, c, d;
read(a), read(b), read(c), read(d);
a ^= lstans, b ^= lstans, c ^= lstans, d ^= lstans;
printf("%d\n", lstans=ask(rt, a, b, c, d));
}
else break;
}
return 0;
}