hdu5372 Segment Game 树状数组
来源:互联网 发布:合肥高铁枢纽 知乎 编辑:程序博客网 时间:2024/05/18 23:52
题意:有长度为1,2,3……的若干线段,现在在x轴上进行操作(a,b)。
当a=0时为添加操作,在[ b,b+i ]上放上一条长度为 i 的线段(当前添加操作是第 i 次添加操作)。
当a=1时为删除操作,把第 b 次添加操作的线段删掉。
每次添加操作时,输出该区间范围内有多少个完整的线段。
操作数<=2*10^5,| b |<=10^9
分析:本来很麻烦,但由于线段长度是递增的,不可能出现横跨的情况。
直接用 右端点小于等于当前区间右端点的个数 - 左端点小于当前区间左端点的个数 即可。
由于| b |<=10^9,所以需要进行离散化。
由于我是把左右端点一起离散化的,树状数组要开 2 * maxn。因为这个WA了好几发,都没发现。
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <map>#define clr(x, y) memset(x, y, sizeof x)using namespace std;typedef long long LL;const double eps=1e-8;const int maxn=200100;int b0[maxn];int a[maxn],b[maxn],id[maxn];int tx[maxn*2],ty[maxn*2];int dgree[maxn*2];int len;void add(int pos,int x,int *A){ while(pos <= len+10) { A[pos] += x; pos += pos & -pos; }}int sum(int pos,int *A){ int ans=0; while(pos > 0) { ans += A[pos]; pos -= pos & -pos; } return ans;}int main(){ // freopen("input.txt","r",stdin); int n,t=0; while(scanf("%d",&n)!=EOF) { t++; int cnt=1; len=0; memset(tx,0,sizeof(tx)); memset(ty,0,sizeof(ty)); for(int i=0;i<n;i++) { scanf("%d %d",&a[i],&b[i]); if(a[i]==0) { b0[cnt]=b[i]; id[i]=cnt; dgree[len++]=b[i]; dgree[len++]=b[i]+cnt; cnt++; } } sort(dgree,dgree+len); len=unique(dgree,dgree+len)-dgree; printf("Case #%d:\n",t); for(int i=0;i<n;i++) { if(a[i]==0) { int x=lower_bound(dgree,dgree+len,b[i])-dgree+1; int y=lower_bound(dgree,dgree+len,b[i]+id[i])-dgree+1; printf("%d\n",sum(y,ty)-sum(x-1,tx)); add(x,1,tx); add(y,1,ty); } else { int x=lower_bound(dgree,dgree+len,b0[b[i]])-dgree+1; int y=lower_bound(dgree,dgree+len,b0[b[i]]+b[i])-dgree+1; add(x,-1,tx); add(y,-1,ty); } } } return 0;}
0 0
- hdu5372 Segment Game 树状数组
- hdu5372 Segment Game (树状数组)
- hdu5372(2015多校7)--Segment Game(树状数组)
- hdu5372 Segment Game
- hdu5372(树状数组)
- hdu5372 离散化+树状数组
- HDU 5372 Segment Game 树状数组
- 【树状数组】 HDU 5372 Segment Game
- hdu 5372 Segment Game(树状数组)
- HDU 5372 - Segment Game(树状数组)
- 【HDU 5372】Segment Game(树状数组)
- HDU 5372 Segment Game 树状数组
- HDU 5372 Segment Game(树状数组)
- [HDU 5372] Segment Game (树状数组)
- [树状数组] hdu 5372 Segment Game
- Segment Game (hdu 5372 树状数组+离散化)
- hdu 5372 Segment Game(树状数组+离散化)
- hdu 5372 Segment Game (树状数组+离散化)
- Magento SOAP API V2 开放接口修改订单状态
- SQL优化基础 使用索引(一个小例子)
- 解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题
- 多项式求和
- 人民币贬值三重冲击波
- hdu5372 Segment Game 树状数组
- 猴子吃香蕉
- HTTP协议详解
- [Win32]打开文件/保存文件/选择文件夹对话框
- NYOJ 469 擅长排序的小明
- 智能设备是如何连接云的
- sql统计某字段出现次数
- xilinx ise 的planahead的使用1
- 如何在servlet中将数据封装成JSON格式之后传到前台