This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub tko919/library
#define PROBLEM "https://judge.yosupo.jp/problem/point_add_rectangle_sum" #include "Template/template.hpp" #include "DataStructure/2dsegtree.hpp" ll f(ll a,ll b){return a+b;} ll g(ll a,ll b){return a+b;} ll e(){return 0;} int main(){ int n,q; cin>>n>>q; vector<int> x(n),y(n),w(n); rep(i,0,n)cin>>x[i]>>y[i]>>w[i]; vector<int> t(q); using P=array<int,4>; vector<P> que(q); rep(i,0,q){ cin>>t[i]; if(t[i]==0)rep(j,0,3)cin>>que[i][j]; else rep(j,0,4)cin>>que[i][j]; } SegmentTree2D<ll,ll,f,g,e> seg; rep(i,0,n)seg.push(x[i],y[i]); rep(i,0,q)if(t[i]==0)seg.push(que[i][0],que[i][1]); seg.init(); rep(i,0,n)seg.update(x[i],y[i],w[i]); rep(i,0,q){ if(t[i]==0)seg.update(que[i][0],que[i][1],que[i][2]); else cout<<seg.query(que[i][0],que[i][2],que[i][1],que[i][3])<<'\n'; } return 0; }
#line 1 "Verify/LC_point_add_rectangle_sum.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/point_add_rectangle_sum" #line 1 "Template/template.hpp" #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(b-1); i >= (int)(a); i--) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define SZ(v) (int)v.size() #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin()) #define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin()) using uint = unsigned int; using ll = long long int; using ull = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T, typename U> T ceil(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? (x + y - 1) / y : x / y); } template <typename T, typename U> T floor(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? x / y : (x - y + 1) / y); } template <typename T> int popcnt(T x) { return __builtin_popcountll(x); } template <typename T> int topbit(T x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } template <typename T> int lowbit(T x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } #ifdef LOCAL #define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__) #else #define show(...) true #endif template <typename T> void _show(int i, T name) { cerr << '\n'; } template <typename T1, typename T2, typename... T3> void _show(int i, const T1 &a, const T2 &b, const T3 &...c) { for (; a[i] != ',' && a[i] != '\0'; i++) cerr << a[i]; cerr << ":" << b << " "; _show(i + 1, a, c...); } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "P(" << p.first << ", " << p.second << ")"; return os; } template <typename T, template <class> class C> ostream &operator<<(ostream &os, const C<T> &v) { os << "["; for (auto d : v) os << d << ", "; os << "]"; return os; } #line 2 "DataStructure/segtree.hpp" template<typename M,typename N,M (*f)(M,M),M (*g)(M,N),M (*m1)()>struct SegmentTree{ int n; vector<M> data; SegmentTree(int _n=0){ n=1; while(n<_n)n<<=1; data.assign(2*n,m1()); } void run(vector<M>& v){ for(int i=0;i<(int)v.size();i++)data[i+n]=v[i]; for(int k=n-1;k>0;k--)data[k]=f(data[2*k],data[2*k+1]); } void set(int k,const M &x){ k+=n; data[k]=x; while(k>>=1)data[k]=f(data[2*k],data[2*k+1]); } void update(int k,const N &x){ k+=n; data[k]=g(data[k],x); while(k>>=1)data[k]=f(data[2*k],data[2*k+1]); } M query(int a,int b){ M L=m1(),R=m1(); for(a+=n,b+=n;a<b;a>>=1,b>>=1){ if(a&1)L=f(L,data[a++]); if(b&1)R=f(data[--b],R); } return f(L,R); } M operator[](const int &k)const{return data[k+n];} }; /** * @brief Segment Tree */ #line 3 "DataStructure/2dsegtree.hpp" template<typename M,typename N,M (*f)(M,M),M (*g)(M,N),M (*m1)()>struct SegmentTree2D{ int n; vector<SegmentTree<M,N,f,g,m1>> st; vector<vector<int>> ys; SegmentTree2D(){} int id(int x,int y){return lower_bound(ALL(ys[x]),y)-ys[x].begin();} using P=pair<int,int>; vector<int> xs; vector<P> ps; void push(int x,int y){ ps.push_back({x,y}); } void init(){ for(auto& [x,y]:ps)xs.push_back(x); sort(ALL(xs)); xs.erase(unique(ALL(xs)),xs.end()); n=xs.size(); ys.resize(n*2); for(auto& [x,y]:ps){ int i=lower_bound(ALL(xs),x)-xs.begin(); for(i+=n;i;i>>=1)ys[i].push_back(y); } for(int i=0;i<n*2;i++){ sort(ALL(ys[i])); ys[i].erase(unique(ALL(ys[i])),ys[i].end()); st.push_back(SegmentTree<M,N,f,g,m1>(ys[i].size())); } } void update(int x,int y,N w){ x=(lower_bound(ALL(xs),x)-xs.begin())+n; st[x].update(id(x,y),w); M cur=st[x][id(x,y)]; int pos=x; while(x>>=1){ M opp=st[pos^1].query(id(pos^1,y),id(pos^1,y+1)); cur=f(cur,opp); st[x].set(id(x,y),cur); pos>>=1; } } M query(int x0,int x1,int y0,int y1){ x0=lower_bound(ALL(xs),x0)-xs.begin(); x1=lower_bound(ALL(xs),x1)-xs.begin(); M res=m1(); for(x0+=n,x1+=n;x0<x1;x0>>=1,x1>>=1){ if(x0&1)res=f(res,st[x0].query(id(x0,y0),id(x0,y1))),x0++; if(x1&1)x1--,res=f(res,st[x1].query(id(x1,y0),id(x1,y1))); } return res; } }; /** * @brief 2D Segment Tree */ #line 5 "Verify/LC_point_add_rectangle_sum.test.cpp" ll f(ll a,ll b){return a+b;} ll g(ll a,ll b){return a+b;} ll e(){return 0;} int main(){ int n,q; cin>>n>>q; vector<int> x(n),y(n),w(n); rep(i,0,n)cin>>x[i]>>y[i]>>w[i]; vector<int> t(q); using P=array<int,4>; vector<P> que(q); rep(i,0,q){ cin>>t[i]; if(t[i]==0)rep(j,0,3)cin>>que[i][j]; else rep(j,0,4)cin>>que[i][j]; } SegmentTree2D<ll,ll,f,g,e> seg; rep(i,0,n)seg.push(x[i],y[i]); rep(i,0,q)if(t[i]==0)seg.push(que[i][0],que[i][1]); seg.init(); rep(i,0,n)seg.update(x[i],y[i],w[i]); rep(i,0,q){ if(t[i]==0)seg.update(que[i][0],que[i][1],que[i][2]); else cout<<seg.query(que[i][0],que[i][2],que[i][1],que[i][3])<<'\n'; } return 0; }