计蒜客ICPC2019 (区域赛 上海)
链接:https://www.jisuanke.com/contest/3003
B题 Light bulbs(思维)
题意:给出n个灯泡,一开始都是暗的,给出m个询问,每个询问给出l和r,表示对该区间操作(打开或关闭),最后输出亮着的灯有多少,
一开始用线段树差分,但是要遍历1e6的数组严重超时,下面时网上借鉴的方法(膜拜)
用map标记头和尾,最后遍历所有点(map已经默认升序排序),将头每次加1,尾后面减1,对于每次操作,只有奇数次才能操作。其实带一组数模拟一次就大概了解了。
#include <cstdio>
#include <map>
using namespace std ;
const int N = 1e6 + 5 ;
map<int,int> bulb ;
int main(){
int t , n , m ;
scanf ("%d",&t) ;
for (int i = 1 ; i <= t ; i ++){
scanf ("%d%d",&n,&m);
int a , b ;
for (int j = 1 ; j <= m ; j ++){
scanf ("%d%d",&a,&b) ;
bulb[a] ++ ;
bulb[b+1] -- ;
}
int ans = 0 , sum = 0 , last = 0 ;
map<int,int>::iterator it ;
for (it = bulb.begin() ; it != bulb.end() ; it ++){
if (sum % 2 == 1)
ans += it->first - last ;
last = it->first ;
sum += it->second ;
}
bulb.clear() ;
printf ("Case #%d: %d\n",i,ans) ;
}
return 0 ;
}
L题 Digit Sum (暴力 —> 一开始还以为会超时结果不会)
#include <cstdio>
using namespace std ;
const int N = 1e6 + 5 ;
long long num[11][N] ;
int main(){
int t , n , b ;
scanf("%d",&t) ;
for (int i = 2 ; i <= 10 ; i ++){
for (int j = 1 ; j <= 1e6 ; j ++){
int ans = 0 ;
int now = j ;
while(now){
ans = ans + now % i ;
now /= i ;
}
num[i][j] = num[i][j-1] + ans ;
}
}
for(int i = 1 ; i <= t ; i ++){
scanf ("%d%d",&n,&b) ;
printf ("Case #%d: %d\n",num[b][n]);
}
return 0 ;
}
emmmmm 后面的题还没有来得及补,等补提(To Be Continue…) ;