(C++)树状数组和线段树的VSCode Snippet

学都学了,肯定要往snippet里塞好东西嘛

{
	// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"SegmentTree": {
	 	"prefix": "SegmentTree",
		"body": [
				"class SegmentTree",
				"{",
				"private:",
					"\tstruct node{",
						"\t\tint l,r;",
						"\t\tint sum;",
					"\t};",
					"\tint size;",
					"\tstatic const int N=100010;",
					"\tnode t[N*4];",
					"\tint w[N];",
				"\n",
				"public:",
					"\tvoid getArray(vector<int> nu)",
					"\t{",
						"\t\tfor(int i : nu)",
						"\t\t{",
							"\t\t\tsize++;",
							"\t\t\tw[size] = i;",
						"\t\t}",
						"\t\tbuild(1, 1, size);",
					"\t}",
					"\tvoid build(int u, int l, int r)",
					"\t{",
						"\t\tif(l == r)",
							"\t\t\tt[u] = {l, r, w[r]};",
						"\t\telse",
						"\t\t{",
							"\t\t\tt[u] = {l,r};",
							"\t\t\tint mid = (l+r) >> 1;",
							"\t\t\tbuild(u<<1, l, mid);",
							"\t\t\tbuild(u<<1|1, mid+1, r);",
							"\t\t\tpushup(u);",
						"\t\t}",
					"\t}",
					"\tvoid pushup(int u)",
					"\t{",
						"\t\tt[u].sum = t[u<<1].sum + t[u<<1|1].sum;",
					"\t}",
					"\tint query(int u, int l, int r)",
					"\t{",
						"\t\tif(t[u].l >= l && t[u].r <= r)",
							"\t\t\treturn t[u].sum;",
						"\t\tint mid = (t[u].l + t[u].r) >> 1;",
						"\t\tint sum = 0;",
						"\t\tif(l<=mid)",
							"\t\t\tsum += query(u<<1, l, r);",
						"\t\tif(r > mid)",
							"\t\t\tsum += query(u<<1|1, l, r);",
						"\t\treturn sum;",
					"\t}",
					"\tvoid modify(int u, int x, int v)",
					"\t{",
						"\t\tif(t[u].l == t[u].r)",
							"\t\t\tt[u].sum += v;",
						"\t\telse",
						"\t\t{",
							"\t\t\tint mid = t[u].l + t[u].r >> 1;",
							"\t\t\tif(x <= mid)",
								"\t\t\t\tmodify(u<<1, x, v);",
							"\t\t\telse",
								"\t\t\t\tmodify(u<<1|1, x, v);",
							"\t\t\tpushup(u);",
						"\t\t}",
					"\t}",
				"};$0"
		 	],
		 	"description": "Instant Segment Tree"
		},
		"TreeArray": {
				"prefix": "TreeArray",
				"body": [
					"class TreeArray{",
					"private:",
					"    vector<int> tr;",
					"    int n;",
					"public:",
					"    void init(vector<int> g){",
					"        for(int i=1; i<=g.size(); i++)",
					"            tr.push_back(0);",
					"        n = tr.size();",
					"        for(int i=1; i<n; i++)",
					"            add(i, g[i]);",
					"    }",
					"    int lowbit(int x){",
					"        return x & -x;",
					"    }",
					"    void add(int x, int v){",
					"        for(int i=x; i<=n; i += lowbit(i))",
					"            tr[i]+=v;",
					"    }",
					"    int query(int x){",
					"        int res = 0;",
					"        for(int i=x; i; i -= lowbit(i))",
					"            res += tr[i];",
					"        return res;",
					"    }",
					"    int rangeQuery(int x, int y){",
					"        return query(y) - query(x-1);",
					"    }",
					"};"
				],
				"description": "Instant TreeArray"
			}
}
posted @ 2024-03-11 22:38  ComputerEngine  阅读(4)  评论(0编辑  收藏  举报