Uva12086-potentiometer (line segment tree/tree array)

Source: Internet
Author: User

Uva12086-potentiometer (line segment tree/tree array)

Question Link

Give you n numbers and then have Q operations. Operation Type: M indicates modifying the value of a certain position to R, and s indicates querying the numbers and values of a certain segment.

Solution: Line Segment tree or tree array.

Line Segment tree

#include <cstdio>#include <cstring>const int N = 8e5 + 5;int v[N];int n;int Query (int o, int l, int r, int ql, int qr) {    int    m = l + (r - l) / 2;    if (ql == l && r == qr)        return v[o];     if (qr <= m)        return Query(2 * o, l, m, ql, qr);    else if (ql > m)        return Query(2 * o + 1, m + 1, r, ql, qr);    else         return Query(2 * o, l, m, ql, m) + Query(2 * o + 1, m + 1, r, m + 1, qr);}void Update (int o, int l, int r, int p, int val) {    int m = l + (r - l) / 2;    if (l == r)         v[o] = val;    else {        if (p <= m)            Update (2 * o, l, m, p, val);        else            Update (2 * o + 1, m + 1, r, p, val);        v[o] = v[2 * o] + v[2 * o + 1];    }}void solve () {    char str[10];    int x, y, r;    while (scanf ("%s", str) && str[0] != ‘E‘) {        if (str[0] == ‘M‘) {            scanf ("%d%d", &x, &y);            printf ("%d\n", Query (1, 1, n, x, y));        } else {            scanf ("%d%d", &x, &r);            Update (1, 1, n, x, r);            }    }}int main () {    int cas = 0;    int num;    while (scanf ("%d", &n) && n) {        if (cas)            printf ("\n");        printf ("Case %d:\n", ++cas);        memset (v, 0, sizeof (v));        for (int i = 1; i <= n; i++) {            scanf ("%d", &num);            Update (1, 1, n, i, num);        }        solve();                }    return 0;}

Tree Array

# Include <cstdio >#include <cstring> const int maxn = 2e5 + 5; int lowbit (int x) {return X &-X;} int N; int A [maxn], C [maxn]; void add (int x, int d) {While (x <= N) {C [x] + = D; X + = lowbit (x) ;}} int sum (int x) {int ret = 0; while (x> 0) {RET + = C [x]; x-= lowbit (x);} return ret;} void solve () {char STR [10]; int X, R, Y; while (scanf ("% s ", str) & STR [0]! = 'E') {If (STR [0] = 'M') {scanf ("% d", & X, & Y ); printf ("% d \ n", sum (y)-sum (X-1);} else {scanf ("% d", & X, & R); add (x, R-A [x]); A [x] = r; // remember to modify this part.} int main () {int CAS = 0; while (scanf ("% d", & N) {If (CAS) printf ("\ n"); memset (C, 0, sizeof (c); For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]); add (I, a [I]);} printf ("case % d: \ n", ++ CAS); solve ();} return 0 ;}

Uva12086-potentiometer (line segment tree/tree array)

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.