单点修改+区间查询
# include <iostream>
# include <cstring>
# include <algorithm>
# include <cstdio>
using namespace std;
const int N = 1e5 + 10 ;
struct node {
int l, r, sum;
} tree[ N<< 2 ] ;
void pushup ( int k)
{
tree[ k] . sum = tree[ k<< 1 ] . sum + tree[ k<< 1 | 1 ] . sum;
}
void build ( int k, int l, int r)
{
tree[ k] = { l, r, 0 } ;
if ( l== r)
{
return ;
}
int mid = ( l+ r) >> 1 ;
build ( k<< 1 , l, mid) ;
build ( k<< 1 | 1 , mid+ 1 , r) ;
pushup ( k) ;
}
void update ( int k, int pos)
{
if ( tree[ k] . r== tree[ k] . l)
{
tree[ k] . sum = 1 ;
return ;
}
int mid = ( tree[ k] . l+ tree[ k] . r) >> 1 ;
if ( pos<= mid)
update ( k<< 1 , pos) ;
else
update ( k<< 1 | 1 , pos) ;
pushup ( k) ;
}
int query ( int k, int l, int r)
{
if ( tree[ k] . l> r|| tree[ k] . r< l)
return 0 ;
if ( tree[ k] . l>= l&& tree[ k] . r<= r)
return tree[ k] . sum;
return query ( k<< 1 , l, r) + query ( k<< 1 | 1 , l, r) ;
}
int main ( )
{
}
区间修改+单点查询
# include <iostream>
# include <cstring>
# include <algorithm>
# include <cstdio>
using namespace std;
const int N = 1e5 + 10 ;
struct node {
int l, r, sum;
} tree[ N<< 2 ] ;
void pushup ( int k)
{
tree[ k] . sum = tree[ k<< 1 ] . sum + tree[ k<< 1 | 1 ] . sum;
}
void build ( int k, int l, int r)
{
tree[ k] = { l, r, 0 } ;
if ( l== r)
{
return ;
}
int mid = ( l+ r) >> 1 ;
build ( k<< 1 , l, mid) ;
build ( k<< 1 | 1 , mid+ 1 , r) ;
pushup ( k) ;
}
void update ( int k, int l, int r , int val)
{
if ( tree[ k] . l< r|| tree[ k] . r> l)
return ;
if ( tree[ k] . l>= l&& tree[ k] . r<= r)
{
tree[ k] . sum += val* ( tree[ k] . r - tree[ k] . l+ 1 ) ;
return ;
}
update ( k<< 1 , l, r, val) ;
update ( k<< 1 | 1 , l, r, val) ;
pushup ( k) ;
}
int query ( int k, int pos)
{
if ( tree[ k] . l== tree[ k] . r)
return tree[ k] . sum;
int mid = ( tree[ k] . l+ tree[ k] . r) >> 1 ;
if ( pos<= mid)
return query ( k<< 1 , pos) ;
else
return query ( k<< 1 | 1 , pos) ;
}
int main ( )
{
}
区间修改+区间查询
# include <iostream>
# include <cstring>
# include <algorithm>
# include <cstdio>
using namespace std;
const int N = 1e5 + 10 ;
struct node {
int l, r, sum, lazy;
} tree[ N<< 2 ] ;
void pushup ( int k)
{
tree[ k] . sum = tree[ k<< 1 ] . sum + tree[ k<< 1 | 1 ] . sum;
}
void pushdown ( int k)
{
if ( tree[ k] . lazy)
{
int lz = tree[ k] . lazy;
tree[ k] . lazy = 0 ;
tree[ k<< 1 ] . lazy+= lz;
tree[ k<< 1 | 1 ] . lazy+= lz;
tree[ k<< 1 ] . sum += lz* ( tree[ k<< 1 ] . r- tree[ k<< 1 ] . l+ 1 ) ;
tree[ k<< 1 | 1 ] . sum+= lz* ( tree[ k<< 1 ] . r- tree[ k<< 1 | 1 ] + 1 ) ;
}
}
void build ( int k, int l, int r)
{
tree[ k] = { l, r, 0 } ;
if ( l== r)
{
return ;
}
int mid = ( l+ r) >> 1 ;
build ( k<< 1 , l, mid) ;
build ( k<< 1 | 1 , mid+ 1 , r) ;
pushup ( k) ;
}
void update ( int k, int l, int r , int val)
{
if ( tree[ k] . l< r|| tree[ k] . r> l)
return ;
if ( tree[ k] . l>= l&& tree[ k] . r<= r)
{
tree[ k] . sum += val* ( tree[ k] . r - tree[ k] . l+ 1 ) ;
return ;
}
pushdown ( k) ;
update ( k<< 1 , l, r, val) ;
update ( k<< 1 | 1 , l, r, val) ;
pushup ( k) ;
}
int query ( int k, int pos)
{
if ( tree[ k] . l > r || tree[ k] . r< l)
return 0 ;
if ( tree[ k] . l>= l&& tree[ k] . r<= r)
return tree[ k] . sum;
pushdown ( k) ;
return query ( k<< 1 , l, r) + query ( k<< 1 | 1 , l, r) ;
}
int main ( )
{
}