Hoj 1867 manager's troubles (tree array)

Source: Internet
Author: User

Hoj 1867 manager's troubles (tree array)

Jerry is the manager of a company's sales department. This company has many chain stores, numbered 1, 2, 3,... Jerry has to pay attention to the number and changes of each chain every day, a very boring job. When there are few chain stores, Jerry prefers to calculate the number of products in the chain stores in the [I, j] range. However, the number of chain stores is increasing sharply, it is difficult for Jerry to get the result because of the large amount of computing.

Input Format
There are multiple input groups for the question. Each group has three integers in the first line: the number of C chain stores, the number of N commands, and the initial number of commodities for each chain store.
Next there are N rows, and each row has a command. Command Format:
0 x y chain stores x Change the commodity quantity value to y, y> 0 increase the commodity quantity, and y <0 decrease
1 I j number of products in a chain store in the [I, j] interval
1 <= I, x, j <1000000 the number of items in a chain store must meet 0 <= a <10000000, C = N = M = 0. The input ends.

Output Format
Output the serial number of each input group. The integer required for the output of the 1 command in a group of inputs. Each group outputs a blank line.

Sample Input

100000 4 40 1 11 4 100 11 31 1 1120 3 01 1 200 3 31 1 200 0 0
Sample output
CASE #1:02CASE #2:01

 

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        
         #include
         #define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8typedef long long ll;#define fre(i,a,b) for(i = a; i 
          
           = a;i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define ssf(n) scanf(%s, n)#define sf(n) scanf(%d, &n)#define sff(a,b) scanf(%d %d, &a, &b)#define sfff(a,b,c) scanf(%d %d %d, &a, &b, &c)#define pf printf#define bug pf(Hi)using namespace std;#define INF 0x3f3f3f3f#define N 1000005ll a[N],c[N];int n,m;int judge(int x){ if(x==0) return 0; if(x==1) return 0; if(x==2) return 1; for(int i=2;i<=(int)sqrt(x)+1;i++) if(x%i==0) return 0; return 1;}inline int lowbit(int x){ return x&(-x);}void update(int x,int va){ while(x<=n) { c[x]+=va; x+=lowbit(x); }}int sum(int x){ int s=0; while(x) { s+=c[x]; x-=lowbit(x); } return s;}int main(){ int i,j,s,ca=0; while(~sfff(n,m,s),n+m+s) { printf(CASE #%d:,++ca); for(i=1;i<=n;i++) a[i]=s; s=judge(s); mem(c,0); if(s) { for(i=1;i<=n;i++) update(i,s); } int op,le,ri; while(m--) { sfff(op,le,ri); if(op==0) { int cur=a[le]; int to=a[le]+ri; a[le]=to; cur=judge(cur); to=judge(to); if(cur==to) continue; if(cur) update(le,-1); else update(le,1); } else { pf(%d,sum(ri)-sum(le-1)); } } printf(); } 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.