CDQ分治
把询问离线
对于第二个询问(x1,y1)(x2,y2)(x1,y1)(x2,y2),把它拆(1,1)(x2,y2)−(1,1)(x1−1,y2)−(1,1)(x2,y1−1)+(1,1)(x1−1,y1−1)(1,1)(x2,y2)−(1,1)(x1−1,y2)−(1,1)(x2,y1−1)+(1,1)(x1−1,y1−1)
然后我们发现只有横坐标,纵坐标,以及出现时间三者都小于当前询问的才能对当前询问做出贡献。
ok了,三维偏序板题出现了
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define edl '\n'
const int N = 3e6 + 10;
const int M = 1e3 + 10;
const int mod = 1e9 + 7;
ll n,m,q;
string s;
template <typename T>
struct Fenwick {
int n;
std::vector<T> a;
Fenwick(int n_ = 0) {
init(n_);
}
void init(int n_) {
n = n_;
a.assign(n, T{});
}
void add(int x, const T &v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] = a[i - 1] + v;
}
}
//[0,x)
T sum(int x) {
T ans{};
for (int i = x; i > 0; i -= i & -i) {
ans = ans + a[i - 1];
}
return ans;
}
//左闭右开
T rangeSum(int l, int r) {
return sum(r) - sum(l);
}
//return 第一个sum(x) > k的x
int select(const T &k) {
int x = 0;
T cur{};
for (int i = 1 << __lg(n); i; i /= 2) {
if (x + i <= n && cur + a[x + i - 1] <= k) {
x += i;
cur = cur + a[x - 1];
}
}
return x;
}
};
Fenwick<ll> f(N);
struct QWQ
{
ll x,y,t,sum,op;
}a[N];
bool cmp(QWQ A,QWQ B)
{
if(A.x != B.x) return A.x < B.x;
if(A.y != B.y) return A.y < B.y;
if(A.t != B.t) return A.t < B.t;
}
bool cmp1(QWQ A,QWQ B)
{
if(A.y != B.y) return A.y < B.y;
if(A.t != B.t) return A.t < B.t;
}
bool cmp2(QWQ A,QWQ B){return A.t < B.t;}
ll x,y,z,x1,y2;
ll AN[N];
int idx;
void cdq(int l,int r)
{
if(l == r) return;
ll mid = (l + r) >> 1;
cdq(l,mid);
cdq(mid + 1,r);
sort(a + l,a + mid + 1,cmp1);
sort(a + mid + 1,a + r + 1,cmp1);
int i = l;
int j = mid + 1;
while(j <= r)
{
while(i <= mid && a[i].y <= a[j].y)
{
f.add(a[i].t,a[i].sum);
i ++;
}
AN[a[j].t] += f.sum(a[j].t + 1);
j ++;
}
for(j = l; j < i; j ++ ) f.add(a[j].t,-a[j].sum);
}
void solve()
{
while(cin >> q)
{
if(q == 3) break;
if(q == 0) cin >> n;
if(q == 1)
{
cin >> x >> y >> z;
a[++idx].x = x;
a[idx].y = y;
a[idx].t = idx;
a[idx].sum = z;
a[idx].op = 0;
}
if(q == 2)
{
cin >> x >> y >> x1 >> y2;
a[++idx] = {x - 1,y - 1,idx,0,1};
a[++idx] = {x1,y2,idx,0,1};
a[++idx] = {x - 1,y2,idx,0,1};
a[++idx] = {x1,y - 1,idx,0,1};
}
}
sort(a + 1, a + idx + 1,cmp);
cdq(1,idx);
sort(a + 1, a + idx + 1,cmp2);
for(int i = 1; i <= idx; i ++ )
{
if(a[i].op)
{
// if(a[i].x == 148)
// {
// cout << "?" << edl;
// cout << AN[a[i].t] << edl;
// cout << AN[a[i + 1].t] << edl;
// cout << AN[a[i + 2].t] << edl;
// cout << AN[a[i + 3].t] << edl;
// }
cout << AN[a[i].t] + AN[a[i + 1].t] - AN[a[i + 2].t] - AN[a[i + 3].t] << edl;
i += 3;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
// cin >> t;
while(t -- ) solve();
return 0;
}