题意:给你一个长度为n的只含有1和0的字符串,w个询问,每次询问输入l,r;在[l,r]中在l+k-1、l+2*k-1、......r的位置都必须为1,如果不为1的,变成1,记为一次操作,其它的地方的都必须为0,不为0的地方要变成1,也记为一次操作,最后问在区间[l,r]最少几次操作。

思路:可以用树状数组记录在某个地方的右方有多少个1,然后在 预处理出从1,2,..k的为开头的在i+c*k-1的位置上前面有多少个1,最后的答案是拿走的1+添加的1,拿走的1的个数等于现有的1的个数-位置正确的1的个数。 添加的1的个数等于需要添加的个数的总数-位置正确的1的个数。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 100010
 5 using namespace std;
 6
 7 int n,k,w;
 8 char str[maxn];
 9 int a[maxn];
10 int c[maxn];
11 int dp[maxn];
12 int sum[maxn];
13
14 int lowbit(int x)
15 {
16     return x&-x;
17 }
18
19 void insert(int x,int d)
20 {
21     while(x<maxn)
22     {
23        c[x]+=d;
24        x+=lowbit(x);
25     }
26 }
27
28 int Getsum(int x)
29 {
30     int ans=0;
31     while(x>0)
32     {
33         ans+=c[x];
34         x-=lowbit(x);
35     }
36     return ans;
37 }
38
39
40 int main()
41 {
42     while(scanf("%d%d%d",&n,&k,&w)!=EOF)
43     {
44          scanf("%s",str+1);
45          memset(sum,0,sizeof(sum));
46          memset(a,0,sizeof(a));
47          memset(c,0,sizeof(c));
48          for(int i=1; i<=n; i++)
49          {
50             if(str[i]=='1')
51             {
52                 insert(i,1);
53             }
54          }
55          for(int i=1; i<=k; i++)
56          {
57              int cnt=0;
58              for(int c=1; i+c*k-1<=n; c++)
59              {
60                   cnt+=(str[i+c*k-1]-'0');
61                   dp[i+c*k-1]=cnt;
62              }
63          }
64          for(int i=1; i<=w; i++)
65          {
66              int l,r;
67              int ans=0;
68              scanf("%d%d",&l,&r);
69              int num=(r-l+1)/k;
70              ans+=Getsum(r)-Getsum(l-1);
71              ans-=dp[r]-dp[l-1];
72              ans+=num;
73              ans-=dp[r]-dp[l-1];
74              printf("%d\n",ans);
75          }
76     }
77     return 0;
78 }

View Code

转载于:https://www.cnblogs.com/fanminghui/p/4250839.html

cf C. Inna and Candy Boxes相关推荐

  1. Codeforces #229 D2C:Inna and Candy Boxes

    题目:http://codeforces.com/contest/390/problem/C 参考:http://codeforces.com/blog/Berezin 第一次参加比赛,卡在这道题了, ...

  2. no scp yes 不提示_linux脚本实现scp命令自动输入密码和yes/no等确认信息

    TypeScript 素描-变量声明 博文读自 TypeScript 官方文档而来,不具有学习性,仅是本人学习时记录以供日后翻阅 ,有学习TypeScript的朋友还请去看更为详细的官方文档 /* 变 ...

  3. Codeforces Round #229

    390 A. Inna and Alarm Clock http://codeforces.com/contest/390/problem/A 水题一枚,两个set就可完成 1 #include< ...

  4. ACM-ICPC 2018 焦作赛区网络预赛 J(二分+JAVA高精)

    传送门 题面: 65536K Jessie and Justin want to participate in e-sports. E-sports contain many games, but t ...

  5. Codeforces Round #278 (Div. 2)

    A. Giga Tower 模拟即可,略过 B. Candy Boxes 四个整数x1..x4,满足x1<=x2<=x3<=x4,假如sum x1..x4/4 == x2+x3 /2 ...

  6. Participate in E-sports【Java大数+二分】

    Participate in E-sports 时间限制: 2 Sec 内存限制: 128 MB 提交: 194 解决: 53 [提交] [状态] [命题人:admin] 题目描述 Jessie an ...

  7. J: Participate in E-sports [大数牛顿迭代判断是否是平方数]

    J: Participate in E-sports [大数牛顿迭代判断是否是平方数] **题目描述 Jessie and Justin want to participate in e-sports ...

  8. games+in+java_Participate in E-sports【Java大数+二分】

    Participate in E-sports 时间限制: 2 Sec 内存限制: 128 MB 提交: 194 解决: 53 [提交] [状态] [命题人:admin] 题目描述 Jessie an ...

  9. UPC Participate in E-sports(参加电子竞技)(Biginteger的平方根:二分或牛顿迭代法)

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

最新文章

  1. 老男孩博客获三大搜素引擎搜索自然排名第一位(百度谷歌搜狗)
  2. java命令执行类,这里设置了classpath,系统变量里的classpath将失效
  3. 自定义ClassLoader实现java应用核心逻辑模块热部署
  4. 上传附件每次都是上传中。_起标题头疼?每次卡在标题上 我都回顾这7种方法 创作灵感就来了...
  5. cc、gcc、g++ 的区别和联系
  6. SQL语句之left join、right join、inner join的区别
  7. 不一样 使用别名 数据字段和bean_【修炼内功】[spring-framework] [3] Bean是如何创建又是如何销毁的?...
  8. 从微信浏览器,调起本地应用,最简单的解决方案
  9. php mysql工单_详解使用PHP开发客服工单系统
  10. linux替换windows换行符_vim编辑器的查找与替换
  11. Windows 8 Beta 64位 简体中文 消费者预览版 安装截图(30P)
  12. UVA11876 N + NOD (N)【欧拉筛法+前缀和】
  13. js中的null VS undefined
  14. 输入年份和月份输出该月有多少天python_输入年份和月份,输出该月有多少天,判断这一天是该年的第几天...
  15. linux 系统定时任务 服务 详解
  16. 电脑开机黑屏错误代码U盘重装系统教学
  17. 嘉鱼县开展寒冬送暖志愿服务活动
  18. Oracle数据库精讲与疑难解析(第2版)
  19. 计算机二级题目之字符串练习学习
  20. 使用NTP协议获取网络时间代码

热门文章

  1. pycharm上python项目的导出_pycharm项目打包成exe
  2. java 连接oracle_「事件驱动架构」使用GoldenGate创建从Oracle到Kafka的CDC事件流
  3. 二叉树 中序遍历 python_leetcode No.105 从前序与中序遍历序列构造二叉树
  4. 计算机网络—GBN协议(后退N帧协议)
  5. 2013-2017蓝桥杯省赛C++A组真题总结(题型及解法)
  6. SQL注入漏洞(原理;网页注入)
  7. 牛客网暑期ACM多校训练营(第三场): C. Shuffle Cards(splay)
  8. “玲珑杯”郑州轻工业学院第八届ACM程序设计大赛Problem G: 蛤玮点菜
  9. python爬虫爬取慕课网中的图片
  10. [paper reading] 译 + 注 :如何阅读 Research Papers(Andrew Ng)