"Hibernate Step by Step"--a detailed description of composite primary key mappings

Source: Internet
Author: User

The subject is the most basic segmented tree operation. or generally called line tree, but it seems to have nothing to do with the line, just segmented.

Without using the lazy flag, the update is just a single point of updating.

If you do not use a segmented tree, then the update time efficiency requires only O (1), and using the segmented tree update efficiency requires O (LGN).

But not with the segmented tree, the time efficiency of the query is O (n), and the segmentation tree query efficiency is O (LGN)

This is the time amortize apportioned, and lgn really fast, the data is not very large time, close to the constant.

Therefore, we need to use a segmented tree.

#include <cstdio>class enemyinfo{static const int SIZE = 50001;int segtree[size<<2];inline int lchild (int r) { Return r<<1; }inline int rchild (int r) {return r<<1|1;} void pushup (int root) {Segtree[root] = Segtree[lchild (root)] + segtree[rchild (root)];} void Buildtree (int l, int r, int rt) {if (L = = r) {scanf ("%d", &segtree[rt]); return;} int m = l + ((r-l) >>1) Buildtree (L, M, Lchild (RT)), Buildtree (m+1, R, Rchild (RT));p Ushup (RT);} void Update (int addpoint, int addnum, int l, int r, int rt) {if (L = = r) {Segtree[rt] + = Addnum;return;} int m = l + ((r-l) >>1), if (Addpoint <= m) Update (Addpoint, Addnum, L, M, Lchild (RT)); Else update (Addpoint, Addnum , M+1, R, Rchild (RT));p Ushup (RT);}  int query (const int l, const int R, int L, int r, int rt) {if (L <= l && R <= R) return segtree[rt];int m = L + ((r-l) >>1); int res = 0;if (l <= m) Res + = query (l, R, L, M, Lchild (RT)), if (R > m) Res + = query (l, R, M+1, R, Rchild (RT)); return res;} Public: Enemyinfo () {int T, n, A, b;scanf ("%d", &t), for (int T = 1; t <= t; t + +) {printf ("Case%d:\n", T), scanf ("%d", &n ); Buildtree (1, N, 1); Char Op[6];while (scanf ("%s", op) && op[0]! = ' E ') {scanf ("%d%d", &a,&b); if (op[0] = =  ' Q ') printf ("%d\n", query (A, B, 1, N, 1)), else if (op[0] = = ' S ') update (A,-B, 1, n, 1); Else update (A, B, 1, N, 1);}}};



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.