码迷,mamicode.com
首页 > 编程语言 > 详细

Light bulbs (树状数组模板题)

时间:2019-09-15 18:24:46      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:rom   --   ini   otto   test   clip   file   include   one   

There are N light bulbs indexed from 00 to N1. Initially, all of them are off.

A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L, R)means to flip all bulbs x such that LxR. So for example, FLIP(3, 5) means to flip bulbs 3 , 4 and 5, and FLIP(5, 5)means to flip bulb 5.

Given the value of N and a sequence of M flips, count the number of light bulbs that will be on at the end state.

InputFile

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with a line containing two integers N and M, the number of light bulbs and the number of operations, respectively. Then, there are M more lines, the i-th of which contains the two integers Li? and Ri?, indicating that the ii-th operation would like to flip all the bulbs from Li? to Ri? , inclusive.

1T1000

1 N1000000

1 M1000

0Li?Ri?N1

OutputFile

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yyis the number of light bulbs that will be on at the end state, as described above.

样例输入

2
10 2
2 6
4 8
6 3
1 1
2 3
3 4

样例输出

Case #1: 4
Case #2: 3
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const int maxn=2e3+5;
int str[maxn],str2[maxn],c[1000005],n,m;
int ask(int x)
{
    int ans=0;
    for(; x; x-=x&-x)ans+=c[x];
    return ans;
}
void add(int x,int y)
{
    for(; x<=n; x+=x&-x)c[x]+=y;
}
template<class T>
inline void read(T&x)
{
    T ans=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        ans=ans*10+ch-‘0‘;
        ch=getchar();
    }
    x=ans*f;
}
int main()
{
    int t,l,r,cnt=0,k=0;
    read(t);
    while(t--)
    {
        cnt=0;
        read(n);
        read(m);
        int m2=m;
        while(m2--)
        {
            read(l);read(r);
            ++l,++r;
            add(l,1);
            add(r+1,-1);
            str[++cnt]=l,str2[cnt]=l;
            str[++cnt]=r+1,str2[cnt]=r+1;
        }
        sort(str+1,str+cnt+1);
        int cnt2=0;
        for(int i=1; i<=cnt; ++i)
        {
            if(str[i]!=str[cnt2])
                str[++cnt2]=str[i];
        }
        int ans=0,flag;
        for(int i=1; i<cnt2; ++i)
        {
            flag=ask(str[i]);
            if((flag&1)==1)
                ans+=str[i+1]-1-str[i]+1;
        }
        printf("Case #%d: %d\n",++k,ans);
        for(int i=1; i<=2*m; i+=2)
        {
            add(str2[i],-1);
            add(str2[i+1],1);
        }
    }
    return 0;
}

  

Light bulbs (树状数组模板题)

标签:rom   --   ini   otto   test   clip   file   include   one   

原文地址:https://www.cnblogs.com/lengsong/p/11523654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!