Пример #1
0
int main()
{
    while(~scanf("%d%d%d%d",&w,&h,&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

            square[i].l = min(x1,x2);
            square[i].r = max(x1,x2);
            square[i].b = min(y1,y2);
            square[i].t = max(y1,y2);
        }

        V.clear();

        for(int i=1;i<=n;i++)
        {
            V.push_back(Node(square[i].t,square[i].b,1,square[i].l));
            V.push_back(Node(square[i].t,square[i].b,-1,square[i].r+1));
        }
        V.push_back(Node(1,h,1,w+1));

        sort(V.begin(),V.end());

        int xx = 0,pre = -1;
        long long ans = 0;
        S.clear();
        S.insert(setNode(0,0));
        S.insert(setNode(h+1,h+1));
        int width = h-m+1 > 0 ? h-m+1 : 0;

        while(xx < V.size())
        {
            while(xx+1 < V.size() && V[xx].pos == V[xx+1].pos) xx++;

            ans += (long long)width * (long long)(V[xx].pos - (pre == -1 ? 1 : V[pre].pos));
            for(int i=pre+1;i<=xx;i++)
            {
                if(V[i].kind == 1)
                {
                    multiset<setNode>::iterator It1 = S.upper_bound(setNode(V[i].top,V[i].bot));
                    multiset<setNode>::iterator It2 = --It1;++It1;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It1->bot - V[i].top - m > 0 ? It1->bot - V[i].top - m : 0;
                    width += V[i].bot - It2->top - m > 0 ? V[i].bot - It2->top - m : 0;
                    S.insert(setNode(V[i].top,V[i].bot));
                }
                else if(V[i].kind == -1)
                {
                    multiset<setNode>::iterator It1 = S.find(setNode(V[i].top,V[i].bot));if(It1 == S.end())while(1);
                    multiset<setNode>::iterator It2 = --It1;++It1;
                    multiset<setNode>::iterator It3 = ++It1;--It1;
                    width -= It3->bot - It1->top - m > 0 ? It3->bot - It1->top - m : 0;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It3->bot - It2->top - m > 0 ? It3->bot - It2->top - m : 0;
                    S.erase(It1);
                }
            }
            pre = xx;
            xx++;
        }
        //-------------------------------------------
        if(m == 1) goto Print;

        V.clear();

        for(int i=1;i<=n;i++)
        {
            V.push_back(Node(square[i].r,square[i].l,1,square[i].b));
            V.push_back(Node(square[i].r,square[i].l,-1,square[i].t+1));
        }
        V.push_back(Node(1,w,1,h+1));

        sort(V.begin(),V.end());

        xx = 0,pre = -1;
        S.clear();
        S.insert(setNode(0,0));
        S.insert(setNode(w+1,w+1));
        width = w-m+1 > 0 ? w-m+1 : 0;

        while(xx < V.size())
        {
            while(xx+1 < V.size() && V[xx].pos == V[xx+1].pos) xx++;

            ans += (long long)width * (long long)(V[xx].pos - (pre == -1 ? 1 : V[pre].pos));
            for(int i=pre+1;i<=xx;i++)
            {
                if(V[i].kind == 1)
                {
                    set<setNode>::iterator It1 = S.upper_bound(setNode(V[i].top,V[i].bot));
                    set<setNode>::iterator It2 = --It1;++It1;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It1->bot - V[i].top - m > 0 ? It1->bot - V[i].top - m : 0;
                    width += V[i].bot - It2->top - m > 0 ? V[i].bot - It2->top - m : 0;
                    S.insert(setNode(V[i].top,V[i].bot));
                }
                else if(V[i].kind == -1)
                {
                    set<setNode>::iterator It1 = S.find(setNode(V[i].top,V[i].bot));
                    set<setNode>::iterator It2 = --It1;++It1;
                    set<setNode>::iterator It3 = ++It1;--It1;
                    width -= It3->bot - It1->top - m > 0 ? It3->bot - It1->top - m : 0;
                    width -= It1->bot - It2->top - m > 0 ? It1->bot - It2->top - m : 0;
                    width += It3->bot - It2->top - m > 0 ? It3->bot - It2->top - m : 0;
                    S.erase(It1);
                }
            }
            pre = xx;
            xx++;
        }
        Print:printf("%lld\n",ans);
    }
    return 0;
}
Пример #2
0
void MAIN()
{
    int cases;
    pree();

    scanf("%d",&cases);
    int tn,cct,vct;
    int i,j;

    for(int t=1;t<=cases;++t)
    {
        ansc.clear();
        ansv.clear();
        scanf("%d",&n);
        cct=n/2;
        vct=n-cct;
        tn=n;

        for(i=0;i<v.size();++i)
        {
            for(j=0;j<min(21,vct);++j)
            {
                //printf("%c\n",v[i].second);
                ansv.insert(v[i].second);
            }
            vct-=j;
        }

        for(i=0;i<c.size();++i)
        {
            for(j=0;j<min(5,cct);++j)
            {
                //printf("%c\n",c[i].second);
                ansc.insert(c[i].second);
            }
            cct-=j;
        }

        printf("Case %d: ",t);

        sit1=ansv.begin();
        sit2=ansc.begin();

        //printf("%d\t%d\n",anv.size())

        for(i=0;i<n;++i)
        {
            if(i&1)
            {
                printf("%c",*sit2);
                ++sit2;
            }
            else
            {
                printf("%c",*sit1);
                ++sit1;
            }
        }


        printf("\n");
    }
}
Пример #3
0
int main()
{
	freopen("in", "r", stdin);
	freopen("std", "w", stdout);
	tree.clear();
	while (1)
	{
		int operation;
		scanf("%d", &operation);
		if (operation == -1)
		{
			return 0;
		}
		/*
		else if (operation == 0)
		{
			tree.clear();
		}
		*/
		else if (operation == 0)
		{
			int key;
			scanf("%d", &key);
			tree.insert(key);
		}
		else if (operation == 1)
		{
			int key;
			scanf("%d", &key);
			multiset<int>::iterator it = tree.find(key);
			if (it != tree.end())
			{
				tree.erase(it);
				printf("OK\n");
			}
			else
				printf("Delete Error\n");
		}
		else if (operation == 2)
		{
			int key;
			scanf("%d", &key);
			multiset<int>::iterator it = tree.find(key);
			if (it != tree.end())
				printf("Yes\n");
			else
				printf("No\n");
		}
		/*
		else if (operation == 4)
		{
			int key;
			scanf("%d", &key);
			multiset<int>::iterator it = tree.find(key);
			if (it != tree.end())
			{
				-- it;
				printf("%d\n", *it);
			}
			else
				printf("Prev Error!\n");
		}
		else if (operation == 5)
		{
			int key;
			scanf("%d", &key);
			multiset<int>::iterator it = tree.find(key);
			if (it != tree.end())
			{
				++ it;
				printf("%d\n", *it);
			}
			else
				printf("Succ Error!\n");
		}
		*/
		else if (operation == 3)
		{
			if (tree.empty())
				printf("NULL\n");
			else
			{
				multiset<int>::reverse_iterator it = tree.rbegin();
				printf("%d\n", *it);
			}
		}
		else if (operation == 4)
		{
			if (tree.empty())
				printf("NULL\n");
			else
			{
				multiset<int>::iterator it = tree.begin();
				printf("%d\n", *it);
			}
		}
	}
	return 0;
}
int main(){
    powB[0] = powB2[0] = 1;
    
    for(int i = 1;i < MAXL;++i){
        powB[i] = B * powB[i - 1];
        powB2[i] = B * powB2[i - 1] % MOD;
    }
    
    int T,L;
    
    scanf("%d",&T);
    
    while(T--){
        scanf("%s",s);
        
        L = strlen(s);
        
        H[L] = H3[L] = 0;
        
        for(int i = L - 1;i >= 0;--i){
            H[i] = ((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H[i + 1];
            H3[i] = (((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H3[i + 1]) % MOD;
        }
        
        H2[0] = H4[0] = 0;
        
        for(int i = 0;i < L;++i){
            H2[i + 1] = ((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H2[i];
            H4[i + 1] = (((s[i] >= 'a' && s[i] <= 'z')? s[i] - 'a' : s[i] - 'A' + 26) + B * H4[i]) % MOD;
        }
        
        for(int i = 0;i < L;++i){
            int lo = 0,hi = min(i + 1,L - i - 1),mi;
            
            while(lo < hi){
                mi = (lo + hi + 1) >> 1;
                
                if(get_hash3(i - mi + 1,i) != get_hash4(i + 1,i + mi)) hi = mi - 1;
                else lo = mi;
            }
            
            r[i] = lo;
        }
        
        int Q = 0;
        vq.clear();
        in.clear();
        out.clear();
        
        for(int i = 0;i < L;++i){
            if(r[i]){
                int r2 = r[i] / 2;
                
                vq.push_back(make_pair(i,i + 1 - r2));
                in.push_back(make_pair(i + 1,i));
                out.push_back(make_pair(i + r[i] + 1,i));
                ++Q;
            }
        }
        
        sort(vq.begin(),vq.end());
        sort(in.begin(),in.end());
        sort(out.begin(),out.end());
        
        S.clear();
        int ans = 0;
        
        for(int i = 0,p1 = 0,p2 = 0,p3 = 0;i < L;++i){
            while(p3 < Q && out[p3].first == i){
                it = S.find(out[p3].first - r[ out[p3].second ]);
                S.erase(it);
                ++p3;
            }
            
            while(p2 < Q && in[p2].first == i){
                S.insert(in[p2].first);
                ++p2;
            }
            
            while(p1 < Q && vq[p1].first == i){
                it = S.lower_bound( vq[p1].second );
                if(it != S.end()) ans = max(ans,i - *it + 1);
                ++p1;
            }
        }
        
        printf("%d\n",ans * 4);
    }
    
    return 0;
}
 /** initialize your data structure here. */
 MedianFinder() 
 {
     Small.clear();
     Large.clear();
 }
Пример #6
0
int main(){
    while (~scanf("%d%d",&n,&m)){
        S.clear();
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            S.insert(a[i]);
        }
        if (n==1){
            printf("1\n%d\n",a[1]);
            continue;
        }
        if (n==2){
            if (a[1]+a[2]>m)
                printf("2\n%d %d\n",a[1],a[2]);
            else
                printf("1\n%d %d\n",a[1],a[2]);
            continue;
        }
        memset(bi,true,sizeof(bi));
        sort(a+1,a+n+1);
        b[2]=a[n];
        multiset<int>::iterator it;
        /*
        it=S.lower_bound(b[2]);
        S.erase(it);

        it=S.upper_bound(m-b[2]);
        if (it==S.end()) --it;
        b[1]=(*it);
        S.erase(it);

        for (int i=2;i<n;++i){
            it=S.upper_bound(m-b[i]);
            if (it==S.end()) --it;
            b[i+1]=(*it);
            S.erase(it);
        }
        */
        bool flag=true;
        for (int i=1;i<=n;i++)
        {
            if (flag)
            {
                if (i<n)
                {
                    it=S.end();it--;
                    b[i+1]=(*it);
                    S.erase(it);
                    it=S.upper_bound(m-b[i+1]);
                    if (it==S.end()) it=S.begin();
                    b[i]=(*it);
                    S.erase(it);
                    i++;
                    if (b[i-1]+b[i]<=m)
                        flag=true;
                    else flag=false;
                }
                else
                {
                    it=S.begin();
                    b[i]=(*it);
                    S.erase(it);
                }
            }
            else
            {
                it=S.upper_bound(m-b[i-1]);
                if (it==S.end())
                {
                    it=S.begin();
                    flag=true;
                }
                else flag=false;
                b[i]=(*it);
                S.erase(it);
            }
        }
        int ans=0;
        b[n+1]=0;
        for (int i=1;i<=n;++i){
            if (b[i]+b[i+1]<=m) i++;
            ans++;
        }
        printf("%d\n",ans);
        for (int i=1;i<=n;++i) printf("%d%c",b[i],i==n?'\n':' ');
    }
    return 0;
}
multiset<int> reinit_set(multiset<int> ackset)
{
	ackset.clear();
	return ackset;
}
int main(){
	bool first = true;
	int i, x;

	scanf("%d", &N);

	while (N--){
		if (!first) putchar('\n');
		first = false;

		G.clear();
		B.clear();

		scanf("%d %d %d", &BT, &SG, &SB);

		for (i = 0; i < SG; i++){
			scanf("%d", &x);
			G.insert(x);
		}

		for (i = 0; i < SB; i++){
			scanf("%d", &x);
			B.insert(x);
		}

		while (G.size() && B.size()){
			auto git = G.begin(), bit = B.begin();

			Ga.clear();
			Ba.clear();

			for (i = 0; i < BT && G.size() && B.size(); i++){
				int g = *git;
				int b = *bit;

				git = G.erase(git);
				bit = B.erase(bit);

				if (b > g)
					Ba.push_back(b - g);
				else if (b < g)
					Ga.push_back(g - b);
			}

			G.insert(Ga.begin(), Ga.end());
			B.insert(Ba.begin(), Ba.end());
		}

		auto &x = G;

		if (!G.size() && !B.size())
			printf("green and blue died\n");
		else if (G.size()){
			printf("green wins\n");
			x = G;
		} else {
			printf("blue wins\n");
			x = B;
		}

		for (auto it = x.begin(); it != x.end(); it++)
			printf("%d\n", *it);
	}
}
int main(){

    int i,j,k,l,test,t=1;

    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    scanf("%d",&test);

    while(test--){

        scanf("%d",&n);

        set1.clear();

        printf("Case #%d:\n",t++);

        int li,ci;
        for(i=1;i<=n;i++){
            scanf("%d %d",&li,&ci);
            pii now=mp(li,ci);

            if(set1.find(now)!=set1.end()){
                set1.insert(now);
                printf("%d\n",set1.size());
                continue;
            }

            //printf("\n%d %d\n",li,ci);

            while(set1.size()){
                it=set1.upper_bound(now);
                //printf("it= %d-%d\n ",(*it).uu,(*it).vv);
                if(it==set1.end()){
                    break;
                }
                else if((*it).vv>=ci){
                    set1.erase(it);
                }
                else break;
            }


			it=set1.upper_bound(now);

			if(it==set1.begin()){
				set1.insert(now);
			}
			else{
				it--;
				if((*it).vv>ci){
					set1.insert(now);
				}
			}

            for(it=set1.begin();it!=set1.end();it++){
            //    printf("%d-%d  ",(*it).uu,(*it).vv);
            }
            //puts("");
            printf("%d\n",set1.size());
            continue;
        }

        if(test) puts("");
    }

    return 0;
}
Пример #10
0
int main()
{
    int T, ca=0, ti = 0, a, bb, sum, le, ri;
    char op[10];
    vi p1, p2;
    scanf("%d", &T);
    while (T--)
    {
          scanf("%d%d%d", &n, &Q, &t);
        //  n+=2;
          ans.init();
          seg.init();
          point.clear();
          ti=0;
          printf("Case %d:\n", ++ca);
          for (int i=0; i<Q; i++)
          {
              scanf("%s", op);
              if (op[0]=='A')
              {
                  ti++;
                  scanf("%d%d", &que[ti][0], &que[ti][1]);
                  que[ti][0] += 2; que[ti][1] += 2;
              //    cout <<ti << " "<<t<<endl;
                  if (ti-t>=1) 
                  {
                      seg.insert_seg(que[ti-t][0], que[ti-t][1], -1);
                      point.erase(lower_bound(point.begin(), point.end(), que[ti-t][0]));
                      point.erase(lower_bound(point.begin(), point.end(), que[ti-t][1]));                                                         
                  }
                  if (point.size()>=2){
                  vi pp = point.end();
                  pp--;
                  le = max(*point.begin(), que[ti][0]); 
                  ri = min(*pp, que[ti][1]);
                  if (le > ri)
                  {
                     le = que[ti][0];
                     ri = que[ti][1];                         
                  }
                  cout <<le-2 <<" "<<ri-2 <<"----"<<*point.begin() <<" "<<*pp <<endl;
                  ans.updata(ri-2, 1);
                  ans.updata(le-1-2, -1);
                  }
                  point.insert(que[ti][0]);
                  point.insert(que[ti][1]);                  
                  if (ti-t>=1)
                  {
                     le = max(que[ti-t][0], le);
                     ri = min(ri, que[ti-t][1]);
                     cout <<le-2 <<" "<<ri-2<<"  recovery" <<endl;
                     ans.updata(ri-2, -1);
                     ans.updata(le-1-2, 1);                             
                         
                   le = n+2; ri = 2;
                   if (ti-t-1>=1){
                      for (int j=ti-t-1; j+t>=ti-t && j+t<ti; j--)
                      {
                        //  if (j < 1) break;
                          le = min(le, que[j][0]);
                          ri = max(ri, que[j][1]);    
                      }
                      le = max(le, que[ti-t][0]);
                      ri = min(ri, que[ti-t][1]);
                   //   cout <<le-2 <<" "<<ri-2<<" add" <<endl;
                      ans.updata(ri-2, 1);
                      ans.updata(le-1-2, -1);        
                      }                         
                  
                  }
              }
              else
              {
                  scanf("%d", &bb);
                  printf("%d\n", ans.query(bb));
              }
          }
    }
    system("pause");
    return 0;
    }
Пример #11
0
void solve() {
	scanf("%d", &n);
	axis_cnt = 0;
	memset(cntA, 0, sizeof(cntA));
	memset(cntB, 0, sizeof(cntB));
	int totA = 0, totB = 0;
	for ( int i = 1; i <= n; i ++ ) {
		scanf("%d", &x[i]);
		axis[axis_cnt ++] = x[i];
	}
	sort(axis, axis + axis_cnt);
	axis_cnt = unique(axis, axis + axis_cnt) - axis;
	for ( int i = 1; i <= n; i ++ )
		x[i] = lower_bound(axis, axis + axis_cnt, x[i]) - axis;
	scanf("%d%d", &A, &B);

	for ( int st = 1; st + A - 1 <= n; st ++ ) {
		static multiset<int> S;
		S.clear();
		for ( int i = st; i <= st + A - 1; i ++ ) 
			S.insert(x[i]);
		int mx = *(-- S.end());
		cntA[mx] ++;
		totA ++;
		for ( int en = st + A; en <= n; en ++ ) {
			if ( x[en] < mx ) {
				S.erase(-- S.end());
				S.insert(x[en]);
				mx = *(-- S.end());
			}
			cntA[mx] ++;
			totA ++;
		}
	}
	for ( int st = 1; st + B - 1 <= n; st ++ ) {
		static multiset<int> S;
		S.clear();
		for ( int i = st; i <= st + B - 1; i ++ ) 
			S.insert(x[i]);
		int mx = *(-- S.end());
		cntB[mx] ++;
		totB ++;
		for ( int en = st + B; en <= n; en ++ ) {
			if ( x[en] < mx ) {
				S.erase(-- S.end());
				S.insert(x[en]);
				mx = *(-- S.end());
			}
			cntB[mx] ++;
			totB ++;
		}
	}

	double res = 0;
	for ( int i = 0; i < axis_cnt; i ++ )
		for ( int j = 0; j < axis_cnt; j ++ ) {
			double prob = ((double)cntA[i] / totA) * ((double)cntB[j] / totB);
			if ( i > j )
				res += prob;
			else if ( i == j )
				res += 0.5 * prob;
		}
	printf("%.6lf\n", res);
}