POJ 3468 A simple problem with integers

Source: Internet
Author: User

Test instructions: A group of numbers, two operations, Q for the query L to r interval and C for the change L to r interval value for the original value plus C.

Solution: You can use a segment tree or a tree-like array.

Segment number into a paragraph update, set template changed, never thought the topic said int will not explode is to deceive me ......

Line segment Tree Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL Long Long#define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1#define maxn 100005using namespace std; LL SUM[MAXN << 2], COL[MAXN << 2], POINT[MAXN << 2];void pushdown (int rt) {if (Col[rt]) {Co        L[rt << 1] + = Col[rt];        Col[rt << 1 | 1] + + COL[RT];        Sum[rt << 1] + = col[rt] * point[rt << 1];        Sum[rt << 1 | 1] + = col[rt] * Point[rt << 1 | 1];    COL[RT] = 0; }}void pushup (int rt) {Sum[rt] = Sum[rt << 1] + sum[rt << 1 | 1];}    void build (int l, int r, int rt) {Col[rt] = 0;        if (L = = r) {scanf ("%i64d", &sum[rt]);   POINT[RT] = 1;     return;    } int m = (L + r) >> 1;    Build (Lson);    Build (Rson);    POINT[RT] = point[rt << 1] + point[rt << 1 | 1]; Pushup (RT);} void Update (int ll, int rr, int c, int l, int r, int rt) {if (ll <= l && RR >= R) {Col[rt] + = C        ;        SUM[RT] + = (LL) c * POINT[RT];    return;    } pushdown (RT);    int m = (L + r) >> 1;    if (ll <= m) update (LL, RR, C, Lson);    if (RR > M) update (LL, RR, C, Rson); Pushup (RT);}    ll query (int ll, int rr, int l, int r, int rt) {if (ll <= l && RR >= R) return SUM[RT];    Pushdown (RT);    int m = (L + r) >> 1;    LL ret = 0;    if (ll <= m) ret + = query (ll, RR, Lson);    if (RR > m) ret + = query (ll, RR, Rson); return ret;}    int main () {int n, q;        while (~SCANF ("%d%d", &n, &q)) {build (1, N, 1);            for (int i = 0; i < Q; i++) {char ch[2];           scanf ("%s", ch); if (ch[0] = = ' Q ') {int L, R;                scanf ("%d%d", &l, &r);            printf ("%i64d\n", query (L, R, 1, N, 1));                } else {int L, r, C;                scanf ("%d%d%d", &l, &r, &c);            Update (L, R, c, 1, N, 1); }}} return 0;}

Tree-like array solution: http://kenby.iteye.com/blog/962159

It's very well written ... I will not say more ...

Tree-like array code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL Long longusing namespace Std; LL sum[100005], c1[100005], c2[100005];int n;inline int lowbit (int x) {return x & (x);} void update (LL a[], int pos, LL val) {for (int i = pos; I <= n; i + = Lowbit (i)) a[i] + = val;}    ll Getsum (ll a[], int pos) {ll res = 0;    for (int i = pos; i > 0; I-= Lowbit (i)) res + = A[i]; return res;}    int main () {int m;        while (~SCANF ("%d%d", &n, &m)) {memset (c1, 0, sizeof (C1));        memset (c2, 0, sizeof (C2));        Sum[0] = 0;            for (int i = 1; I <= n; i++) {LL x;            scanf ("%lld", &x);        Sum[i] = sum[i-1] + x; } for (int i = 0; I &lT M            i++) {char com[2];            int L, R;            scanf ("%s%d%d", com, &l, &r);                if (com[0] = = ' Q ') {LL ans = sum[r]-SUM[L-1];                Ans + = (R + 1) * Getsum (C1, R)-L * getsum (C1, L-1);                Ans-= getsum (C2, R)-Getsum (C2, L-1);            printf ("%i64d\n", ans);                } else {LL C;                scanf ("%i64d", &c);                Update (C1, L, c);                Update (c1, R + 1,-c);                Update (C2, L, c * l);            Update (C2, R + 1,-c * (R + 1)); }}} return 0;}

  

POJ 3468 A simple problem with integers

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.