Valera and Queries CodeForces - 369E

http://codeforces.com/problemset/problem/369/E

将查询中的点抽象为线段 即将p1 p2...pk抽象为[1,p1] (p1,p2]...(pk-1,pk] 全部按右端点排序 离线处理

离散化反而WA了 真的很迷。。

#include <bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
const int maxm=1e6+10;

struct node
{
    int id,l,r;
};

node seg[maxn],order[maxn];
int sum[4*maxm];
int pos[maxn],ans[maxn];
int n,q,tot;

bool cmp(node n1,node n2)
{
    return n1.r>n2.r;
}

void update(int tar,int val,int l,int r,int cur)
{
    int m;
    sum[cur]+=val;
    if(l==r) return;
    m=(l+r)/2;
    if(tar<=m) update(tar,val,l,m,2*cur);
    else update(tar,val,m+1,r,2*cur+1);
}

int query(int pl,int pr,int l,int r,int cur)
{
    int res,m;
    if(pl<=l&&r<=pr) return sum[cur];
    res=0,m=(l+r)/2;
    if(pl<=m) res+=query(pl,pr,l,m,2*cur);
    if(pr>m) res+=query(pl,pr,m+1,r,2*cur+1);
    return res;
}

int main()
{
    int i,j,k,maxx;
    scanf("%d%d",&n,&q);
    maxx=0;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&seg[i].l,&seg[i].r);
        maxx=max(maxx,seg[i].r);
    }
    for(i=1;i<=q;i++)
    {
        scanf("%d",&k);
        for(j=1;j<=k;j++) scanf("%d",&pos[j]);
        maxx=max(maxx,pos[k]);
        for(j=1;j<=k;j++)
        {
            tot++;
            order[tot].id=i;
            if(j==1) order[tot].l=1,order[tot].r=pos[j];
            else order[tot].l=pos[j-1]+1,order[tot].r=pos[j];
        }
    }
    sort(seg+1,seg+n+1,cmp);
    sort(order+1,order+tot+1,cmp);
    j=1;
    for(i=1;i<=tot;i++)
    {
        while(j<=n&&seg[j].r>=order[i].r)
        {
            update(seg[j].l,1,1,maxx,1);
            j++;
        }
        ans[order[i].id]+=query(order[i].l,order[i].r,1,maxx,1);
    }
    for(i=1;i<=q;i++) printf("%d\n",ans[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/82935228