题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1877
#include "algorithm" #include "iostream" #include "cstring" #include "cstdio" #include "string" #include "stack" #include "cmath" #include "queue" #include "set" #include "map" #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 typedef long long ll; using namespace std; const int inf=0x3f3f3f3f; const int maxn=1e5+5; int t,n,m; struct node{ int l,r; }qujian[maxn]; bool ok[maxn]; int tree[maxn<<2]; int col[maxn<<2]; void pushUp(int rt) { tree[rt] = min( tree[rt<<1] ,tree[rt<<1|1] ); } void push_down(int rt) { if(col[rt]) { tree[rt<<1]+=col[rt]; tree[rt<<1|1]+=col[rt]; col[rt<<1]+=col[rt]; col[rt<<1|1]+=col[rt]; col[rt]=0; } } void build(int l,int r,int rt) { col[rt]=0; if(l==r) { tree[rt]=0; return; } int m = (l+r)>>1; build(lson); build(rson); pushUp(rt); } void update(int L,int R,int l,int r,int rt) { if(L<=l&&R>=r) { col[rt]++; tree[rt]++; return ; } push_down(rt); int m = (l+r)>>1; if(L<=m)update(L,R,lson); if(R>m)update(L,R,rson); pushUp(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l && r<=R) { return tree[rt]; } push_down(rt); int m = (l+r)>>1; int ret = inf; if(L<=m)ret = min(ret, query(L,R,lson)); if(R>m)ret=min(ret, query(L,R,rson) ); return ret; } int main() { //freopen("in.txt","r",stdin); scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); build(1,n,1); memset(ok,0,sizeof ok); int l,r; for(int i=0;i<m;++i) { scanf("%d%d",&l,&r); qujian[i].l=l; qujian[i].r=r; update(l,r,1,n,1); } int ans=0; for(int i=0;i<m;++i) { if( query(qujian[i].l,qujian[i].r,1,n,1 )>1 ) { ok[i]=1; ans++; } } printf("%d\n",ans); bool first=1; for(int i=0;i<m;++i) { if( ok[i] ) { if(first) { printf("%d",i+1); first=false; } else { printf(" %d",i+1); } } } if(ans)printf("\n"); } return 0; }
原文:http://www.cnblogs.com/bruce27/p/5425101.html