Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //do struct [set_segm, get_segm]
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- struct node{
- int L, R;
- node* l = nullptr;
- node* r = nullptr;
- bool all = true;
- ll x = 0;
- node(int a, int b){
- L = a;
- R = b;
- }
- };
- void init(node* &v, int lr, int rr){
- v = new node(lr, rr);
- if(lr == rr-1)
- return;
- int m = (lr + rr)/2;
- init(v->l, lr, m);
- init(v->r, m, rr);
- }
- void push(node* &v){
- v->l->all = true;
- v->r->all = true;
- ll y = v->x / (v->R - v->L);
- v->l->x = y* (v->l->R - v->l->L);
- v->r->x = y* (v->r->R - v->r->L);
- }
- void setx(node* &v, int lr, int rr, ll y){
- if(v->R <= lr || rr <= v->L)
- return;
- if(lr <= v->L && v->R <= rr){
- v->all = true;
- v->x = (v->R - v->L) * y;
- return;
- }
- if(v->all)
- push(v);
- v->all = false;
- setx(v->l, lr, rr, y);
- setx(v->r, lr, rr, y);
- v->x = v->l->x + v->r->x;
- }
- ll get(node* &v, int lr, int rr){
- if(v->R <= lr || rr <= v->L)
- return 0;
- if(lr <= v->L && v->R <= rr)
- return v->x;
- if(v->all)
- push(v);
- return get(v->l, lr, rr) + get(v->r, lr, rr);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n, m, t, l, r, v;
- cin >> n >> m;
- node* root = new node(0, n);
- init(root, 0, n);
- while(m--){
- cin >> t >> l >> r;
- if(t == 1){
- cin >> v;
- setx(root, l, r, v);
- } else{
- cout << get(root, l, r) << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement