【BZOJ2762】[JLOI2011]不等式组(树状数组)
题目:
分析:
加入的不等式分三种情况
当\(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]不等式组(树状数组)的更多相关文章
- 【BZOJ2762】[JLOI2011]不等式组 树状数组
[BZOJ2762][JLOI2011]不等式组 Description 旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了, ...
- bzoj 2762: [JLOI2011]不等式组——树状数组
旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了,所以旺喵想挑战旺汪.旺喵给出一组一元不等式,并给出一个数值 .旺汪需要回答 ...
- Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对
题目:http://codevs.cn/problem/3286/ 3286 火柴排队 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : ...
- NOIP2013 提高组day2 2 花匠 动规 找拐点 树状数组
花匠 描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花排列得比较别致. 具体 ...
- 【洛谷】NOIP提高组模拟赛Day2【动态开节点/树状数组】【双头链表模拟】
U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...
- 【做题记录】 [JLOI2011]不等式组
P5482 [JLOI2011]不等式组 超烦人的细节题!(本人调了两天 QAQ ) 这里介绍一种只用到一只树状数组的写法(离线). 树状数组的下标是:所有可能出现的数据进行离散化之后的值. 其含义为 ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- UVALive 6911---Double Swords(贪心+树状数组(或集合))
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
随机推荐
- xfce 安装文泉驿字体
下载文泉驿字体 #拷贝字体到目录/usr/share/fonts/wqy#创建字体缓存 mkfontscale # 在当前目录下生成fonts.scale文件 mkfontdir # 在当前目录下生成 ...
- C语言结构体用法
结构体的定义: 方法一: struct student { char name[10]; int age; int number; }; struct student stu1; 方法二: struc ...
- Datatable 插入一行数据到第一行
var t = $('#passwdHOST').DataTable({ 'searching': true, 'ordering': false, 'autoWidth': false, dom: ...
- SRAM的简单概念
CY7C138 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 【Codeforces 264B】Good Sequences
[链接] 我是链接,点我呀:) [题意] 让你在一个递增数组中选择一个最长子序列使得gcd(a[i],a[i+1])>1 [题解] 设f[i]表示以一个"含有素因子i的数字" ...
- type="timestamp"与type="date"区别
type="timestamp"-----数据库中保存的时间为年月日时分秒 与type="date"---------数据库中保存的时间为年月日
- python 线程进程
一 线程的2种调用方式 直接调用 实例1: import threading import time def sayhi(num): #定义每个线程要运行的函数 print("runni ...
- [luoguP3146] [USACO16OPEN]248(区间DP)
传送门 f[i][j]表示区间 i-j 合并的最大值 转移: 若f[i][k] && f[k+1][j] && f[i][k] == f[k+1][j] --> ...
- Uva10305 Ordering Tasks
John有n个任务,但是有些任务需要在做完另外一些任务后才能做. 输入 输入有多组数据,每组数据第一行有两个整数1 <= n <= 100 和 m.n是任务个数(标记为1到n),m两个任务 ...
- codevs3410 别墅房间
题目描述 Description 小浣熊松松到他的朋友家别墅去玩,发现他朋友的家非常大,而且布局很奇怪.具体来说,朋友家的别墅可以被看做一个N*M的矩形,有墙壁的地方被标记为’#’,其他地方被标记为’ ...