hdu 5975---Aninteresting game(树状数组)
2018-06-17 22:28:06来源:未知 阅读 ()
题目链接
When we add i,we must create a new set, and put iinto it.And meanwhile we have to bring [i-lowbit(i)+1,i-1] from their original sets, and put them into the new set,too.When we put one integer into a set,it costs us one unit physical strength. But bringing integer from old set does not cost any physical strength.
After we add 1,2...n,we have q queries now.There are two different kinds of query:
1 L R:query the cost of strength after we add all of [L,R](1≤L≤R≤n)
2 x:query the units of strength we cost for putting x(1≤x≤n) into some sets.
For each case,the first line contains two integers n and q.Then q lines follow.Each line contains one query.The form of query has been shown above.
n≤10^18,q≤10^5
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; typedef long long LL; LL lowbit(LL x) { return x&(-x); } LL query(LL x,LL n) { LL ans=0; while(x<=n) { ans++; x+=lowbit(x); } return ans; } LL cal(LL x) { LL ans=0; LL tmp=1; for(LL i=0; tmp<=x; i++) ans+=(x/(tmp)-x/(tmp<<1))*tmp,tmp<<=1; return ans; } int main() { LL n,q; while(scanf("%lld%lld",&n,&q)!=EOF) { while(q--) { int op; scanf("%d",&op); if(op==1) { LL x,y; scanf("%lld%lld",&x,&y); LL ans=cal(y)-cal(x-1); printf("%lld\n",ans); } else { LL x; scanf("%lld",&x); LL ans=query(x,n); printf("%lld\n",ans); } } } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- CF1215DTicketGame——(博弈) 2020-04-25
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- AtCoder agc007_d Shik and Game 2020-02-08
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash