Rokua P3374 "template" Tree array 1 (CDQ Division)

Source: Internet
Author: User
Tags stdin

Title Description

Title, you need to perform the following two operations, known as a sequence:

1. Add a number to the X

2. Find out the amount of each number in a range and

Input/output format

Input format:

The first line contains two integers n, M, respectively, indicating the number of numbers and the total number of operations.

The second line contains n space-delimited integers, where the I number represents the initial value of item I of a sequence.

The next m row contains 3 integers for each row, indicating an operation, as follows:

Action 1: Format: 1 x k Meaning: Add x number plus K

Action 2: Format: 2 x y meaning: the and of each number in the output interval [x, y]

Output format:

The output contains several line integers, which is the result of all Operation 2.

Input and Output sample input sample #: Copy
5 51 5 4 2 31 1 32 2 51 3-11 4 22 1 4
Output Example # #: Replication
1416
Description

Time limit: 1000ms,128m

Data size:

For 30% data: n<=8,m<=10

For 70% data: n<=10000,m<=10000

For 100% data: n<=500000,m<=500000

Sample Description:

Therefore, the output results 14, 16

Maintenance of two-dimensional partial order by CDQ

The first dimension is used to sort out

The second dimension is treated by CDQ

Slow to die.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <ctime>5#include <cstdlib>6 using namespacestd;7 #defineLS T[now].ch[0]8 #defineRS T[now].ch[1]9 Const intmaxn=2*1e6+Ten;TenInlineCharNC () One { A     Static CharBuf[maxn],*p1=buf,*p2=buf; -     returnp1==p2&& (p2= (p1=buf) +fread (buf,1, Maxn,stdin), P1==P2)? eof:*p1++; - } theInlineintRead () - { -     CharC=NC ();intx=0, f=1; -      while(c<'0'|| C>'9'){if(c=='-') f=-1; c=NC ();} +      while(c>='0'&&c<='9') {x=x*Ten+c-'0', c=NC ();} -     returnx*F; + } A intn,m; at structnode - { -     intIdx//The first few questions -     intVal//the modified value -     intType//type of Operation -     BOOL operator< (ConstNode &a)Const { in         returnidx==a.idx?type<a.type:idx<A.idx;} - }Q[MAXN]; to intqidx=0;//Number of Operations + intaidx=0;//Number of Queries - intANS[MAXN]; the node TMP[MAXN]; * voidCDQ (intLintR) $ {Panax Notoginseng     if(r-l<=1)return ; -     intMid= (l+r) >>1; CDQ (L,mid); CDQ (mid,r); the     intsum=0; +     intp=l,q=mid,o=0; A      while(p<mid&&q<R) the     { +         if(q[p]<Q[q]) -         { $             if(q[p].type==1) sum+=Q[p].val; $tmp[o++]=q[p++]; -         } -         Else the         { -             if(q[q].type==2) ans[q[q].val]-=sum;Wuyi             Else if(q[q].type==3) ans[q[q].val]+=sum; thetmp[o++]=q[q++]; -         } Wu     } -      while(P<mid) tmp[o++]=q[p++]; About      while(q<R) $     { -         if(q[q].type==2) ans[q[q].val]-=sum; -         Else if(q[q].type==3) ans[q[q].val]+=sum; -tmp[o++]=q[q++]; A     } +      for(intI=0; i<o;i++) q[i+l]=Tmp[i]; the } - intMain () $ { the #ifdef WIN32 theFreopen ("a.in","R", stdin); the     #else the     #endif -N=read (); m=read (); in      for(intI=1; i<=n;i++) the     { theq[qidx].idx=i; AboutQ[qidx].type=1; theQ[qidx].val=read (); + +Qidx; the     } the      for(intI=0; i<m;i++) +     { -         intType=read (); theQ[qidx].type=type;Bayi         if(type==1) Q[qidx].idx=read (), q[qidx].val=read (); the         Else the         { -             intL=read (), r=read (); -q[qidx].idx=l-1; q[qidx].val=aidx;++Qidx; theQ[qidx].type=3; Q[qidx].idx=r; q[qidx].val=aidx;aidx++; the         } the++Qidx; the     } -CDQ (0, QIDX); the      for(intI=0; i<aidx;i++) printf ("%d\n", Ans[i]); the     return 0; the}

Rokua P3374 "template" Tree array 1 (CDQ Division)

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.