B. Light bulbs(2019 ICPC上海站)

題目描述:

There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are off.A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L,R)FLIP(L, R)FLIP(L,R) means to flip all bulbs xxx such that L≤x≤RL \leq x \leq RL≤x≤R. So for example, FLIP(3,5)FLIP(3, 5)FLIP(3,5) means to flip bulbs 333 , 444 and 555, and FLIP(5,5)FLIP(5, 5)FLIP(5,5) means to flip bulb 555.Given the value of NNN and a sequence of MMM flips, count the number of light bulbs that will be on at the end state.

輸入:

The first line of the input gives the number of test cases, TTT. TTT test cases follow. Each test case starts with a line containing two integers NNN and MMM, the number of light bulbs and the number of operations, respectively. Then, there are MMM more lines, the iii-th of which contains the two integers LiL_iLi​ and RiR_iRi​, indicating that the iii-th operation would like to flip all the bulbs from LiL_iLi​ to RiR_iRi​ , inclusive.1≤T≤10001 \leq T \leq 10001≤T≤10001≤N≤1061 \leq N \leq 10^61≤N≤1061≤M≤10001 \leq M \leq 10001≤M≤10000≤Li≤Ri≤N−10 \leq L_i \leq R_i \leq N-10≤Li​≤Ri​≤N−1

輸出:

For each test case, output one line containing Case #x: y, where xxx is the test case number (starting from 111) and yyy is 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

code:

這道題,當時寫的時候,實在是一言難盡,一開始感覺是線段樹??然後寫了寫發現超內存,然後換成樹狀數組??然後寫了寫發現超時,超時在分個塊查詢???然後怎麼分都是超時,超時,超時,,,,自閉。。。。。
代碼其實很簡單,初學者都能看懂,只是這個思路,有點難想

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6+20;
int num[maxn*2];
int main()
{
 int ttt;
 scanf("%d",&ttt);
 for(int kk=1;kk<=ttt;kk++)
 {
  int n,m;
  scanf("%d%d",&n,&m);
  int count=0;
  for(int i=0;i<m;i++)
  {
   int xx,yy;
   scanf("%d%d",&xx,&yy);
   num[count++]=xx;
   num[count++]=yy+1;
  }
  sort(num,num+m*2);
  int sum=0;
  for(int i=0;i<count;i+=2)
  {
   sum+=num[i+1]-num[i];
  }
  printf("Case #%d: %d\n",kk,sum);
 }
 return 0;
 } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章