啊啊啊,跳题坑死人。抽了一道国集的题,自己瞎编了一个算法,好像过不了而半途而废。转去看题解,发现用二维树状数组维护一下,偏移量我倒是想对了,但是维护的东西和我的完全不一样。还是有很大差距啊。。。

题解链接

吐槽一个事,谁能给我讲讲位运算的优先级?

#include<iostream>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<bitset>
#include<set>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
#define MAXN 1010
#define MAXM 1010
#define ll long long
#define eps 1e-8
#define MOD 1000000007
#define INF 1000000000
#define lb(x) x & -x
int m;
int N=<<;
char o[MAXN];
int c[][];
int ch;
map<int,int>h;
void change(int *c,int x,int y)
{
for(; x <= N; x += lb(x))
{
c[x] += y;
}
}
int ask(int *c,int x)
{
int re=;
for(; x; x -= lb(x))
{
re += c[x];
}
return re;
}
int main()
{
int i;
int x;
scanf("%d",&m);
while(m--)
{
scanf("%s",o);
if(o[] == 'I')
{
scanf("%d",&x);
h[x - ch]++; //ch为偏移量
for(i = ; i <= ;i++)
{
change(c[i],(x - ch & (( << i) - )) + ,);//维护树状数组
}
}
if(o[] == 'D')
{
scanf("%d",&x);
for(i = ; i <= ; i++)
{
change(c[i],(x - ch & (( << i) - )) + ,-h[x - ch]);//正常的删除操作
}
h[x - ch] = ;
}
if(o[] == 'A')
{
scanf("%d",&x);
ch += x;
}
if(o[] == 'Q')
{
scanf("%d",&x);
int ans = ;
ans += ask(c[x + ],min(max(( << x + ) - (ch & (( << x + ) - )),), << x + ));
ans -= ask(c[x + ],min(max(( << x) - (ch & (( << x + ) - )),), << x + ));
//这里最不好理解,前两句是统计没有进位的情况
//下两句统计进位的情况,有点复杂
//这块理解不了可以去链接看一眼 (其实我也不太懂)
ans += ask(c[x + ],min(max(( << x + )-(ch & (( << x + ) - )),), << x + ));
ans -= ask(c[x + ],min(max(( << x) + ( << x + )-(ch & (( << x + ) - )),), << x + ));
printf("%d\n",ans);
}
}
return ;
}
/*
8
INS 1
QBIT 0
ADD 1
QBIT 0
QBIT 1
DEL 2
INS 1
QBIT 1
*/

题干:

Description
  比特集合是一种抽象数据类型(Abstract Data Type) ,其包含一个集合S,并支持如下几种操作:
  INS M : 将元素 M 插入到集合S中;
  DEL M : 将集合S中所有等于 M 的元素删除;
  ADD M : 将集合S中的所有元素都增加数值M ;
  QBIT k : 查询集合中有多少个元素满足其二进制的第 k位为 。
  初始时,集合S为空集。请实现一个比特集合,并对于所有的QBIT操作输出相应的答案。
Input
  输入第一行包含一个正整数N,表示操作的数目。
  接下来N行,每行为一个操作,格式见问题描述。
Output
  对于每一个QBIT操作,输出一行,表示相应的答案。
Sample Input INS QBIT ADD QBIT QBIT DEL INS QBIT Sample Output HINT 数据规模和约定   时间限制2s。   对于30%的数据, ≤ N ≤ 。   对于100%的数据, ≤ N ≤ ;QBIT操作中的k满足, ≤ k < 。INS/DEL操作中,满足0 ≤ M ≤ ^;ADD操作中, 满足0 ≤ M ≤ 。 注意   注意集合S可以包含多个重复元素。 Source 2012国家集训队Round day4

我的凉凉代码(都没写完):

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
priority_queue <int> qu;
int cha[],a[];
int tot[],n,x,q = ;
int k[];
char s[];
void ins(int x)
{
int len = ,ok = ;
clean(k);
while(x != )
{
if(x % == )
{
tot[len]++;
k[len] = ;
if(k[len - ] == )
cha[len]++;
}
x /= ;
len++;
}
}
void add(int x)
{
q += x; }
int main()
{
read(n);
duke(i,,n)
{
scanf("%s",s);
if(s[] == 'I')
{
read(x);
ins(x);
qu.push(x - q);
}
else if(s[] == 'A')
{
read(x);
add(x);
}
else if(s[] == 'D')
{
read(x);
del(x);
}
else
{
read(x);
qbit(x);
}
}
}

正解(不是我写的,但是我改了一下,加了点注释)

B2568 比特集合 树状数组的更多相关文章

  1. UVALive 6911---Double Swords(贪心+树状数组(或集合))

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. Solution -「树状数组」 题目集合

    T1 冒泡排序 题目描述 clj 想起当年自己刚学冒泡排序时的经历,不禁思绪万千 当年,clj 的冒泡排序(伪)代码是这样的: flag=false while (not flag): flag=tr ...

  3. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  4. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  5. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  6. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  7. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  8. bzoj 3594: [Scoi2014]方伯伯的玉米田 dp树状数组优化

    3594: [Scoi2014]方伯伯的玉米田 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 314  Solved: 132[Submit][Sta ...

  9. hdu_2227_Find the nondecreasing subsequences_树状数组,离散化

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 题意:给你一个集合,让你求递增子序列有多少个,和树状数组求逆序对差不多,不过数据比较大,要离散化 ...

随机推荐

  1. UI开发模式对比:JSP、Android、Flex

    前一篇文章分析了Java平台下不同类型WEB框架对开发模式的影响,多数Java领域的WEB框架都是聚焦于服务端MVC的实现,这些框架对View的支持,通常是基于标准的JSP或类似JSP的模板技术如Fr ...

  2. vue iView 打包后 字体图标不显示

    问题描述: 今天webpack打包后发现iView 字体图标不显示 解决方案: build/webpack.prod.conf.js 这个文件里面 module: { rules: utils.sty ...

  3. Caffe FCN:可视化featureMaps和Weights(C++)、获取FCN结果

    为何不使用C++版本FCN获取最后的分割掩模,何必要使用python呢!因此需要获取网络最后层的featureMaps,featureMaps的结果直接对应了segmentation的最终结果,可以直 ...

  4. PowerDesigner16逆向工程生成PDM列注释(My Sql5.0模版)

    一.编辑当前DataBase 选择DataBase——>edit Current DBMS...弹出如下对话框:  如上图,先解释一下: 根据红颜色框从上往下解释一下. 第一个红框是对应的修改的 ...

  5. Windows如何正确的修改administrator用户名

    无论是服务器还是电脑.修改默认的administrator的用户总是好的.话不多少.直接上图 1.win+r  输入:gpedit.msc 2.按照我下图的圈圈找到重命名系统管理员账户并自定义 3.重 ...

  6. 最新 Xilinx vivado IP许可申请

    xilinx的fpga使用vivado开发,zynq系列fpga的SOC开发成为主流,加快fpga开发,也进一步提高了fpga开发的灵活性. xilinx提供很多ip核供开发者直接使用,开发快捷方便, ...

  7. Heaters (codeforces 1066B)

    贪心题 策略 在最左边设置一个array 0,每一次从右往左,如果有heater的话就寻找heater左边界是不是小于等于目前的上一个heater的右边界,如果没有一个这样的,那么就直接输出-1 代码 ...

  8. C#学习笔记_09_构造方法/函数

    09_构造方法/函数 代码案例 作用:构造函数主要是用来创建对象时为对象赋初值来初始化对象:总与new运算符一起使用在创建对象的语句中,例如A a=new A(); 特点: 构造函数具有和类一样的名称 ...

  9. BZOJ 1601 USACO 2008 Oct. 灌水

    [Description] Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水 ...

  10. ZOJ 5579 Stean

    Stean Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge Tom is good at making stea ...