记录编号 118274 评测结果 AAAAAAAAAAAAAA
题目名称 线段覆盖 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 1.941 s
提交时间 2014-09-05 21:21:28 内存使用 7.19 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============

//==============var declaration=================
const int MAXN=200100;
int n,q,cmd;
int maxv[MAXN*2],minv[MAXN*2],leftc[MAXN*2],rightc[MAXN*2],addv[MAXN];
#define y1 y
int y1,y2;
//==============function declaration============
void update(int o,int l,int r);//将y1..y2全部++
void maintain(int o,int l,int r);//更新节点 
int query(int o,int l,int r,int add);//求黑色块数
int cons(int o,int l,int r,int add);//求连续黑色块数 
//==============main code=======================
int main()
{  
  string FileName="xdfg";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
  clock_t Start_Time=clock();
#endif      
   memset(minv,0,sizeof(minv));
   memset(maxv,0,sizeof(maxv));
   memset(addv,0,sizeof(addv));
   memset(leftc,0,sizeof(leftc));
   memset(rightc,0,sizeof(rightc));
   scanf("%d%d",&n,&q);
   For(i,1,q)
   {
     scanf("%d%d%d",&cmd,&y1,&y2);
     y2=y1+y2-1;
     update(1,1,n);
     printf("%d ",cons(1,1,n,0));
     printf("%d\n",query(1,1,n,0));
   } 
#ifdef DEBUG  
  clock_t End_Time=clock();
  cout<<endl<<endl<<"Time Used: "<<(End_Time-Start_Time)/CLOCKS_PER_SEC<<endl;
#endif    
  return 0;
}
//================fuction code====================
void maintain(int o,int l,int r)
{
   if (l==r)
   {
     maxv[o]=minv[o]=leftc[o]=rightc[o]=addv[o];
     return;
   }
   int lc=o*2;
   int rc=o*2+1;
   maxv[o]=max(maxv[lc],maxv[rc])+addv[o];
   minv[o]=min(minv[lc],minv[rc])+addv[o];
   leftc[o]=leftc[lc]+addv[o];
   rightc[o]=rightc[rc]+addv[o];
}
void update(int o,int l,int r)//全体++; 
{
   int lc=o*2;
   int rc=o*2+1;
   int m=(l+r)>>1;
   if (y1<=l&&r<=y2)
   {
     if (cmd==1)  addv[o]++;
     if (cmd==2)  addv[o]--;
   }
   else
   {
     if (m>=y1)
       update(lc,l,m);
     if (m<y2)
       update(rc,m+1,r); 
   } 
   maintain(o,l,r);
}
int query(int o,int l,int r,int add)
{
   int lc=o*2;
   int rc=o*2+1;
   int m=(l+r)>>1;
   if (minv[o]+add+addv[o]>0)
     return r-l+1;
   if (maxv[o]+add+addv[o]==0)
     return 0;
   int res=0;
   res+=query(lc,l,m,add+addv[o]);
   res+=query(rc,m+1,r,add+addv[o]);
   return res;
}
int cons(int o,int l,int r,int add)
{ 
   int lc=o*2;
   int rc=o*2+1;
   int m=(l+r)>>1;
   if (minv[o]+add>0)
     return 1;
   if (maxv[o]+add==0)
     return 0;
   int res=0;
   res+=cons(lc,l,m,add+addv[o]);
   res+=cons(rc,m+1,r,add+addv[o]);
   if (rightc[lc]+addv[o]&&leftc[rc]+addv[o])
     res--;
   return res;
}