1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include <bits/stdc++.h> #define all(vec) vec.begin(),vec.end() #define lson(o) (o<<1) #define rson(o) (o<<1|1) #define SZ(a) ((long long) a.size()) #define debug(var) cerr << #var <<" = ["<<var<<"]"<<"\n"; #define debug1d(a) \ cerr << #a << " = ["; \ for (int i = 0; i < (int)(a).size(); i++) \ cerr << (i ? ", " : "") << a[i]; \ cerr << "]\n"; #define debug2d(a) \ cerr << #a << " = [\n"; \ for (int i = 0; i < (int)(a).size(); i++) \ { \ cerr << " ["; \ for (int j = 0; j < (int)(a[i]).size(); j++) \ cerr << (j ? ", " : "") << a[i][j]; \ cerr << "]\n"; \ } \ cerr << "]\n"; #define cend cerr<<"\n-----------\n" #define fsp(x) fixed<<setprecision(x)
using namespace std;
using ll = long long; using ull = unsigned long long; using DB = double; using i128 = __int128; using CD = complex<double>;
static constexpr ll MAXN = (ll)1e6+10, INF = (1ll<<61)-1; static constexpr ll mod = 998244353; static constexpr double eps = 1e-8; const double pi = acos(-1.0);
ll lT,testcase;
ostream& operator<<(ostream& os, i128 x) { if (x < 0) os << '-', x = -x; if (x > 9) os << x / 10; return os << (char)('0' + x % 10); }
struct Tag { ll add=0; bool need_apply=false; void apply(const Tag &t) { add+=t.add; need_apply=true; } };
struct Info { ll l=0,r=0; i128 sum=0; static Info merge(const Info &a,const Info &b) { Info res=a; res.r=b.r; res.sum+=b.sum; return res; } void apply(const Tag&t) { if (!t.need_apply) { return; } ll len=r-l+1; sum+=len*t.add; } };
template<class I=Info,class T=Tag> struct SEG { struct Node { I info; T tag; Node* rs=nullptr; Node* ls=nullptr; };
void init_node(Node *u,ll l,ll r) { u->info.l=l; u->info.r=r; u->info.sum=i128(u->info.l+u->info.r)*(u->info.r-u->info.l+1)/2; }
Node* root=nullptr; ll N;
static bool isIntersect(ll l1, ll r1, ll l2, ll r2) { if (!(l1 > r2 || r1 < l2)) return true; return false; }
SEG(ll N):N(N) { root=new Node(); root->info.l=1; root->info.r=N; init_node(root,1,N); }
void extend(Node *u) { if (u->ls==nullptr) { u->ls=new Node(); init_node(u->ls,u->info.l,(u->info.l+u->info.r)>>1); } if (u->rs==nullptr) { u->rs=new Node(); init_node(u->rs,((u->info.l+u->info.r)>>1)+1,u->info.r); } }
void push(Node *u) { if (u->info.l==u->info.r) { return; } extend(u); if (u->tag.need_apply) { u->ls->info.apply(u->tag); u->rs->info.apply(u->tag); u->ls->tag.apply(u->tag); u->rs->tag.apply(u->tag); u->tag={}; } } void pull(Node *u) { if (u->info.l==u->info.r) { return; } u->info=I::merge(u->ls->info,u->rs->info); } void modify(Node*u,ll l,ll r,const T& t) { if (u->info.l>=l && u->info.r<=r) { u->info.apply(t); u->tag.apply(t); return; } push(u); if (isIntersect(u->ls->info.l,u->ls->info.r,l,r)) { modify(u->ls,l,r,t); } if (isIntersect(u->rs->info.l,u->rs->info.r,l,r)) { modify(u->rs,l,r,t); } pull(u); }
Info query(Node *u,ll l,ll r) { if (u->info.l>=l && u->info.r<=r) { return u->info; } Info res; bool is_get=false; push(u); if (isIntersect(u->ls->info.l,u->ls->info.r,l,r)) { res=query(u->ls,l,r); is_get=true; } if (isIntersect(u->rs->info.l,u->rs->info.r,l,r)) { if (!is_get) { res=query(u->rs,l,r); }else { res=I::merge(res,query(u->rs,l,r)); } } return res; } };
void Solve() { ll N,M; cin >> N >> M; SEG<Info,Tag> seg(N); for (int i=1;i<=M;++i) { ll op; cin>>op; if (op==1) { ll l,r,k; cin>>l>>r>>k; seg.modify(seg.root,l,r,{k,true}); }else { ll l,r; cin>>l>>r; cout<<seg.query(seg.root,l,r).sum<<"\n"; } } }
signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL cout.setf(ios::unitbuf); #endif
Solve(); return 0; }
|