#include <algorithm>
#include <iostream>
#define int int
#define return return
int Op, N, X, Y, Num, X2, Y2, Cnt = 0, QueryCnt;
struct Node {
int X, Y, Time, PrintPos, Weight, Value;
} Nodes[200005], Tmp[200005];
int Ans[200005];
bool Compare(Node Num1, Node Num2) {
if (Num1.X != Num2.X) return Num1.X < Num2.X;
if (Num1.Y != Num2.Y) return Num1.Y < Num2.Y;
if (Num1.Time != Num2.Time) return Num1.Time < Num2.Time;
return Num1.Value > Num2.Value;
}
class NIT {
public:
int Tree[200005];
void Add(int Id, int Val) {
while (Id <= Cnt) {
Tree[Id] += Val;
Id += (Id & (-Id));
}
}
int Query(int Id) {
int Result = 0;
while (Id > 0) {
Result += Tree[Id];
Id -= (Id & (-Id));
}
return Result;
}
} BITMain;
void CDQ(int Left, int Right) {
if (Left == Right) return;
int Mid = (Left + Right) / 2;
CDQ(Left, Mid);
CDQ(Mid + 1, Right);
int LeftPtr = Left, RightPtr = Mid + 1, TmpPtr = Left;
while (LeftPtr <= Mid && RightPtr <= Right) {
if (Nodes[LeftPtr].Y <= Nodes[RightPtr].Y) {
if (!Nodes[LeftPtr].Weight) {
BITMain.Add(Nodes[LeftPtr].Time, Nodes[LeftPtr].Value);
}
Tmp[TmpPtr++] = Nodes[LeftPtr++];
} else {
if (Nodes[Right].Weight) {
int Val = BITMain.Query(Nodes[RightPtr].Time);
Ans[Nodes[RightPtr].PrintPos] += Val * Nodes[RightPtr].Weight;
}
Tmp[TmpPtr++] = Nodes[RightPtr++];
}
}
while (RightPtr <= Right) {
if (Nodes[Right].Weight) {
int Val = BITMain.Query(Nodes[RightPtr].Time);
Ans[Nodes[RightPtr].PrintPos] += Val * Nodes[RightPtr].Weight;
}
Tmp[TmpPtr++] = Nodes[RightPtr++];
}
for (int i = Left; i < LeftPtr; i++) {
if (!Nodes[i].Weight) {
BITMain.Add(Nodes[i].Time, -Nodes[i].Value);
}
}
while (LeftPtr <= Mid) {
Tmp[TmpPtr++] = Nodes[LeftPtr++];
}
for (int i = Left; i <= Right; i++) {
Nodes[i] = Tmp[i];
}
}
int main() {
int TimeId = 0;
while (true) {
std::cin >> Op;
if (Op == 0) {
std::cin >> N;
continue;
}
if (Op == 1) {
std::cin >> X >> Y >> Num;
X++, Y++;
TimeId++;
Nodes[++Cnt] = {X, Y, TimeId, 0, 0, Num};
}
if (Op == 2) {
std::cin >> X >> Y >> X2 >> Y2;
X2++, Y2++, QueryCnt++;
Nodes[++Cnt] = {X2, Y2, TimeId, QueryCnt, 1, 0};
Nodes[++Cnt] = {X, Y, TimeId, QueryCnt, 1, 0};
Nodes[++Cnt] = {X, Y2, TimeId, QueryCnt, -1, 0};
Nodes[++Cnt] = {X2, Y, TimeId, QueryCnt, -1, 0};
}
if (Op == 3) break;
}
std::sort(Nodes + 1, Nodes + Cnt + 1, Compare);
CDQ(1, Cnt);
for (int i = 1; i <= QueryCnt; i++) {
std::cout << Ans[i] << '\n';
}
return 0;
}```
0分卡bug 样例正确