hdu 4288 Coder(线段树)

来源:互联网 发布:wow 1.12数据库 编辑:程序博客网 时间:2024/06/04 21:17

Coder

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


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
34

5

solution:

首先我们将所有的数字读入然后通过去重得到要操作的数字,然后对于每一个节点维护sum[5]和cnt分别表示5个位置的和以及个数,那么对于一个父亲节点,左孩子节点对应的5个位置的和和父亲节点一样,但对于有孩子来说不同,设左孩子含有的有效节点个数为cnt,那么右孩子中第x个位置%5在父亲中是第i个 那么有(cnt+x)%5==i,解得x=(i-cnt)%5

#include<cstdio>#include<algorithm>using namespace std;#define ls t<<1#define rs (t<<1)|1const int maxn = 1e5 + 200;#define ll long longll a[maxn];struct node{    char op[5];    ll x;}query[maxn];struct Node{    ll sum[5];    int l, r, cnt;}tree[maxn*4];void build(int l, int r, int t){    tree[t].l = l;    tree[t].r = r;    tree[t].cnt = 0;    for (int i = 0; i < 5; i++)        tree[t].sum[i] = 0;    if (l == r)return;    int mid = (l + r) / 2;    build(l, mid, ls);    build(mid + 1,r,rs);}void update(int pos, long long val, int cnt,int t){    tree[t].cnt += cnt;    if (tree[t].l == tree[t].r)    {        tree[t].sum[0] += val;        return;    }    int mid = (tree[t].l + tree[t].r) / 2;    if (pos <= mid)update(pos, val, cnt, ls);    else update(pos, val, cnt, rs);    for (int i = 0; i < 5; i++)        tree[t].sum[i] = tree[ls].sum[i] + tree[rs].sum[((i-tree[ls].cnt+5)%5+5)% 5];}int main(){    int n,pos;    while (~scanf("%d", &n))    {        int cnt = 1;        for (int i = 0; i < n; i++)        {            scanf("%s", query[i].op);            if (query[i].op[0] != 's')            {                scanf("%I64d", &query[i].x);                a[cnt++] = query[i].x;            }        }        sort(a+1, a + cnt);        cnt = unique(a + 1, a + cnt) - a - 1;        build(1, cnt, 1);        for (int i = 0; i < n; i++)        {            if (query[i].op[0] == 's')                printf("%I64d\n", tree[1].sum[2]);            else {                pos = lower_bound(a + 1, a + cnt, query[i].x) - a;                if (query[i].op[0] == 'a')update(pos, query[i].x, 1,1);                else update(pos, -query[i].x,-1, 1);            }        }    }    return 0;}


0 0
原创粉丝点击