Codevs 1080~1082 Segment Tree Practice Redo

Source: Internet
Author: User

To commemorate the first night of sleep in the computer room ~
Tonight to open a sub-block ~
Incidentally see how much more tonight
(March 18, 2016 21:03:28)

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <
Cmath> using namespace std;
const int MAXN = 1000000 + 5;
int n,m;
int NUM[MAXN];
int SUM[MAXN];
int M;
    void Init () {M = sqrt (n) + 1;
    for (int i = 0;i < N;i + +) sum[i/m] + = Num[i];
Return
    } void Change (int x,int y) {num[x] + = y;
    Sum[x/m] + = y;
Return
    } int Ask (int x,int y) {int ans = 0;
    while (x% M && x < y) ans + = num[x + +];
    while (y% M && y > x) ans + = num[y--];
    Ans + = num[y];
        while (x < Y) {ans + = sum[x/m];
    x + = M;
} return ans;
} int q,a,b,v;
    int main () {scanf ("%d", &n);
    for (int i = 0;i < N;i + +) scanf ("%d", &num[i]);
    Init ();
    scanf ("%d", &m);
        for (int i = 1;i <= m;i + +) {scanf ("%d", &q); Switch (q) {case 1:scanf ("%d%d", &a,&v); Change (A-1,v); break;
        Case 2:scanf ("%d%d", &a,&b);p rintf ("%d\n", Ask (a-1,b-1));
}} return 0; }

Second question ~
still uses chunking ~
Remember to mark
(March 18, 2016 21:17:28)

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <
Cmath> using namespace std;
const int MAXN = 1000000 + 5;
int n,m;
int NUM[MAXN];
int SUM[MAXN];
int ADD[MAXN];
int M;
    void Init () {M = sqrt (n) + 1;
    for (int i = 0;i < N;i + +) sum[i/m] + = Num[i];
Return
    } void Change (int x,int Y,int v) {while (x% M && x < y) Num[x + +] + = V;
    while (y% M && y > x) num[y--] + = V;
    Num[y] + = V;
        while (x < y) {add[x/m] + = V;
    x + = M;
} return;
} int ask (int x) {return num[x] + add[x/m];} int q,a,b,v;
    int main () {scanf ("%d", &n);
    for (int i = 0;i < N;i + +) scanf ("%d", &num[i]);
    Init ();
    scanf ("%d", &m);
        for (int i = 1;i <= m;i + +) {scanf ("%d", &q);
      Switch (q) {case 1:scanf ("%d%d", &a,&b,&v); change (A-1,B-1,V);      Case 2:scanf ("%d", &a);p rintf ("%d\n", Ask (A-1));
}} return 0; }

Third question
summarizes the experience is to think of what is good for the block, what is the element of ...
in other words, sum[x/m] do not write sum[x] ...
DQS: "Bunny, you're a fool X"
March 18, 2016 21:41:22

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <
Cmath> using namespace std;
typedef long Long LL;
const int MAXN = 1000000 + 5;
int n,m;
LL NUM[MAXN],SUM[MAXN],ADD[MAXN];
int M;
    void Init () {M = sqrt (n) + 1;
    for (int i = 0;i < N;i + +) sum[i/m] + = Num[i];
Return
        } void Change (int x,int Y,ll v) {while (x% M && x < y) {num[x] + = V;
        SUM[X/M] + = V;
    x + +;
        } while (y% M && y > x) {num[y] + = V;
        SUM[Y/M] + = V;
    Y--;
    } Num[y] + = V;
    SUM[Y/M] + = V;
        while (x < y) {add[x/m] + = V;
        SUM[X/M] + = v * M;
    x + = M;
} return;
    } ll ask (int x,int y) {ll ans = 0;
    while (x% M && x < y) ans + = Num[x] + add[x/m],x + +;
    while (y% M && y > x) ans + = Num[y] + add[y/m],y--;
    Ans + = Num[y] + add[y/m]; Whilex < Y) {ans + = sum[x/m];
    x + = M;
} return ans;
} int q,a,b;
LL v;
    int main () {scanf ("%d", &n);
    for (int i = 0;i < N;i + +) scanf ("%lld", &num[i]);
    Init ();
    scanf ("%d", &m);
        for (int i = 1;i <= m;i + +) {scanf ("%d", &q);
            Switch (q) {case 1:scanf ("%d%d%lld", &a,&b,&v); change (A-1,B-1,V);
        Case 2:scanf ("%d%d", &a,&b);p rintf ("%lld\n", Ask (a-1,b-1));
}} return 0; }

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.