一些关于常数的问题(动态线段树)

P3374 【模板】树状数组 1

## 代码1:(函数全在结构体内) ```cpp #include <iostream> #include <cstdio> using namespace std; struct node{ int l , r , val; node *lson , *rson; node(int L = 0 , int R = 0 , int VAL = 0 , node* LSON = NULL , node* RSON = NULL):l(L),r(R),val(VAL),lson(LSON),rson(RSON){}; }; struct tree{ node *root = new node; node* buildl(node *x){ if (x -> lson) return x -> lson; return x -> lson = new node(x -> l , (x -> l + x -> r) >> 1); } node* buildr(node *x){ if (x -> rson) return x -> rson; return x -> rson = new node(((x -> l + x -> r) >> 1) + 1 , x -> r); } void pushup(node *x){ x -> val = 0; if (x -> lson) x -> val += x -> lson -> val; if (x -> rson) x -> val += x -> rson -> val; } void build(int l , int r){ *root = node(l , r); } void update(node *x , int pos , int d){ int l = x -> l , r = x -> r; if (l == r){ x -> val += d; return; } int m = l + r >> 1; if (pos <= m) update(buildl(x) , pos , d); else update(buildr(x) , pos , d); pushup(x); } int query(node *x , int a , int b){ int l = x -> l , r = x -> r; if (a <= l && r <= b) return x -> val; int m = l + r >> 1; int ans = 0; if (a <= m) ans += query(buildl(x) , a , b); if (m < b) ans += query(buildr(x) , a , b); return ans; } }; tree t; int n,m; int main(){ cin >> n >> m; t.build(1 , n); int x,y,z; for (int i = 1;i <= n;i++){ cin >> x; t.update(t.root , i , x); } while (m--){ cin >> x >> y >> z; if (x == 1){ t.update(t.root , y , z); } else{ cout << t.query(t.root , y , z) << '\n'; } } return 0; } ``` ## 代码2:(函数有的在结构体内,有的在结构体外) ```cpp #include <iostream> #include <cstdio> using namespace std; struct node{ int l , r , val; node *lson , *rson; node(int L = 0 , int R = 0 , int VAL = 0 , node* LSON = NULL , node* RSON = NULL):l(L),r(R),val(VAL),lson(LSON),rson(RSON){}; }; struct tree{ node *root = new node; node* buildl(node *x){ if (x -> lson) return x -> lson; return x -> lson = new node(x -> l , (x -> l + x -> r) >> 1); } node* buildr(node *x){ if (x -> rson) return x -> rson; return x -> rson = new node(((x -> l + x -> r) >> 1) + 1 , x -> r); } void pushup(node *x){ x -> val = 0; if (x -> lson) x -> val += x -> lson -> val; if (x -> rson) x -> val += x -> rson -> val; } void build(int l , int r){ *root = node(l , r); } void update(node *x , int pos , int d){ int l = x -> l , r = x -> r; if (l == r){ x -> val += d; return; } int m = l + r >> 1; if (pos <= m) update(buildl(x) , pos , d); else update(buildr(x) , pos , d); pushup(x); } int query(node *x , int a , int b){ int l = x -> l , r = x -> r; if (a <= l && r <= b) return x -> val; int m = l + r >> 1; int ans = 0; if (a <= m) ans += query(buildl(x) , a , b); if (m < b) ans += query(buildr(x) , a , b); return ans; } }; tree t; int n,m; int main(){ cin >> n >> m; t.build(1 , n); int x,y,z; for (int i = 1;i <= n;i++){ cin >> x; t.update(t.root , i , x); } while (m--){ cin >> x >> y >> z; if (x == 1){ t.update(t.root , y , z); } else{ cout << t.query(t.root , y , z) << '\n'; } } return 0; } ``` ## 代码3:(函数全在结构体外) ```cpp #include <iostream> #include <cstdio> using namespace std; struct node{ int l , r , val; node *lson , *rson; node(int L = 0 , int R = 0 , int VAL = 0 , node* LSON = NULL , node* RSON = NULL):l(L),r(R),val(VAL),lson(LSON),rson(RSON){}; }; struct tree{ node *root = new node; void build(int l , int r){ *root = node(l , r); } }; node* buildl(node *x){ if (x -> lson) return x -> lson; return x -> lson = new node(x -> l , (x -> l + x -> r) >> 1); } node* buildr(node *x){ if (x -> rson) return x -> rson; return x -> rson = new node(((x -> l + x -> r) >> 1) + 1 , x -> r); } void pushup(node *x){ x -> val = 0; if (x -> lson) x -> val += x -> lson -> val; if (x -> rson) x -> val += x -> rson -> val; } void update(node *x , int pos , int d){ int l = x -> l , r = x -> r; if (l == r){ x -> val += d; return; } int m = l + r >> 1; if (pos <= m) update(buildl(x) , pos , d); else update(buildr(x) , pos , d); pushup(x); } int query(node *x , int a , int b){ int l = x -> l , r = x -> r; if (a <= l && r <= b) return x -> val; int m = l + r >> 1; int ans = 0; if (a <= m) ans += query(buildl(x) , a , b); if (m < b) ans += query(buildr(x) , a , b); return ans; } tree t; int n,m; int main(){ cin >> n >> m; t.build(1 , n); int x,y,z; for (int i = 1;i <= n;i++){ cin >> x; update(t.root , i , x); } while (m--){ cin >> x >> y >> z; if (x == 1){ update(t.root , y , z); } else{ cout << query(t.root , y , z) << '\n'; } } return 0; } ```
by Michael2012 @ 2024-08-21 21:53:19


@[Michael2012](/user/689640) 应该是的,大概是玄学常数 还有之前某同学写了一个大概是 `for` 里面嵌套判断函数,每循环一边调用一次判断函数,然后 T 了,结果把函数里的代码直接复制到 `for` 循环内就 A 了,差不多也是玄学常数
by No_Rest @ 2024-08-21 22:05:24


@[ldf1208](/user/550821) 很多 STL 和 C++ 自带的东西似乎常数都不小
by No_Rest @ 2024-08-21 22:07:11


|