CF C. Inna and Candy Boxes

Source: Internet
Author: User

Test instructions: give you a string of length n containing only 1 and 0, W inquiry, each query input l,r, in [L,r] in the l+k-1, l+2*k-1 、...... R position must be 1, if not 1, into 1, recorded as an operation, the other place must be 0, Not 0 of the place to become 1, also recorded as an operation, and finally asked in the interval [l,r] at least several operations.

Idea: You can use a tree-like array to record how many 1 to the right of a place, and then preprocess out from 1, 2,. K for the beginning of the position of the i+c*k-1 in front of how many 1, the final answer is to take away the 1+ added 1, take away the number of 1 is equal to the number of existing 1-position the correct number of 1. The number of 1 added is equal to the total number of items to be added-1 of the correct number of positions.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 1000105 using namespacestd;6 7 intn,k,w;8 CharSTR[MAXN];9 intA[MAXN];Ten intC[MAXN]; One intDP[MAXN]; A intSUM[MAXN]; -  - intLowbit (intx) the { -     returnx&-x; - } -  + voidInsertintXintd) - { +      while(x<MAXN) A     { atc[x]+=D; -x+=lowbit (x); -     } - } -  - intGetsum (intx) in { -     intans=0; to      while(x>0) +     { -ans+=C[x]; thex-=lowbit (x); *     } $     returnans;Panax Notoginseng } -  the  + intMain () A { the      while(SCANF ("%d%d%d", &n,&k,&w)! =EOF) +     { -scanf"%s", str+1); $memset (SUM,0,sizeof(sum)); $Memset (A,0,sizeof(a)); -Memset (c,0,sizeof(c)); -           for(intI=1; i<=n; i++) the          { -             if(str[i]=='1')Wuyi             { theInsert (I,1); -             } Wu          } -           for(intI=1; i<=k; i++) About          { $              intCnt=0; -               for(intC=1; i+c*k-1<=n; C++) -              { -cnt+= (str[i+c*k-1]-'0'); Adp[i+c*k-1]=CNT; +              } the          } -           for(intI=1; i<=w; i++) $          { the              intL,r; the              intcn1=0; thescanf"%d%d",&l,&R); the              intNum= (r-l+1)/K; -Ans+=getsum (R)-getsum (l1); inans-=dp[r]-dp[l-1]; theans+=num; theans-=dp[r]-dp[l-1]; Aboutprintf"%d\n", ans); the          } the     } the     return 0; +}
View Code

CF C. Inna and Candy Boxes

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.