"POJ" 3468 A simple problem with integers

Source: Internet
Author: User

This problem is easily solved with a segment tree, re-using the tree array solution, the key point is the interval update.
The formula is deduced as follows:
SUM[X] = Org_sum[x] + delta[1]*x + delta[2]* (x-1) + delta[x]*1
= Org_sum[x] + sigma (delta[1..x]) * (x+1)-Sigma (Delta[i]*i)
Tree array Add two node information to save Delta[i] and Delta[i] * I will be OK.

1 /*3468*/2#include <iostream>3#include <sstream>4#include <string>5#include <map>6#include <queue>7#include <Set>8#include <stack>9#include <vector>Ten#include <deque> One#include <algorithm> A#include <cstdio> -#include <cmath> -#include <ctime> the#include <cstring> -#include <climits> -#include <cctype> -#include <cassert> +#include <functional> -#include <iterator> +#include <iomanip> A using namespacestd; at //#pragma COMMENT (linker, "/stack:102400000,1024000") -  - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> in #definePII pair<int,int> - #defineVpii vector<pair<int,int> > to #defineRep (I, A, n) for (int i=a;i<n;++i) + #definePer (i, a, n) for (int i=n-1;i>=a;--i) - #defineCLR Clear the #definePB Push_back * #defineMP Make_pair $ #defineFIR FirstPanax Notoginseng #defineSEC Second - #defineAll (x) (x). Begin (), (x). End () the #defineSZ (x) ((int) (x). Size ()) + #defineLson L, Mid, rt<<1 A #defineRson mid+1, R, rt<<1|1 the  +typedefstruct { - __int64 S, SS; $ } node_t; $  - Const intMAXN = 1e5+5; - intA[MAXN]; the __int64 TOT[MAXN]; - node_t ND[MAXN];Wuyi intN, Q; the  - intLowestintx) { Wu     return-X &x; - } About  $__int64 sum (intx) { -__int64 s =0, SS =0; -     intxx =x; -      A      while(x) { +s + =Nd[x].s; theSS + =ND[X].SS; -X-=lowest (x); $     } the      the     return(xx+1) * S-SS; the } the  - voidUpdateintXintDelta) { in__int64 tmp = 1LL * x *Delta; the      the      while(x <=N) { AboutNd[x].s + =Delta; theND[X].SS + =tmp; theX + =lowest (x); the     } + } -  the intMain () {BayiIos::sync_with_stdio (false); the #ifndef Online_judge theFreopen ("data.in","R", stdin); -Freopen ("Data.out","W", stdout); -     #endif the      thescanf"%d%d", &n, &q); theRep (I,1, n+1) { thescanf"%d", &a[i]); -Tot[i] = tot[i-1] +A[i]; the     } the      the __int64 ans;94     Charop[4]; the     intL, R, D; the      the      while(q--) {98scanf"%s%d%d", OP, &l, &R); About         if(op[0] =='Q') { -Ans = tot[r]-tot[l-1] + sum (r)-SUM (l1);101printf"%i64d\n", ans);102}Else {103scanf"%d", &d);104 Update (L, d); theUpdate (r+1, -d);106         }107     }108     109 #ifndef Online_judge theprintf"Time =%d.\n", (int) clock ());111     #endif the     113     return 0; the}

Data generators.

1  fromCopyImportdeepcopy2  fromRandomImportRandint, Shuffle3 ImportShutil4 Importstring5 6 7 defGendatain ():8With open ("data.in","W") as Fout:9t = 1Tenbound = 10**3 One         #fout.write ("%d\n"% (t)) A          forTtinchxrange (t): -n = randint (100, 200) -Q = randint (100, 200) theFout.write ("%d%d\n"%(n, q)) -L = [] -              forIinchxrange (n): -x = Randint (1, bound) + l.append (x) -Fout.write (" ". Join (Map (str, L)) +"\ n") +              forIinchxrange (q): Aop = Randint (0, 1) at                 ifop: -L = Randint (1, N) -R =Randint (l, N) -K = Randint (1, bound) -Fout.write ("C%d%d%d\n"%(L, R, K)) -                 Else: inL = Randint (1, N) -R =Randint (l, N) toFout.write ("Q%d%d\n"%(L, R)) +              -                  the defMovdatain (): *Desfilename ="F:\eclipse_prj\workspace\hdoj\data.in" $Shutil.copyfile ("data.in", Desfilename)Panax Notoginseng  -      the if __name__=="__main__": + Gendatain () AMovdatain ()

"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.