hdu 4288 Coder(樹形結構-線段樹)

來源:互聯網
上載者:User

標籤:hdu 4288 coder樹形結構-線

<span style="font-family: 'Times New Roman'; font-size: 12px;">Coder</span>
Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3187    Accepted Submission(s): 1258


Problem Description  In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done. 1
  You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
  By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
  1. add x – add the element x to the set;
  2. del x – remove the element x from the set;
  3. sum – find the digest sum of the set. The digest sum should be understood by

  where the set S is written as {a1, a2, ... , ak} satisfying a1 < a2 < a3 < ... < ak 
  Can you complete this task (and be then fired)?
------------------------------------------------------------------------------
1 See http://uncyclopedia.wikia.com/wiki/Algorithm 
Input  There’re several test cases.
  In each test case, the first line contains one integer N ( 1 <= N <= 105 ), the number of operations to process.
  Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
  You may assume that 1 <= x <= 109.
  Please see the sample for detailed format.
  For any “add x” it is guaranteed that x is not currently in the set just before this operation.
  For any “del x” it is guaranteed that x must currently be in the set just before this operation.
  Please process until EOF (End Of File).
 
Output  For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.
 
Sample Input
9add 1add 2add 3add 4add 5sumadd 6del 3sum6add 1add 3add 5add 7add 9sum
 
Sample Output
345HintC++ maybe run faster than G++ in this problem. 這題線段樹有點分治的感覺,下面舉個例子就明白了,看圖:
#include <iostream>#include <cstdio>#include <map>#include <vector>using namespace std;#define ll long longconst int maxn = 100010;struct tree{    int l , r , sum;    ll mod[5];}a[4*maxn];struct OP{    char op;    int x;    OP(char o = 'a' , int a = 0){        op = o , x = a;    }};int N , cnt , value[maxn];map<int , int> mp;vector<OP> operate;void pushup(int k){    a[k].sum = a[2*k].sum+a[2*k+1].sum;    int x = a[2*k].sum%5;    for(int i = 0; i < 5; i++){        a[k].mod[(x+i)%5] = a[2*k].mod[(x+i)%5]+a[2*k+1].mod[i];    }}void build(int l , int r , int k){    a[k].l = l;    a[k].r = r;    a[k].sum = 0;    for(int i = 0; i < 5; i++) a[k].mod[i] = 0;    if(l != r){        int mid = (l+r)/2;        build(l ,mid , 2*k);        build(mid+1 , r , 2*k+1);    }}void update(int l , int r , int k , int c){    if(l <= a[k].l && a[k].r <= r){        if(c < 0) a[k].sum -= 1;        else a[k].sum += 1;        a[k].mod[1] += c;    }else{        int mid = (a[k].l+a[k].r)/2;        if(mid >= r) update(l , r , 2*k , c);        else update(l , r , 2*k+1 , c);        pushup(k);    }}void initial(){    mp.clear();    cnt = 0;    operate.clear();}void readcase(){    char op[10];    int x;    for(int i = 0; i < N; i++){        scanf("%s" , op);        if(op[0] == 'a' || op[0] == 'd'){            scanf("%d" , &x);            operate.push_back(OP(op[0] , x));            mp[x] = 0;        }else operate.push_back(OP(op[0] , -1));    }    for(map<int , int>::iterator it = mp.begin(); it != mp.end(); it++){        it->second = cnt;        value[cnt++] = it->first;    }}void computing(){    build(0 , cnt-1 , 1);    for(int i = 0; i < N; i++){        char op = operate[i].op;        int x = operate[i].x;        if(op == 'a') update(mp[x] , mp[x] , 1 , x);        else if(op == 'd') update(mp[x] , mp[x] , 1 , -1*x);        else{            printf("%I64d\n" , a[1].mod[3]);        }    }}int main(){    while(~scanf("%d" , &N)){        initial();        readcase();        computing();    }    return 0;}


 

hdu 4288 Coder(樹形結構-線段樹)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.