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;
 } 
The ICPC world finals will be held in a luxurious hotel with a big ballroom. A buffet meal will be served in this ballroom, and organizers decided to decorate its walls with pictures of past champion teams. In order to avoid criticism about favouring some of those teams over others, the organizing commitee wants to make sure that all pictures are appropiately illuminated. The only direct way they’ve found for doing this is ensuring each picture has at least one lightbulb that directly illuminates it. In this way, the perimeter of the ballroom wall can be divided into illuminated parts (in which pictures may be placed) and dark parts (which are not suitable for placing the pictures). The ballroom has the shape of a box and contains several lightbulbs. Each lightbulb emits light in all directions, but this light can be blocked by columns. All columns in the ballroom have cylindrical shape and go from the floor to the ceiling, so light cannot pass over or above them. Columns are of course placed so that its circular section is parallel to the ballroom floor. Any given point p on the perimeter wall is said to be illuminated if there exists a line segment (a light ray) which starts on a lightbulb, ends in p and does not touch or pass through any column. Your task as a helper of the ICPC organization is to examine the blueprints of the ballroom and determine the total length of illuminated sections of the perimeter wall. The blueprint consist of a rectangle indicating a top view of the ballroom, with the lightbulbs and columns marked in it. 输入 Each test case will consist on several lines. The first line will contain four integers: L, the number of lightbulbs, C, the number of columns, X, the size of the ballroom on the x coordinate and Y , the size of the ballroom on the y coordinate. The lower-left corner of the ballroom is at (0, 0) while the upper-right corner is at (X, Y ). The next L lines will contain two integers each representing the x and y coordinate of each lightbulb. The last C lines of the test case will contain three integers each, representing the x and y coordinates of the center of a column and its radius, in that order. You can assume that 1 <= L,C <= 103 and 4 <= X, Y <= 106. Also, for all pairs of coordinates (x,y), 0 < x < X and 0 < y < Y , both for lightbulbs and column center locations. All radii of the columns will be positive. Finally, no two columns will overlap, although they may touch, and no column will touch or intersect with the border of the ballroom. No lightbulb will be inside a column or in its boundary and no two lightbulbs will be in the same place. Input is terminated with L = C = X = Y = 0. 输出 For each test case, output a single line with the total length of the illuminated parts of the perimeter wall. The result must be printed as a real number with exactly four decimal figures, with the lowest-order decimal figure rounded up. 样例输入 2 1 8 8 6 6 2 6 4 4 2 1 4 7 7 3 3 2 4 1 4 2 1 2 2 1 4 4 1 2 2 9 7 1 2 5 5 3 3 2 7 5 1 0 0 0 0 ⧉ 样例输出 28.0000 0.0000 25.8214 ⧉ 题目来源 用巧思
最新发布
11-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值