題意:有長度爲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;
}