题目:

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. 一:安装centos 7最小编程环境 xfce桌面

    1, u盘制作安装盘------------------------------------------------------安装时, table或者e进入编辑选项    如果不知道你的u盘的盘符 ...

  2. 恶补---bell数

    定义 bell数即一个集合划分的数目 示例 前几项的bell数列为 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975 ,... 求值方法 1.bell ...

  3. [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

    传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...

  4. 初识iBatis

    在JAVA EE应用程序中,持久层框架常用的有:Hibernate和IBATIS(或MyBatis),Hibernate是全自动的,IBatis是半自动的. IBatis的主要的作用是把SQL语句从我 ...

  5. [bzoj 1025][SCOI2009]游戏(DP)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1025 分析:首先这个问题等价于A1+A2+……Ak=n,求lcm(A1,A2,……,Ak)的种 ...

  6. Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率

    安装: sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor sudo apt-get update sudo apt-get in ...

  7. systemtap --diskio

    http://blog.163.com/digoal@126/blog/static/1638770402013101993142404

  8. Mybatis在Spring环境下的启动顺序

    主要看三个类: mybatis-spring-1.2.2.jar包 -> org.mybatis.spring.SqlSessionFactoryBean mybatis-3.2.6.jar包 ...

  9. Cocos2d-x 3.x 图形学渲染系列十一

    笔者介绍:姜雪伟.IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  10. 1. 数组小挪移CyclicRotation Rotate an array to the right by a given number of steps.

    数组小挪移: package com.code; import java.util.Arrays; public class Test02_2 { public int[] solution(int[ ...