BIT区间修改+单点查询

【题目链接】BIT区间修改+单点查询

&题解:

BIT区间修改+单点查询和求和的bit是一模一样的(包括add,sum) 只不过是你使用函数的方式不一样:

使用区间的时候,比如[a,b]区间+1,就是add(a,1); add(b+1,-1); 之后sum(i)查的是i点的值,是一个i点的值,不是区间!!

另外,主函数中fread()必须调用2句话,因为它是缓冲的,所以不可以边输入边测试,只能用freopen测试, 另外附上时间比较,第一个是用的fread,第二个是没用





我还测了一下getchar()的读入挂,结果是780ms,比scanf还慢点,总感觉那个挂没什么用哦

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define cle(a,v) memset(a,(v),sizeof(a))
const int maxn = 100000 + 7; //Reading Plugin
const int BUF = 20000000;
char Buf[BUF], *buf = Buf;
const int OUT = 10000000;
char Out[OUT], *ou = Out; int Outn[30], Outcnt;
inline void write(int x) {
if(!x)*ou++ = 48;
else {
for(Outcnt = 0; x; x /= 10)Outn[++Outcnt] = x % 10 + 48;
while(Outcnt)*ou++ = Outn[Outcnt--];
}
}
inline void writell(ll x) {
if(!x)*ou++ = 48;
else {
for(Outcnt = 0; x; x /= 10)Outn[++Outcnt] = x % 10 + 48;
while(Outcnt)*ou++ = Outn[Outcnt--];
}
}
inline void writechar(char x) {*ou++ = x;}
inline void writeln() {*ou++ = '\n';}
inline void read(int&a) {for(a = 0; *buf < 48; buf++); while(*buf > 47)a = a * 10 + *buf++ -48;} int n, a, b;
struct BIT {
int n, bit[maxn];
void ori(int _n) {
n = _n;
cle(bit, 0);
}
int lowb(int x) { return x & -x; }
void add(int i, int x) {
for(; i <= n; i += lowb(i))
bit[i] += x;
}
int sum(int i) {
int s = 0;
for(; i; i -= lowb(i))
s += bit[i];
return s;
}
} bt;
int main() {
#ifndef ONLINE_JUDGE
freopen("E:1.in", "r", stdin);
#endif
fread(Buf, 1, BUF, stdin);
while(1) {
read(n);
if(n == 0) break;
bt.ori(n);
for(int i = 0; i < n; i++) {
read(a); read(b);
bt.add(a, 1);
bt.add(b + 1, -1);
}
for(int i = 1; i <= n; i++) {
write(bt.sum(i));
if(i == n) writechar('\n');
else writechar(' ');
}
}
fwrite(Out, 1, ou - Out, stdout);
return 0;
}

HDU 1556 BIT区间修改+单点查询(fread读入优化)的更多相关文章

  1. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  2. hdu-1556 Color the ball---树状数组+区间修改单点查询

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...

  3. D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询

    题意 贴海报 最后可以看到多少海报 思路 :离散化大区间  其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时  把y+1点也加入 ...

  4. BZOJ4999:This Problem Is Too Simple!(DFS序&树上差分&线段树动态开点:区间修改单点查询)

    Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x&l ...

  5. POJ2155 Matrix(二维树状数组||区间修改单点查询)

    Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...

  6. 【树状数组区间修改单点查询】HDU 4031 Attack

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...

  7. hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...

  8. HDU 5861 Road(线段树 区间修改 单点查询)

    Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  9. cogs 1316. 数列操作B 区间修改 单点查询

    1316. 数列操作B ★★   输入文件:shulieb.in   输出文件:shulieb.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 假设有一个大小为 n(n ...

随机推荐

  1. tensorflow变量-【老鱼学tensorflow】

    在程序中定义变量很简单,只要定义一个变量名就可以,但是tensorflow有点类似在另外一个世界,因此需要通过当前的世界中跟tensorlfow的世界中进行通讯,来告诉tensorflow的世界中定义 ...

  2. flask微电影系统开发中上下文处理器

    在做日志模块开发的过程中,我们遇到了一个问题,就是日期的问题,我们这个时间就要用到上下文处理器来解决问题,他的作用就是可以在我们固定模块中进行传递,我们的模块如下 @admin.context_pro ...

  3. [数据结构] 快速排序C语言程序

    //由大到小//快速排序(待排序数组,左侧起点,右侧起点) void quickSort(int *array, int l, int r) { if ( l >= r) return; int ...

  4. Java虚拟机----垃圾回收与内存分配

    一.垃圾回收的对象: 在Java的运行时数据区中,程序计数器和虚拟机栈.本地方法栈是随着线程的生灭而生灭,栈当中栈帧的大小在编译的时候已知,在方法结束之后栈帧出栈,这部分的垃圾回收是明确的,因此需要讨 ...

  5. C++调用C语言的库函数

    在项目中,使用C语言编写了一个socket后台程序tkcofferd,并且为方便客户端的使用,提供了动态库,其中包含socket接口. 现在的需求是使用qt做一个前端界面,用来展示tkcofferd的 ...

  6. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  7. linux命令技巧

    printf "%-5s %-10s %-4s\n" No Name Mark printf "%-5s %-10s %-4.2f\n" 1 Sarath 80 ...

  8. oracle的用户管理

    创建用户 在Oracle中创建用户需要用到dba,普通用户无法创建 > create user 用户名 identified by 密码; * 密码必须以英文开头,不然是创建不起来的! * 如果 ...

  9. Win7 IIS配置

    一.首先,在开始打开你的控制面板 二.进入到控制面板,选择程序和功能,点击进入 三.在程序与功能中,左菜单栏,打开你的Windows工能 四.在Windows工能中,把你的Internet信息服务中的 ...

  10. PDF在线解除密码的方法是什么

    大家在网上下载一些关于PDF文件的时候通常会发现有的PDF文件是有密码的,有密码的文件通常是不可以被转换的,这个时候我们就需要将其密码解除掉,这样后期不仅方便阅读也方便了PDF文件的转换. 操作工具: ...