设为首页 加入收藏

TOP

HDU-4288-Coder(线段树)
2015-07-20 17:49:33 来源: 作者: 【 】 浏览:1
Tags:HDU-4288-Coder 线段
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 {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3ViPiA8IC4uLiA8IGE8c3ViPms8L3N1Yj4gPGJyPgqhoaGhQ2FuIHlvdSBjb21wbGV0ZSB0aGlzIHRhc2sgKGFuZCBiZSB0aGVuIGZpcmVkKT88YnI+Ci0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTxicj4KPHN1cD4xPC9zdXA+IFNlZSBodHRwOi8vdW5jeWNsb3BlZGlhLndpa2lhLmNvbS93aWtpL0FsZ29yaXRobQoKIAo8YnI+CgpJbnB1dAoKoaGhoVRoZXJloa9yZSBzZXZlcmFsIHRlc3QgY2FzZXMuPGJyPgqhoaGhSW4gZWFjaCB0ZXN0IGNhc2UsIHRoZSBmaXJzdCBsaW5lIGNvbnRhaW5zIG9uZSBpbnRlZ2VyIE4gKCAxIDw9IE4gPD0gMTA8c3VwPjU8L3N1cD4gKSwgdGhlIG51bWJlciBvZiBvcGVyYXRpb25zIHRvIHByb2Nlc3MuPGJyPgqhoaGhVGhlbiBmb2xsb3dpbmcgaXMgbiBsaW5lcywgZWFjaCBvbmUgY29udGFpbmluZyBvbmUgb2YgdGhyZWUgb3BlcmF0aW9uczogobBhZGQgeKGxIG9yIKGwZGVsIHihsSBvciChsHN1baGxLjxicj4KoaGhoVlvdSBtYXkgYXNzdW1lIHRoYXQgMSA8PSB4IDw9IDEwPHN1cD45PC9zdXA+Ljxicj4KoaGhoVBsZWFzZSBzZWUgdGhlIHNhbXBsZSBmb3IgZGV0YWlsZWQgZm9ybWF0Ljxicj4KoaGhoUZvciBhbnkgobBhZGQgeKGxIGl0IGlzIGd1YXJhbnRlZWQgdGhhdCB4IGlzIG5vdCBjdXJyZW50bHkgaW4gdGhlIHNldCBqdXN0IGJlZm9yZSB0aGlzIG9wZXJhdGlvbi48YnI+CqGhoaFGb3IgYW55IKGwZGVsIHihsSBpdCBpcyBndWFyYW50ZWVkIHRoYXQgeCBtdXN0IGN1cnJlbnRseSBiZSBpbiB0aGUgc2V0IGp1c3QgYmVmb3JlIHRoaXMgb3BlcmF0aW9uLjxicj4KoaGhoVBsZWFzZSBwcm9jZXNzIHVudGlsIEVPRiAoRW5kIE9mIEZpbGUpLjxicj4KCgogCjxicj4KCk91dHB1dAoKoaGhoUZvciBlYWNoIG9wZXJhdGlvbiChsHN1baGxIHBsZWFzZSBwcmludCBvbmUgbGluZSBjb250YWluaW5nIGV4YWN0bHkgb25lIGludGVnZXIgZGVub3RpbmcgdGhlIGRpZ2VzdCBzdW0gb2YgdGhlIGN1cnJlbnQgc2V0LiBQcmludCAwIGlmIHRoZSBzZXQgaXMgZW1wdHkuPGJyPgoKCiAKPGJyPgoKU2FtcGxlIElucHV0Cgo8cHJlIGNsYXNzPQ=="brush:java;">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
HintC++ maybe run faster than G++ in this problem. 

Source 2012 ACM/ICPC Asia Regional Chengdu Online
思路:用sum[400005][5] 来保存对应区间的 下标模5为i的数的和,剩下的就是单点更新和根据左右儿子节点的数的数量来维护sum。
#include 
   
    
#include 
    
      #include 
      #include 
      
        using namespace std; struct S{ char op[5]; int x,id; }e[100005]; bool cmpval(S a,S b) { if(a.x==b.x) return a.op[0]
       
        >1; build(idx<<1,s,mid); build(idx<<1|1,mid+1,e); } num[idx]=0; for(int i=0;i<5;i++) sum[idx][i]=0; } void update(int idx,int s,int e,int pos,int flag) { num[idx]+=flag; if(s==e) { sum[idx][1]+=val[s]*flag; return; } int mid=(s+e)>>1; if(pos<=mid) update(idx<<1,s,mid,pos,flag); else update(idx<<1|1,mid+1,e,pos,flag); for(int i=0;i<5;i++) sum[idx][i]=sum[idx<<1][i]+sum[idx<<1|1][i-num[idx<<1]%5>=0?i-num[idx<<1]%5:i-num[idx<<1]%5+5];//更新对应区间内下标为模5等于i数的和 } long long query(int idx,int s,int e,int mod) { return sum[idx<<1][mod]+sum[idx<<1|1][mod-num[idx<<1]%5>=0?mod-num[idx<<1]%5:mod-num[idx<<1]%5+5]; } int main() { int i; long long ans; while(~scanf("%d",&n)) { map
        
         mp;//用于离散化过程中判重,有可能存在先add a,再del a,再 add a的情况 for(i=0;i
         
          

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2352 && HDU 1541 St.. 下一篇HDOJ 3516 Tree Construction

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)