CDQ divide and conquer approach:
Ensure that all t values in the left interval are less than all t values in the right interval. (T: time value)
Under this condition, update the tree array according to the x coordinate, and then query the interval value according to the y coordinate.
When querying interval values, pay attention to tolerance.
For the boundary conditions, after thinking about it, I found that it is not necessary to add 1 to all coordinates, because the value of 0 will only appear in the query, not in the change.
#include <bits/stdc++.h>
#define y1 yy1
#define lowbit(x) x&(-x)
using namespace std;
const int N= 2e6 + 5 , Q= 2e5 + 5 ;
int opt, n, x, y, w, x1, y1, x2, y2, tot, cnt;
int c[ N] , ans[ Q] ;
struct node{
int x, y, v, id, opt; } num[ Q] , t[ Q] ;
inline void change ( int x, int v)
{
while ( x<= n)
{
c[ x] + = v;
x+ = lowbit ( x) ;
}
}
inline int query ( int x)
{
int res= 0 ;
while ( x)
{
res+ = c[ x] ;
x- = lowbit ( x) ;
}
return res;
}
void solve ( int l, int r)
{
if ( l== r) return ;
int mid= l+ r>> 1 ;
solve ( l, mid) ; solve ( mid+ 1 , r) ;
int p= l, q= mid+ 1 , now= l;
while ( p<= mid && q<= r)
{
if ( num[ p] . x<= num[ q] . x)
{
if ( num[ p] . opt== 1 ) change ( num[ p] . y, num[ p] . v) ;
t[ now] = num[ p] ; p++ ; now++ ;
}
else
{
if ( num[ q] . opt== 2 ) ans[ num[ q] . id] + = query ( num[ q] . y) * num[ q] . v;
t[ now] = num[ q] ; q++ ; now++ ;
}
}
while ( p<= mid)
{
if ( num[ p] . opt== 1 ) change ( num[ p] . y, num[ p] . v) ;
t[ now] = num[ p] ; p++ ; now++ ;
}
while ( q<= r)
{
if ( num[ q] . opt== 2 ) ans[ num[ q] . id] + = query ( num[ q] . y) * num[ q] . v;
t[ now] = num[ q] ; q++ ; now++ ;
}
for ( register int i= l; i<= mid; ++ i) if ( num[ i] . opt== 1 ) change ( num[ i] . y, - num[ i] . v) ;
for ( register int i= l; i<= r; ++ i) num[ i] = t[ i] ;
}
int main ( ) {
scanf ( "%d" , & opt) ; scanf ( "%d" , & n) ;
scanf ( "%d" , & opt) ;
while ( opt!= 3 )
{
if ( opt== 1 )
{
scanf ( "%d%d%d" , & x, & y, & w) ;
num[ ++ cnt] = ( node) {
x, y, w, 0 , 1 } ;
}
else
{
scanf ( "%d%d%d%d" , & x1, & y1, & x2, & y2) ;
tot++ ;
num[ ++ cnt] = ( node) {
x1- 1 , y1- 1 , 1 , tot, 2 } ;
num[ ++ cnt] = ( node) {
x1- 1 , y2, - 1 , tot, 2 } ;
num[ ++ cnt] = ( node) {
x2, y1- 1 , - 1 , tot, 2 } ;
num[ ++ cnt] = ( node) {
x2, y2, 1 , tot, 2 } ;
}
scanf ( "%d" , & opt) ;
}
solve ( 1 , cnt) ;
for ( register int i= 1 ; i<= tot; ++ i) printf ( "%d\n" , ans[ i] ) ;
return 0 ;
}