【NOIP2016提高A组模拟8.17】Binary
来源:互联网 发布:网游加速器全套源码 编辑:程序博客网 时间:2024/05/05 07:12
//终于把这道@#¥%¥#¥%…#@*&@¥#&的题目切了(ˉ▽ ̄~) 一开始打超多细节打错,改了超久
题目
Input
Sample Input
6 6
8 9 1 13 9 3
1 4 5
2 6 9
1 3 7
2 7 7
1 6 1
2 11 13
Output
Sample Output
45
19
21
Data Constraint
题目の大意
现在我们有一组数
1:把
2:输出
其实就是把题目抄了一遍嘛QAQ
比赛时の想法
以前做过一道类似的题目,也是有一个烦人的+x的,但是比赛的时候并没有仔细想,没时间啊╮(╯_╰)╭
正解
我们可以用权值线段树or树状数组来解决这道问题,我用的是权值线段树(好慢啊)设tree[aa,x]为现在储存的是所有数在二进制情况下的前aa位,值L到值R之间的数有多少个(对于一个确定的x和aa,L和R也是确定的)。这样子建20棵权值线段树。
对于一个修改操作:我们逐位(二进制)在权值线段树中减去原来的数所在的每一个区间,然后再用相同的方法处理改变的数就好了
对于一个询问操作:同样也是逐位(二进制)操作,如果y的这一位是0显然这一位对答案没有贡献,如果是1:我们先假设没有+x这个烦人的操作,设l为当前做到了第l位,那么我们查询的位置就是
那么这一题就搞定啦QAQ
贴代码
保留一部分注释以证我改了超久(懒就直说啦2333)
var tree:array[0..20,0..6000005]of longint; a:array[0..100005]of longint; q:array[0..25]of int64; i,j,k,l,n,o,x,y,t,ss,x1,qq,lo:longint; ans:int64; bz:boolean;function max(x,y:longint):longint;begin if x>y then exit(x) else exit(y);end;procedure change(aa,x,l,r,s:longint);var mid:longint;begin if bz=true then inc(tree[aa,x]) else dec(tree[aa,x]); if l=r then exit; mid:=(l+r) div 2; if s<=mid then change(aa,x*2,l,mid,s) else change(aa,x*2+1,mid+1,r,s);end;procedure find(aa,x,l,r,a,b:longint);var mid:longint;begin if a>b then exit; if (l=a) and (r=b) then ans:=ans+q[aa]*tree[aa,x] else begin mid:=(l+r) div 2; if b<=mid then find(aa,x*2,l,mid,a,b) else if a>mid then find(aa,x*2+1,mid+1,r,a,b) else begin find(aa,x*2,l,mid,a,mid); find(aa,x*2+1,mid+1,r,mid+1,b); end; end;end;procedure init;begin q[0]:=1; bz:=true; for i:=1 to 23 do q[i]:=q[i-1]*2; readln(n,qq); for i:=1 to n do begin read(a[i]); for j:=0 to 19 do change(j,1,0,q[j+1]-1,a[i] mod q[j+1]); end; readln; //for i:=1 to n do maketree(i,1,0,q[i+1]-1);end;begin //assign(input,'t3.in'); reset(input); init; for i:=1 to qq do begin readln(o,x,y); ss:=x; if o=1 then begin l:=0; bz:=false; ss:=a[x]; while l<=19 do begin change(l,1,0,q[l+1]-1,ss mod q[l+1]); a[x]:=a[x] div 2; inc(l); end; a[x]:=y; ss:=y; l:=0; bz:=true; while l<=19 do begin change(l,1,0,q[l+1]-1,ss mod q[l+1]); a[x]:=a[x] div 2; inc(l); end; a[x]:=ss; end else begin l:=0; ans:=0; while l<=19 do begin if y and q[l]=0 then begin //y:=y div 2; inc(l); continue; end; x1:=x mod q[l+1]; find(l,1,0,q[l+1]-1,max(0,q[l]-x1),q[l+1]-1-x1); if x and q[l]>0 then find(l,1,0,q[l+1]-1,q[l]*3-x1,q[l+1]-1); //y:=y div 2; inc(l); end; writeln(ans); end; end; //close(input);end.
- 【NOIP2016提高A组模拟8.17】Binary
- 【NOIP2016提高A组模拟8.17】Binary
- Binary 【NOIP2016提高A组模拟8.17】
- JZOJ 4711. 【NOIP2016提高A组模拟8.17】Binary
- JZOJ 4711 Binary【NOIP2016提高A组模拟8.17】
- JZOJ4711. 【NOIP2016提高A组模拟8.17】Binary
- 【JZOJ4711】【NOIP2016提高A组模拟8.17】Binary
- [jzoj4711]【NOIP2016提高A组模拟8.17】Binary
- Matrix 【NOIP2016提高A组模拟8.17】
- Value【NOIP2016提高A组模拟8.17】
- 【NOIP2016提高A组模拟8.17】Matrix
- 【NOIP2016提高A组模拟8.17】Value
- 【NOIP2016提高A组模拟8.17】Matrix
- 【NOIP2016提高A组模拟8.17】Value
- 【NOIP2016提高A组模拟8.17】(雅礼联考day1)Binary
- jzoj 4711. 【NOIP2016提高A组模拟8.17】Binary 树状数组+位运算
- JZOJ.4710【NOIP2016提高A组模拟8.17】Value
- JZOJ4709. 【NOIP2016提高A组模拟8.17】Matrix
- SAP.Webservice创建.使用SoapUI测试
- cocos2dx- call to OpenGL ES API with no current context(logged once per thread)
- Parallelogram Counting
- apache设置用户名密码登录验证
- Java NIO —— Buffer(缓冲区)
- 【NOIP2016提高A组模拟8.17】Binary
- 关于如何关闭Windows错误报告
- ssh 常用命令,,只是一个链接
- android studio 安装的plugins 插件 重启之后就消失了
- 通知(NSNotification)
- 18 个锻炼编程技能的网站
- MATLAB数学建模(8)-常微分方程求解
- 2016 Multi-University Training Contest 10题解报告
- rxjava和retrofit结合