题目:

BZOJ2762

分析:

加入的不等式分三种情况

当\(a>0\),可以变成\(x>\lfloor \frac{c-b}{a}\rfloor\)

当\(a=0\),若\(b>c\)则恒成立,否则恒不成立

当\(a<0\),可以变成\(x<\lceil \frac{c-b}{a}\rceil\)

对于\(a=0\),用一个变量\(sum\)记一下当前有多少不等式恒成立,删除的时候注意要维护\(sum\)。

对于\(a\neq0\),可以开两个权值树状数组\(greater\)和\(less\)记录。当加入\(a>0\)时,令\(x=\lfloor \frac{c-b}{a}\rfloor\),给\(greater\)的\(x\)位置加\(1\),查询时查\([0,k)\)区间的和。\(a<0\)时在\(less\)上类似。

对于删除操作,在树状数组上删除该不等式贡献的值即可。注意要记录已删除的不等式防止重复删除。

代码:

这题思路简单,但是代码细节比较多……

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
namespace zyt
{
const int DELETED = 1e9, P = 1e6 + 10, OPT = 1e5 + 10, N = P * 2;
class Tree_Array
{
private:
int data[N];
inline int lowbit(const int x)
{
return x & -x;
}
public:
Tree_Array()
{
memset(data, 0, sizeof(data));
}
inline void add(int a, const int x)
{
while (a < N)
data[a] += x, a += lowbit(a);
}
inline int query(int a)
{
int ans = 0;
while (a > 0)
ans += data[a], a -= lowbit(a);
return ans;
}
}less, greater;
int n;
pair<int, int> opt[OPT];
int cnt, sum;
int work()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
if (s == "Add")
{
int a, b, c, x;
cin >> a >> b >> c;
if (a == 0)
{
opt[++cnt] = make_pair(0, (bool)(b > c));
if (b > c)
sum++;
}
else if (a < 0)
{
x = (int)(ceil((c - b) / (double)a) + P);
if (x < 1)
x = 1;
if (x >= N)
x = N - 1;
opt[++cnt] = make_pair(-1, x);
less.add(x, 1);
}
else
{
x = (int)(floor((c - b) / (double)a) + P);
if (x < 1)
x = 1;
if (x >= N)
x = N - 1;
opt[++cnt] = make_pair(1, x);
greater.add(x, 1);
}
}
else if (s == "Del")
{
int a;
cin >> a;
if (opt[a].first == 0)
sum -= (opt[a].second == 1);
else if (opt[a].second != DELETED)
{
if (opt[a].first == -1)
less.add(opt[a].second, -1);
else
greater.add(opt[a].second, -1);
}
opt[a].second = DELETED;
}
else
{
int a;
cin >> a;
a += P;
cout << sum + greater.query(a - 1) + less.query(N - 1) - less.query(a) << '\n';
}
}
return 0;
}
}
int main()
{
return zyt::work();
}

【BZOJ2762】[JLOI2011]不等式组(树状数组)的更多相关文章

  1. 【BZOJ2762】[JLOI2011]不等式组 树状数组

    [BZOJ2762][JLOI2011]不等式组 Description 旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了, ...

  2. bzoj 2762: [JLOI2011]不等式组——树状数组

    旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了,所以旺喵想挑战旺汪.旺喵给出一组一元不等式,并给出一个数值 .旺汪需要回答 ...

  3. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  4. NOIP2013 提高组day2 2 花匠 动规 找拐点 树状数组

    花匠 描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花排列得比较别致. 具体 ...

  5. 【洛谷】NOIP提高组模拟赛Day2【动态开节点/树状数组】【双头链表模拟】

    U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...

  6. 【做题记录】 [JLOI2011]不等式组

    P5482 [JLOI2011]不等式组 超烦人的细节题!(本人调了两天 QAQ ) 这里介绍一种只用到一只树状数组的写法(离线). 树状数组的下标是:所有可能出现的数据进行离散化之后的值. 其含义为 ...

  7. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

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

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

  9. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

随机推荐

  1. svn汉化包安装无效的解决办法

    下载svn汉化包要和对应的svn客户端版本对应,否则安装无效, 在安装前要想将svn安装目录下的languages目录下的文件全部删除 还有一点要注意的是 汉化包安装要放在svn安装目录下进行安装,它 ...

  2. Greek symbols --Latex

    $\propto$     \propto $\infty$   \infty $\ne$   \ne $\approx$     \approx $\sim$ :    \sim  --- same ...

  3. 2.8 补充:shell脚本执行方法

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限.   方法一:切换到shell脚本 ...

  4. JavaSE 学习笔记之网络编程(二十三)

    端口: 物理端口: 逻辑端口:用于标识进程的逻辑地址,不同进程的标识:有效端口:0~65535,其中0~1024系统使用或保留端口. java 中ip对象:InetAddress. import ja ...

  5. 文件处理: read、readline、 readlines()

    假设a.txt的内容如下所示: Hello Welcome What is the fuck.. 1. read([size])方法 read([size])方法:从文件当前位置起读取size个字节, ...

  6. MYSQL常用的性能指标

    (1) QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show  global status like 'Questio ...

  7. mysql模糊查询语句

    select * from tbl_actor where first_char like 'p%' order by first_char;

  8. git SSL certificate problem: unable to get local issuer certificate

    cmd 命令行中输入  git config --global http.sslVerify false 之后再进行操作

  9. Redis是单线程的

    Redis是单线程的 学习了: http://blog.csdn.net/liupeng_qwert/article/details/77263187 https://www.cnblogs.com/ ...

  10. 在Java中按字节获得字符串长度的三种方法

    转载:http://www.blogjava.net/nokiaguy/archive/2010/04/11/317982.html 由于Java是基于Unicode编码的,因此,一个汉字的长度为1, ...