HDU 4288 線段樹

題目鏈接

Coder

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3667 Accepted Submission(s): 1427

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

9
add 1
add 2
add 3
add 4
add 5
sum
add 6
del 3
sum
6
add 1
add 3
add 5
add 7
add 9
sum

Sample Output

3
4
5

做法

  • 先將數據離散化,離線處理數據
  • 線段樹的每個節點都存儲在這個區間內sum[5],代表該區間的所有情況,外加一個cnt表示該區間元素個數用於更新sum[5]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace std ;

typedef long long LL ;
const int N = 1e5 + 11 ;

int cnt[N<<2] ; LL sum[N<<2][5] ;
int oper[N][2] ; int num[N] , n_num ;
int n , m ;
int x , y ;

void up(int u) {
    int l = (u<<1) ;
    int r = (u<<1|1) ;
    cnt[u] = cnt[l] + cnt[r] ;
    for(int i = 0 ; i < 5 ; ++i) sum[u][i] = sum[l][i] ;
    for(int i = 0 ; i < 5 ; ++i) {
        sum[u][(cnt[l]+i)%5] += sum[r][i] ;
    }
}

void build(int u , int l , int r) {
    if(l == r) {
        for(int i = 0 ; i < 5 ; ++i) sum[u][i] = 0 ;
        cnt[u] = 0 ;
        return ;
    }
    int mid = (l+r)>>1 ;
    build(u<<1 , l , mid) ;
    build(u<<1|1 , mid+1 , r) ;
    up(u) ;
}

void update(int u , int l , int r , int d) {
    if(x <= l && r <= y) {
        if(d == -1) {
            for(int i = 0 ; i < 5 ; ++i) sum[u][i] = 0 ;
            cnt[u] = 0 ;
        }else {
            sum[u][1] = d ;
            cnt[u] = 1 ;
        }
        return ;
    }
    int mid = (l+r)>>1 ;
    if(x <= mid) update(u<<1 , l , mid , d) ;
    if(y > mid) update(u<<1|1 , mid+1 , r ,d) ;
    up(u) ;
}

int main() {//freopen("data.in" , "r" , stdin);
    char str[5] ;
    int a ;
    while(scanf("%d" ,&n)==1) {
        n_num = 0 ;
        for(int i = 0 ; i < n ; ++i) {
            scanf("%s", str) ;
            if(str[0] == 'a') {
                oper[i][0] = 0 ;
                scanf("%d" ,&oper[i][1]) ;
                num[n_num++] = oper[i][1] ;
            }else if(str[0] == 'd') {
                oper[i][0] = 1 ;
                scanf("%d" ,&oper[i][1]) ;
                num[n_num++] = oper[i][1] ;
            }else {
                oper[i][0] = 2 ;
            }
        }
        sort(num , num+n_num) ;
        n_num = unique(num , num+n_num)-num ;
        build(1 , 1 , n_num) ;
        for(int i = 0 ; i < n ; ++i) {
            x = y = lower_bound(num , num + n_num , oper[i][1])-num+1 ;
            if(oper[i][0] == 0) {
                update(1 , 1 , n_num , oper[i][1]) ;
            }else if(oper[i][0] == 1) {
                update(1 , 1 , n_num , -1) ;
            }else {
                printf("%I64d\n" , sum[1][3]) ;
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章