【上海网络赛】B.Light bulbs

题目描述

There are NN light bulbs indexed from 00 to N-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) means to flip all bulbs xx such that L \leq x \leq RL≤x≤R. So for example, FLIP(3, 5)FLIP(3,5) means to flip bulbs 33 , 44 and 55, and FLIP(5, 5)FLIP(5,5) means to flip bulb 55.

Given the value of NN and a sequence of MM 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. TT test cases follow. Each test case starts with a line containing two integers NN and MM, the number of light bulbs and the number of operations, respectively. Then, there are MM more lines, the ii-th of which contains the two integers L_iLi? and R_iRi?, indicating that the ii-th operation would like to flip all the bulbs from L_iLi? to R_iRi? , inclusive.

1 \leq T \leq 10001≤T≤1000

1 \leq N \leq 10^61≤N≤106

1 \leq M \leq 10001≤M≤1000

0 \leq L_i \leq R_i \leq N-10≤Li?≤Ri?≤N−1

OutputFile

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy 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
代码线段树会爆内存直接搞差分法会t想的是以类似于括号匹配的思路,每一个端点记录位置和左端点还是右端点。因为“先操作2-6再操作4-8”与“先操作2-8再操作4-6”的结果是一样的,所以可以按照端点从小到大,左端点在右端点前的顺序排序。排序后做一个栈并记录栈内元素,遍历排序后的数组,左端点入栈,遇右端点时,若当前栈内元素个数为奇数,ans加上这段的灯泡数;若当前为偶数,ans减去这段灯泡数(都要记得加一,这是个两头都种树的植树问题)。左端点退栈其实思路我也没理太清楚反正过了……先记下来再说()
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e6+5;
 4 int table[maxn];
 5 struct node{
 6     int n;
 7     int x;///1表示左端点,0表示右端点
 8 }b[2005];
 9 bool cmp(struct node x,struct node y)
10 {
11     if (x.n==y.n) return x.x>y.x;
12     return x.n<y.n;
13 }
14 int s[2005];
15 int main()
16 {
17     int t,n,m,l,r,cnt;
18     scanf("%d",&t);
19     for (int kase=1;kase<=t;kase++){
20         memset(table,0,sizeof(table));
21         //memset(b,0,sizeof(b));
22         cnt=0;
23         struct node n1,n2;
24         scanf("%d %d",&n,&m);
25         for (int i=0;i<m;i++){
26             scanf("%d %d",&l,&r);
27             n1.n=l;
28             n2.n=r;
29             n1.x=1;n2.x=0;
30             b[cnt]=n1;
31             b[cnt+1]=n2;
32             cnt+=2;
33         }
34         sort(b,b+cnt,cmp);
35         int ans=0;int k=0;
36         for (int i=0;i<cnt;i++){
37             if (b[i].x==1){
38                 s[k]=b[i].n;
39                 k++;
40             }
41             else{
42                 if (k%2!=0) ans+=b[i].n+1-s[k-1];
43                 else ans=-(b[i].n+1-s[k-1]-ans);
44                 k--;
45             }
46         }
47 //        long long tcnt=table[b[0]];
48 //        for (int i=1;i<cnt+1;i++){
49 //            if (tcnt%2!=0) ans+=b[i]-b[i-1];
50 //            tcnt+=table[b[i]];
51 //        }
52         printf("Case #%d: %d\n",kase,ans);
53     }
54     return 0;
55 }

八百年了我终于把这题补上了(安详)

原文地址:https://www.cnblogs.com/sixwater6H2O/p/11594920.html

时间: 2024-07-30 13:58:25

【上海网络赛】B.Light bulbs的相关文章

2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)

题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) 到 \(N - 1\)),初始都是关闭的. 给定 \(M\) 个操作,每个操作包含 \(L\) 和 \(R\),对 \([L, R]\) 内的所有灯泡改变状态. 求最后有几个灯泡是亮的. 思路 题目挺简单的,翻转奇数次的灯泡是亮的,所以要求每个灯泡翻转的次数. 容易想到可以用差分. 对所有操作的两

2019上海icpc网络赛B. Light bulbs(思维+差分)

题目传送门 题意 T组案例,每组案例:n个灯泡(from 0 to n-1),m次操作,每次操作把区间[L,R]内的灯泡翻转(开变关,关变开),问m次操作之后有多少灯泡是亮着的.(时间限制:1000ms  内存限制:8192K) 题解 这道题不仅卡时间,更是卡内存,所以用线段树会爆内存 正解: 该题可以转换为经典的差分问题:每次操作对[L,R]的所有数进行+1操作,求最后有多少个奇数.(设该数组为a[n],每次操作a[L]+1,a[R+1]-1,求前缀和sum[i]=sum[i-1]+a[i]即

2014上海网络赛 HDU 5053 the Sum of Cube

水 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<math.h> 4 #include<iostream> 5 #define LL long long 6 using namespace std; 7 8 int main() 9 { 10 int t; 11 int a,b; 12 int cas; 13 LL sum; 14 while(~scanf("%d",&

2015上海网络赛 HDU 5478 Can you find it 数学

HDU 5478 Can you find it 题意略. 思路:先求出n = 1 时候满足条件的(a,b), 最多只有20W对,然后对每一对进行循环节判断即可 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <

2015上海网络赛 HDU 5475 An easy problem 线段树

题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<vector> 8 #include<set> 9 #include<stri

2015上海网络赛 A Puzzled Elena

题意:给定一棵树,求这个节点的所有子树中包括他本身与它互质的节点的个数. 解题思路:题利用dfs序+容斥原理+前缀和性质解决.题目中要求每个结点,和多少个它的子结点互素.如果每次为了求一个点去跑一遍dfs,复杂度将是 O(N(N+M)).一定会超时.因此需要深入加以分析.注意到n的范围是10^5以内的,因此可以事先用线性筛求出每个数含有哪些素因子.接下来,我们 尝试利用dfs序来求解.设num[i]表示遍历到当前结点时候,含有因数i(注意,不一定是素数)的结点个数.可以发现,如果第一次遍历到结点

HDU 5044(2014 ACM-ICPC上海网络赛)

题意:给定一个树形图,节点10^5,有两种操作,一种是把某两点间路径(路径必定唯一)上所有点的权值增加一个固定值. 另一种也是相同操作,不同的是给边加权值.操作次数10^5.求操作过后,每个点和每条边的权值. 分析:此题时间卡得非常紧,最好用输入外挂,最好不要用RMQ来求解LCA. 此题是典型的在线LCA问题,先讲讲在线LCA要怎么做. 在线LCA有两种方法,第一种比较常见,即将其转化成RMA问题. 先对树形图进行深度优先遍历,遍历过程记录路线中点的途经序列,每个非叶子节点会在序列中出现多次,从

HDU 5045 5047 5050 5053(上海网络赛E,F,I,L)

HDU 5045 5047 5050 5053 太菜了,名额差点没保住,吓尿..赶紧开刷树链抛分 5045:状压DP,压缩10个人,由于两个人不能差2以上,所以可以用01表示 5047:推推公式即可,每次交线多4条 5050:求GCD,用java大叔即可 5053:签到题 代码: 5045: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N

hdu 5053 the Sum of Cube(上海网络赛)

the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 405    Accepted Submission(s): 224 Problem Description A range is given, the begin and the end are both integers. You should sum