Binary
来源:互联网 发布:python 调用sap 编辑:程序博客网 时间:2024/05/21 11:22
Description
Data Constraint
分析
这道题有点像[scoi2016]美味,思路也是差不多。
因为题目是and,就不能用trie去做,不过可以分成二进制20位,每一位分别查询答案。
对于第i位,假设x=0,那么查询的区间是所有第i位为1的区间。如果我们把a对
然后考虑x>0的情况,会发现这个区间向左移了x位,也就是区间
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100005,maxm=1048599;typedef long long LL;int n,q,a[maxn],p[maxn],s[20][maxm];LL ans;char c;int read(){ for (c=getchar();c<'0' || c>'9';c=getchar()); int x=c-48; for (c=getchar();c>='0' && c<='9';c=getchar()) x=x*10+c-48; return x;}int lowbit(int x){ return x&(-x);}void change(int t,int x,int y){ for (;x<=p[t+1];x+=lowbit(x)) s[t][x]+=y;}int get(int t,int x){ int sum=0; for (;x;x-=lowbit(x)) sum+=s[t][x]; return sum;}int main(){ n=read(); q=read(); p[0]=1; for (int j=1;j<=20;j++) p[j]=p[j-1]<<1; for (int i=1;i<=n;i++) { a[i]=read(); for (int j=0;j<20;j++) change(j,a[i]%p[j+1]+1,1); } while (q--) { int typ=read(),x=read(),y=read(); if (typ==1) { for (int j=0;j<20;j++) change(j,a[x]%p[j+1]+1,-1); a[x]=y; for (int j=0;j<20;j++) change(j,y%p[j+1]+1,1); }else { ans=0; for (int j=0;j<20;j++) if ((y&p[j])>0) { int tmp=x%p[j+1]; ans+=get(j,p[j+1]-tmp)*(LL)p[j]; if (p[j]>tmp) ans-=get(j,p[j]-tmp)*(LL)p[j]; else ans+=(n-get(j,p[j+1]+p[j]-tmp))*(LL)p[j]; } printf("%lld\n",ans); } } return 0;}
0 0
- Binary
- Binary
- Binary
- Binary
- binary
- Binary
- binary
- binary
- Binary search
- Binary tree
- Binary Search
- binary.c
- Binary Search
- binary search
- Binary Tree
- Binary Path
- Binary Tree
- binary search
- [Codeforces 272E] Dima and Horses (图染色构造)
- 产品环境中安装Cassandra 3.7
- 基于Windows在VirtualBox部署CentOS 7
- struct和typedef struct
- unity 优化大全
- Binary
- 移除0元素
- Makefile Learing (下)
- ZZULIOJ 1918: G 【二分图匹配】
- 微信内置浏览器私有接口WeixinJSBridge介绍
- HDU 3308 LCIS(线段树)
- 关于数组指针,指针数组,双重数组,二维矩阵,字符串数组,双重字符指针的理解
- 蛇形数组
- ruby 2.1.1 rails 4.2.0 安装gemset 却少 mysql ,command line tool for mac osx