本文主要是介绍LG P4148 简单题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
\(\text{Code}\)
#include <cstdio>
#include <iostream>
#include <algorithm>
#define re register
using namespace std;
const int N = 2e5 + 5;
const double alpha = 0.725;
int n, cur, x0, y0, x1, y1, rt;
int L1[N], L2[N], R1[N], R2[N], siz[N], sum[N], ls[N], rs[N], ds[N], gs[N];
struct node{int x, y, v;}s[N];
inline bool cmpx(int a, int b){return s[a].x < s[b].x;}
inline bool cmpy(int a, int b){return s[a].y < s[b].y;}
inline void read(int &x)
{
x = 0; char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
}
inline void update(int p, int o)
{
L1[p] = min(L1[p], L1[o]), R1[p] = max(R1[p], R1[o]);
L2[p] = min(L2[p], L2[o]), R2[p] = max(R2[p], R2[o]);
}
inline void maintain(int p)
{
siz[p] = siz[ls[p]] + siz[rs[p]] + 1, sum[p] = sum[ls[p]] + sum[rs[p]] + s[p].v;
L1[p] = R1[p] = s[p].x, L2[p] = R2[p] = s[p].y;
if (ls[p]) update(p, ls[p]); if (rs[p]) update(p, rs[p]);
}
int build(int l, int r)
{
if (l > r) return 0;
int mid = (l + r) >> 1;
double av1 = 0, av2 = 0, s1 = 0, s2 = 0;
for(re int i = l; i <= r; i++) av1 += s[gs[i]].x, av2 += s[gs[i]].y;
av1 /= (r - l + 1), av2 /= (r - l + 1);
for(re int i = l; i <= r; i++)
s1 += (av1 - s[gs[i]].x) * (av1 - s[gs[i]].x), s2 += (av2 - s[gs[i]].y) * (av2 - s[gs[i]].y);
if (s1 > s2) nth_element(gs + l, gs + mid, gs + r + 1, cmpx), ds[gs[mid]] = 1;
else nth_element(gs + l, gs + mid, gs + r + 1, cmpy), ds[gs[mid]] = 2;
ls[gs[mid]] = build(l, mid - 1), rs[gs[mid]] = build(mid + 1, r), maintain(gs[mid]);
return gs[mid];
}
void getseq(int p){if (!p) return; getseq(ls[p]), gs[++gs[0]] = p, getseq(rs[p]);}
inline void rebuild(int &p){gs[0] = 0, getseq(p), p = build(1, gs[0]);}
inline int bad(int p){return max(siz[ls[p]], siz[rs[p]]) >= siz[p] * alpha;}
void insert(int &p)
{
if (!p) return void(maintain(p = cur));
if (ds[p] == 1)
{
if (s[cur].x <= s[p].x) insert(ls[p]);
else insert(rs[p]);
}
else{
if (s[cur].y <= s[p].y) insert(ls[p]);
else insert(rs[p]);
}
maintain(p);
if (bad(p)) rebuild(p);
}
int query(int p)
{
if (!p || L1[p] > x1 || R1[p] < x0 || L2[p] > y1 || R2[p] < y0) return 0;
if (x0 <= L1[p] && R1[p] <= x1 && y0 <= L2[p] && R2[p] <= y1) return sum[p];
int res = 0;
if (s[p].x >= x0 && s[p].x <= x1 && s[p].y >= y0 && s[p].y <= y1) res = s[p].v;
return res + query(ls[p]) + query(rs[p]);
}
int main()
{
read(n);
for(int opt = -1, last = 0; opt != 3; )
{
read(opt);
if (opt == 1)
++cur, read(s[cur].x), read(s[cur].y), read(s[cur].v),
s[cur].x ^= last, s[cur].y ^= last, s[cur].v ^= last, insert(rt);
else if (opt == 2) read(x0), read(y0), read(x1), read(y1),
x0 ^= last, y0 ^= last, x1 ^= last, y1 ^= last, printf("%d\n", last = query(rt));
}
}
这篇关于LG P4148 简单题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!