POJ 3468 A simple problem with integers tree array or line segment tree

Source: Internet
Author: User
Tags stdin cmath

Title Link: http://poj.org/problem?id=3468

Generally, a tree array is

"Single Point update interval query"

And the subject is "Interval Update interval query"

So change to a maintenance prefix and

The process of pushing the formula is shown in the Challenge Program design Competition on the 181th to 182th side.

Bit0 in code is 0 items of I

Bit1 is a one-time item of I

And so on later

At the time of [L, R] Addend

Write a formula

In the place of L the direct write of the first and above

And then subtract it in 0 times.

In the place of R where one item and above are added before the subtraction

And then 0 more times that plus "the final result after the formulation of Jane minus the one that was added in 0 times"

"It's messy, but I understand that."

The following is a tree-like array:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<Set>#include<map>#include<stack>#include<algorithm>#include<cmath>#include<queue>#include<vector>using namespaceStd;typedefLong Longll;typedef unsignedLong Longull;Constull B =9973;Const intMAXN =110000;Constll M =23333; ll Bit0[maxn];ll BIT1[MAXN];intN, Q;ll sum (ll*b,inti) {ll S=0;  while(I >0) {s+=B[i]; I-= i &-i; }    returns;}voidAdd (ll *b,intIintv) {     while(I <=N) {B[i]+=v; I+ = i &-i; }}intMain () {#ifdef LOCAL freopen ("Input.txt","R", stdin); //freopen ("Output.txt", "w", stdout);#endif     while(SCANF ("%d%d", &n, &q) = =2)    {         for(inti =1; I <= N; i++)        {            intT; scanf ("%d", &t);        Add (bit0, I, t); }         for(inti =0; i < Q; i++)        {            Charmode; scanf ("%c", &mode); scanf ("%c", &mode); if(Mode = ='C')            {                intL, R, X; scanf ("%d%d%d", &l, &r, &x); Add (Bit0, L,-x* (l1));                Add (Bit1, L, X); Add (bit0, R+1, x*R); Add (bit1, R+1, -x); }            Else            {                intL, R; scanf ("%d%d", &l, &R); ll Res=0; Res+ = SUM (bit0, R) + sum (bit1, R) *R; Res-= SUM (bit0, L-1) + SUM (bit1, L-1) * (l1); printf ("%lld\n", RES); }        }    }    return 0;}

Then there is a line tree:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<Set>#include<map>#include<stack>#include<algorithm>#include<cmath>#include<queue>#include<vector>using namespaceStd;typedefLong Longll;typedef unsignedLong Longull;Const intMAXN =410000; ll DATA[MAXN], DATB[MAXN];intN, Q;voidAddintAintBintXintKintLintR) {    if(A <= l && b >=R) {Data[k]+=x; }    Else if(L < b && R >a) {Datb[k]+ = (min (b, R)-Max (A, L)) *x; Add (A, B, X, K*2+1, L, (l+r)/2); Add (A, B, X, K*2+2, (L+R)/2, R); }}ll sum (intAintBintKintLintR) {    if(b <= L | | a >=R)return 0; Else if(A <= l && b >=r) {returndata[k]* (r-l) +Datb[k]; }    Else{ll res= (min (b, R)-Max (A, L)) *Data[k]; Res+ = SUM (A, B, k*2+1, L, (l+r)/2); Res+ = SUM (A, B, k*2+2, (L+R)/2, R); returnRes; }}intMain () {#ifdef LOCAL freopen ("Input.txt","R", stdin); //freopen ("Output.txt", "w", stdout);#endif     while(SCANF ("%d%d", &n, &q) = =2)    {         for(inti =0; I < n; i++)        {            intT; scanf ("%d", &t); Add (i, I+1T0,0, N); }         for(inti =0; i < Q; i++)        {            Charmode; scanf ("%c", &mode); scanf ("%c", &mode); if(Mode = ='C')            {                intA, b, X; scanf ("%d%d%d", &a, &b, &x); Add (a-1, B, X,0,0, N); }            Else            {                intA, B; scanf ("%d%d", &a, &b); printf ("%lld\n", SUM (A-1B0,0, N)); }        }    }    return 0;}

POJ 3468 A simple problem with integers tree array or line segment tree

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.