acdream1157Segments

链接:http://acdream.info/problem?pid=1157

题意:中文题。

分析:cdq分治的练习题,分析同BZOJ1176题解,稍微变一点就是比较变为x>=x',y<=y',z>=z',然后记得离散化和处理好题目说的删除操作就好了。O(nlogn^2)。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=200010;
const int MAX=151;
const int mod=100000000;
const int MOD1=100000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000000;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
struct node {
    int x,y,z,id,tab;
}ope[N],aux[N];
char s[3];
int w,a[2*N],f[N],lin[N],ans[N];
int cmp(node a,node b) {
    if (a.x!=b.x) return a.x<b.x;
    else if (a.y!=b.y) return a.y>b.y;
        else return a.z<b.z;
}
void add(int a,int b) {
    for (;a<=w;a+=a&(-a)) f[a]+=b;
}
int getsum(int a) {
    int ret=0;
    for (;a;a-=a&(-a)) ret+=f[a];
    return ret;
}
void cdq(int l,int r) {
    if (l==r) return ;
    int i,mid=(l+r)>>1,L,R;
    for (i=l;i<=r;i++)
    if (ope[i].z<=mid&&!ope[i].id) add(ope[i].y,ope[i].tab);
    else if (ope[i].z>mid&&ope[i].id) ans[ope[i].id]+=getsum(w)-getsum(ope[i].y-1);
    for (i=l;i<=r;i++)
    if (ope[i].z<=mid&&!ope[i].id) add(ope[i].y,0-ope[i].tab);
    L=l;R=mid+1;
    for (i=l;i<=r;i++)
    if (ope[i].z<=mid) { aux[L]=ope[i];L++; }
    for (i=l;i<=r;i++)
    if (ope[i].z>mid) { aux[R]=ope[i];R++; }
    for (i=l;i<=r;i++) ope[i]=aux[i];
    cdq(l,mid);cdq(mid+1,r);
}
int main()
{
    int i,q,k,l,n;
    while (scanf("%d", &n)!=EOF) {
        q=k=l=0;
        for (i=1;i<=n;i++) {
            scanf("%s", s);ope[i].z=i;
            if (s[0]=='D') {
                ope[i].id=0;ope[i].tab=1;lin[++l]=i;
                scanf("%d%d", &ope[i].x, &ope[i].y);
                k++;a[k]=ope[i].x;k++;a[k]=ope[i].y;
            }
            if (s[0]=='C') {
                scanf("%d", &w);
                ope[i].id=0;ope[i].tab=-1;
                ope[i].x=ope[lin[w]].x;ope[i].y=ope[lin[w]].y;
            }
            if (s[0]=='Q') {
                ope[i].id=++q;ope[i].tab=0;
                scanf("%d%d", &ope[i].x, &ope[i].y);
                k++;a[k]=ope[i].x;k++;a[k]=ope[i].y;
            }
        }
        sort(a+1,a+k+1);
        w=unique(a+1,a+k+1)-(a+1);
        for (i=1;i<=n;i++) {
            ope[i].x=lower_bound(a+1,a+w+1,ope[i].x)-a;
            ope[i].y=lower_bound(a+1,a+w+1,ope[i].y)-a;
        }
        memset(f,0,sizeof(f));
        memset(ans,0,sizeof(ans));
        sort(ope+1,ope+n+1,cmp);
        cdq(1,n);
        for (i=1;i<=q;i++) printf("%d\n", ans[i]);
    }
    return 0;
}


你可能感兴趣的:(cdq分治)