Problem E. GukiZ and GukiZiana

Solution:

  先分成N=sqrt(n)块,然后对这N块进行排序。

利用二分查找确定最前面和最后面的位置。

  

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. vector<int> s[];
  7. ll add[], a[], pos[];
  8. ll n, q, bk, N;
  9.  
  10. bool cmp( int x, int y )
  11. {
  12. if( a[x] == a[y] ) return x < y;
  13. return a[x] < a[y];
  14. }
  15.  
  16. inline void modify( ll l, ll r, ll x )
  17. {
  18. int k = pos[l], t = pos[r];
  19.  
  20. if( k == t ) {
  21. for( ll i = l; i <= r; ++i )
  22. a[i] += x;
  23. sort( s[k].begin(), s[k].end(), cmp );
  24. return ;
  25. }
  26.  
  27. for( ll i = k + ( pos[l - ] == k ); i <= t - ( pos[r + ] == t ) ; ++i )
  28. add[i] += x;
  29.  
  30. if( pos[l - ] == k ) {
  31. for( ll i = l; pos[i] == k; ++i ) {
  32. a[i] += x;
  33. }
  34. sort( s[k].begin(), s[k].end(), cmp );
  35. }
  36.  
  37. if( pos[r + ] == t ) {
  38. for( ll i = r; pos[i] == t; --i ) {
  39. a[i] += x;
  40. }
  41. sort( s[t].begin(), s[t].end(), cmp );
  42. }
  43. }
  44.  
  45. inline ll query( ll x )
  46. {
  47. int l = -, r = -, i;
  48. for( i = ; i <= N; ++i ) {
  49. a[] = x - add[i];
  50. auto it = lower_bound( s[i].begin(), s[i].end(), , cmp );
  51. if( it == s[i].end() ) continue;
  52. if( a[*it] + add[i] == x ) {
  53. l = *it;
  54. break;
  55. }
  56. }
  57.  
  58. if( l == - ) return l;
  59. for( int j = N; j >= i; --j ) {
  60. a[n + ] = x - add[j];
  61. auto it = lower_bound( s[j].begin(), s[j].end(), n + , cmp );
  62. if( it == s[j].begin() ) continue;
  63. it--;
  64. if( a[*it] + add[j] == x ) {
  65. r = *it;
  66. break;
  67. }
  68. }
  69. return r - l;
  70. }
  71.  
  72. int main()
  73. {
  74. ios::sync_with_stdio( );
  75. cin >> n >> q;
  76. bk = ceil( sqrt( .*n ) + 0.005 );
  77. for( int i = ; i <= n; ++i ) {
  78. cin >> a[i];
  79. pos[i] = ( i - ) / bk + ;
  80. s[pos[i]].push_back( i );
  81. }
  82. N = ( n - ) / bk + ;
  83. for( int i = ; i <= N; ++i ) {
  84. sort( s[i].begin(), s[i].end(), cmp );
  85. }
  86. ll cmd, l, r, x;
  87. for( int i = ; i <= q; ++i ) {
  88. cin >> cmd;
  89. if( cmd == ) {
  90. cin >> l >> r >> x;
  91. modify( l, r, x );
  92. } else {
  93. cin >> x;
  94. cout << query( x ) << "\n";
  95. }
  96. }
  97. }

Codeforces 551E - GukiZ and GukiZiana(分块)的更多相关文章

  1. Codeforces 551E GukiZ and GukiZiana(分块思想)

    题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...

  2. CodeForces 551E GukiZ and GukiZiana

    GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  3. CF 551E. GukiZ and GukiZiana [分块 二分]

    GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...

  4. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块

    E. GukiZ and GukiZiana Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

  5. Codeforces 307 div2 E.GukiZ and GukiZiana 分块

    time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...

  6. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)

    E. GukiZ and GukiZiana time limit per test 10 seconds memory limit per test 256 megabytes input stan ...

  7. Codeforces 551 E - GukiZ and GukiZiana

    E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...

  8. [codeforces551E]GukiZ and GukiZiana

    [codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...

  9. (分块)GukiZ and GukiZiana CodeForces - 551E

    题意: 给你一段序列,并且有两种操作 操作①:将序列中从l-r每个元素加上x 操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1 思路: 直接分块暴力即可 对于区间加,普通标记加 ...

随机推荐

  1. bzoj 1069 [SCOI2007]最大土地面积(旋转卡壳)

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2277  Solved: 853[Submit][Stat ...

  2. 初学python(print使用、条件分支、循环、模块引用)

    import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...

  3. Code Forces 711C Coloring Trees

    C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Idea实现WebService实例 转

    作者:http://blog.csdn.net/dreamfly88/article/details/52350370 因为工作需要,数据传输部分需要使用webservice实现,经过两天的研究,实现 ...

  5. 使用CLRMD编写一个自己的C#调试器

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用CLRMD编写一个自己的C#调试器.

  6. redis学习大全

     http://blog.csdn.net/menergy/article/details/17577985 http://blog.sina.com.cn/s/blog_64008ed70102uy ...

  7. 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)

    目前国内对于双卡智能手机的需求还是很大的,各种复杂的业务会涉及到双卡模块:而android标准的api又不提供对双卡的支持.导致国内双卡模块标准混乱,各个厂商各玩各的.目前我知道的双卡解决方案就有:m ...

  8. Java JDBC批处理插入数据操作(转)

    在此笔记里,我们将看到我们如何可以使用像Statement和PreparedStatement JDBC API来批量在任何数据库中插入数据.此外,我们将努力探索一些场景,如在内存不足时正常运行,以及 ...

  9. iOS内存泄漏自动检测工具PLeakSniffer

    新款objective-C内存泄漏自动检测工具 PLeakSniffer , GitHub地址 (https://github.com/music4kid/PLeakSniffer). 背景 前些天读 ...

  10. HTTP基础:URL格式、 HTTP请求、响应、消息

    HTTP URL 格式: http://host[:port][abs_path] 其中http表示要通过HTTP协议来定位网络资源. host表示合法的Internet主机域名或IP地址(以点分十进 ...