http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1149&pid=3
懒人标记代码o(n)
#include<iostream> #include<string> #include<cstring> #include<queue> using namespace std; const int N=100; int main() { int a[N],b[N],dp[N]; queue<int> q; int i,j,k,tmp,n,m,text; cin>>text; while(text--) { memset(dp,0,sizeof(dp)); cin>>n>>m; for(i=0;i<m;i++) cin>>a[i]>>b[i],dp[a[i]]++,dp[b[i]+1]--;//标记 1 -1 for(i=2;i<=n;i++) dp[i]+=dp[i-1];//每间房打扫 的次数 for(i=1;i<=n;i++) if(dp[i]!=1&&dp[i]!=0) dp[i]=0;else dp[i]=1;//将打扫0 1次的房间标记为1 for(i=1;i<=n;i++) dp[i]+=dp[i-1];//前i个有 几个打扫房间打扫0 1次 for(i=0;i<m;i++) if(dp[b[i]]-dp[a[i]-1]==0) q.push(i+1);//判断是否有 打扫0 1次的房间 cout<<q.size()<<endl; if(q.size()==0) continue; tmp=q.front(); q.pop(); cout<<tmp; while(!q.empty()) { tmp=q.front(); q.pop(); cout<<" "<<tmp; } cout<<endl; } }
#include <cstdio> #include<iostream> #include<cstring> #include<string> using namespace std; #define lson l,m,rt<< 1 #define rson m+1,r,rt<<1|1 const int N=100000+10; int s[N]; int main() { int m,n,i,j,k,ans,tmp; while(scanf("%d",&n),n) { int a,b; memset(s,0,sizeof(s)); for(i=0;i<n;i++) scanf("%d%d",&a,&b),s[a]++,s[b+1]--; for(i=1;i<=n;i++) s[i]+=s[i-1]; for(i=1;i<n;i++) cout<<s[i]<<" "; cout<<s[n]<<endl; } return 0; }