B2568 比特集合 树状数组
啊啊啊,跳题坑死人。抽了一道国集的题,自己瞎编了一个算法,好像过不了而半途而废。转去看题解,发现用二维树状数组维护一下,偏移量我倒是想对了,但是维护的东西和我的完全不一样。还是有很大差距啊。。。
吐槽一个事,谁能给我讲讲位运算的优先级?
#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 比特集合 树状数组的更多相关文章
- UVALive 6911---Double Swords(贪心+树状数组(或集合))
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Solution -「树状数组」 题目集合
T1 冒泡排序 题目描述 clj 想起当年自己刚学冒泡排序时的经历,不禁思绪万千 当年,clj 的冒泡排序(伪)代码是这样的: flag=false while (not flag): flag=tr ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 【BZOJ-1103】大都市meg 树状数组 + DFS序
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2009 Solved: 1056[Submit][Sta ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- 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]< ...
- bzoj 3594: [Scoi2014]方伯伯的玉米田 dp树状数组优化
3594: [Scoi2014]方伯伯的玉米田 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 314 Solved: 132[Submit][Sta ...
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 题意:给你一个集合,让你求递增子序列有多少个,和树状数组求逆序对差不多,不过数据比较大,要离散化 ...
随机推荐
- db2 jdbc连接字符串中 指定currentSchema
场景:连接DB2数据库的,jdbc的连接字符串中没有给当前的数据源用户指定默认的schema,而当前的数据源用户下可能有多个schema,则会使用数据源用户默认的schema. 例如:admin用户的 ...
- java数据类型和码表、转义字符
类型名称 字节空间 范围 整数型 byte 1 -27到27-1 或者 -128到127 short 2 -215到215-1 int 4 -231到231-1 long 8 ...
- webpack-dev-middleware 与 webpack-hot-middlware
dev-middleware: live reload的实现: 思考一下我們要如何更新(live reload)呢? 當然是需要取得 webpack 編好的資料啊,於是就需要在從 request 到 ...
- Django REST framework - 权限和限制
目录 Django REST framework 权限和限制 (你能干什么) 设置权限的方法 案例 第一步: 定义一个权限类 第二步: 使用 视图级别 全局级别设置 --- 限制 (你一分钟能干多少次 ...
- Python基础-List找重复数
请从L=[1,10,20,50,20,20,1]中找出重复数. L=[1,10,20,50,20,20,1] L1=[] for i in L: if(L.count(i)>1): L1.app ...
- 【[Offer收割]编程练习赛12 C】矩形分割
[题目链接]:http://hihocoder.com/problemset/problem/1495 [题意] [题解] 把每个方块都再分成3*3的小块; 这样; 对于一个方块来说 如果是'\' 则 ...
- qwb与整数对
qwb与整数对 Time Limit: 1 Sec Memory Limit: 128 MB Description qwb又遇到了一道数学难题,你能帮助他吗? 给出两个整数n和m,请统计满足0&l ...
- Jzzhu and Numbers
Jzzhu and Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- SpringBoot多数据源改造(二)
在上一篇的内容中,主要介绍了spring boot项目的多数据源改造的涉及的基本配置及改动.在spring项目中,常用Mybatis做ORM操作数据库,并且分页操作是避免不了的. 因此,这一篇主要介绍 ...