Hoj 1867 manager's troubles (tree-like array)

Source: Internet
Author: User

Jerry is a manager of a company's sales department. This company has a lot of chain stores, numbered ... Every day, Jerry has to pay attention to the number of goods and the changes in each chain, a tedious job. When there were fewer stores, Jerry liked to count the number of items in the chain of stores that numbered in the [I,j] range, but now the number of chain stores has grown sharply and the amount of calculations is large, and Jerry is hard to come by.

Input format
The topic has multiple sets of inputs. The first line of each set of inputs has three integers: number of C chain stores N instruction number M The initial number of items per chain
Then there are n rows, one for each line. The format of the directive is:
0 x y chain x number of items change value for y,y > 0 Increase in number of items, y < 0 decrease
1 I j output number the number of items in the chain store in the [I,j] range
1 <= I, X, J < 1000000 stores in the number of items a satisfies 0 <= a < 10000000,c = N = M = 0 Flag Input End

Output format
For each set of inputs, output its ordinal number. An integer that is required for 1 instruction output in a set of inputs. Prints a blank line after each set of outputs.

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


Title Link: http://acm.hit.edu.cn/hoj/problem/view?id=1867


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #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 <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) MEMSE T ((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\n") 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&AMP; (-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:\n", ++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\n", Sum (RI)-sum (le-1));    }} printf ("\ n"); } return 0;}





Hoj 1867 manager's troubles (tree-like array)

Related Article

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.