[HNOI/AHOI2017]影魔

题目大意:

有一排\(n(n\le2\times10^5)\)个数\(k_{1\sim n}\)。对于点对\((i,j)\),若不存在\(k_s(i<s<j)\)大于\(k_i\)或\(k_j\), 则对答案造成\(p_1\)的贡献;若\(c=\max_{s\in(i,j)}\{k_s\}\)满足\(k_i<c<k_j\)或\(k_j<c<k_i\)则对答案造成\(p_2\)的贡献。\(m(m\le2\times10^5)\)次询问,每次询问区间\([l,r]\)内所有点对对答案贡献之和。其中\(k_i\)为\(1\sim n\)的全排列。

思路:

首先预处理出对于每个点\(i\),其左侧第一个权值大于它的点\(left[i]\)和其右侧第一个权值大于它的点\(right[i]\)。显然这个点\(i\)对答案的贡献有\(3\)种情况:

  1. 对于点对\((left[i],right[i])\),贡献为\(p_1\);
  2. 对于所有点对\((l\in(left[i],i),right[i])\),贡献为\(p_2\);
  3. 对于所有点对\((left[i],r\in(i,right[i]))\),贡献为\(p_2\)。

我们可以离线处理所有询问。将询问和贡献分别排序,用树状数组维护答案即可。

源代码:

  1. #include<cstdio>
  2. #include<cctype>
  3. #include<algorithm>
  4. typedef long long int64;
  5. inline int getint() {
  6. register char ch;
  7. while(!isdigit(ch=getchar()));
  8. register int x=ch^'0';
  9. while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
  10. return x;
  11. }
  12. const int N=2e5+2,M=2e5;
  13. int64 ans[M];
  14. int n,m,p1,p2,w[N],left[N],right[N];
  15. struct Query {
  16. int p,id,sgn,l,r;
  17. bool operator < (const Query &rhs) const {
  18. return p<rhs.p;
  19. }
  20. };
  21. Query q[M*2];
  22. struct Modify {
  23. int p,l,r,v;
  24. bool operator < (const Modify &rhs) const {
  25. return p<rhs.p;
  26. }
  27. };
  28. Modify mo[M*3];
  29. class FenwickTree {
  30. private:
  31. int64 val1[N],val2[N];
  32. int lowbit(const int &x) const {
  33. return x&-x;
  34. }
  35. public:
  36. void modify(const int &p,const int &x) {
  37. for(register int i=p;i<=n;i+=lowbit(i)) {
  38. val1[i]+=x;
  39. val2[i]+=p*x;
  40. }
  41. }
  42. int64 query(const int &p) const {
  43. int64 ret=0;
  44. for(register int i=p;i;i-=lowbit(i)) {
  45. ret+=(p+1)*val1[i]-val2[i];
  46. }
  47. return ret;
  48. }
  49. };
  50. FenwickTree t;
  51. class SegmentTree1 {
  52. #define _left <<1
  53. #define _right <<1|1
  54. private:
  55. int max[N<<2];
  56. void push_up(const int &p) {
  57. max[p]=std::max(max[p _left],max[p _right]);
  58. }
  59. public:
  60. void modify(const int &p,const int &b,const int &e,const int &x,const int &v) {
  61. if(b==e) {
  62. max[p]=v;
  63. return;
  64. }
  65. const int mid=(b+e)>>1;
  66. if(x<=mid) modify(p _left,b,mid,x,v);
  67. if(x>mid) modify(p _right,mid+1,e,x,v);
  68. push_up(p);
  69. }
  70. int query(const int &p,const int &b,const int &e,const int &x) const {
  71. if(b==e) return b;
  72. const int mid=(b+e)>>1;
  73. if(max[p _right]>x) return query(p _right,mid+1,e,x);
  74. return query(p _left,b,mid,x);
  75. }
  76. #undef _left
  77. #undef _right
  78. };
  79. SegmentTree1 st1;
  80. class SegmentTree2 {
  81. #define _left <<1
  82. #define _right <<1|1
  83. private:
  84. int max[N<<2];
  85. void push_up(const int &p) {
  86. max[p]=std::max(max[p _left],max[p _right]);
  87. }
  88. public:
  89. void modify(const int &p,const int &b,const int &e,const int &x,const int &v) {
  90. if(b==e) {
  91. max[p]=v;
  92. return;
  93. }
  94. const int mid=(b+e)>>1;
  95. if(x<=mid) modify(p _left,b,mid,x,v);
  96. if(x>mid) modify(p _right,mid+1,e,x,v);
  97. push_up(p);
  98. }
  99. int query(const int &p,const int &b,const int &e,const int &x) const {
  100. if(b==e) return b;
  101. const int mid=(b+e)>>1;
  102. if(max[p _left]>x) return query(p _left,b,mid,x);
  103. return query(p _right,mid+1,e,x);
  104. }
  105. #undef _left
  106. #undef _right
  107. };
  108. SegmentTree2 st2;
  109. int main() {
  110. n=getint(),m=getint(),p1=getint(),p2=getint();
  111. for(register int i=1;i<=n;i++) w[i]=getint();
  112. for(register int i=1,pos=0;i<=n;i++) {
  113. left[i]=w[i]<w[pos]?st1.query(1,1,n,w[i]):0;
  114. if(w[i]>w[pos]) pos=i;
  115. st1.modify(1,1,n,i,w[i]);
  116. }
  117. for(register int i=n,pos=n+1;i>=1;i--) {
  118. right[i]=w[i]<w[pos]?st2.query(1,1,n,w[i]):n+1;
  119. if(w[i]>w[pos]) pos=i;
  120. st2.modify(1,1,n,i,w[i]);
  121. }
  122. for(register int i=0;i<m;i++) {
  123. const int l=getint(),r=getint();
  124. ans[i]=(r-l)*p1;
  125. q[i*2]=(Query){l-1,i,-1,l,r};
  126. q[i*2+1]=(Query){r,i,1,l,r};
  127. }
  128. std::sort(&q[0],&q[m*2]);
  129. int cnt=0;
  130. for(register int i=1;i<=n;i++) {
  131. if(left[i]!=0&&right[i]!=n+1) mo[cnt++]=(Modify){right[i],left[i],left[i],p1};
  132. if(left[i]!=0&&right[i]!=i+1) mo[cnt++]=(Modify){left[i],i+1,right[i]-1,p2};
  133. if(left[i]!=i-1&&right[i]!=n+1) mo[cnt++]=(Modify){right[i],left[i]+1,i-1,p2};
  134. }
  135. std::sort(&mo[0],&mo[cnt]);
  136. for(register int i=0,j=0;j<m*2;j++) {
  137. for(;i<cnt&&mo[i].p<=q[j].p;i++) {
  138. t.modify(mo[i].r+1,-mo[i].v);
  139. t.modify(mo[i].l,mo[i].v);
  140. }
  141. ans[q[j].id]+=q[j].sgn*(t.query(q[j].r)-t.query(q[j].l-1));
  142. }
  143. for(register int i=0;i<m;i++) {
  144. printf("%lld\n",ans[i]);
  145. }
  146. return 0;
  147. }

[HNOI/AHOI2017]影魔的更多相关文章

  1. [HNOI2017/AHOI2017]影魔

    Description: 奈文摩尔有 \(n\) 个灵魂,他们在影魔宽广的体内可以排成一排,从左至右标号 \(1\) 到 \(n\).第 \(i\) 个灵魂的战斗力为 \(k_i\),灵魂们以点对的形 ...

  2. [HNOI 2017]影魔

    Description 题库链接 给你一段长度为 \(n\) 的序列 \(K\) . \(m\) 组询问,每次给定左右端点 \(l,r\) .求出满足区间内下述贡献和. 如果一个区间的两个端点是这一个 ...

  3. 【HNOI 2017】影魔

    Problem Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还 ...

  4. HNOI做题记录

    算是--咕完了? 2013.2014的就咕了吧,年代太久远了,并且要做的题还有那么多-- LOJ #2112. 「HNOI2015」亚瑟王 发现打出的概率只和被经过几次有关. 于是\(dp_{i,j} ...

  5. 图论(网络流):[HNOI 2013]切糕

    [HNOI 2013]切糕 第三题:切糕(程序文件名:cake.exe)100 分,运行时限:5s 经过千辛万苦小A 得到了一块切糕,切糕的形状是长方体,小A 打算拦腰将切糕切成两半分给小B.出于美观 ...

  6. bzoj4826 [Hnoi2017]影魔

    Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵 ...

  7. CJOJ 1308 【HNOI 2002 】营业额统计 / CodeVS 1296 营业额统计(STL,二分)

    CJOJ 1308 [HNOI 2002 ]营业额统计 / CodeVS 1296 营业额统计(STL,二分) Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一 ...

  8. [补档][HNOI 2008]GT考试

    [HNOI 2008]GT考试 题目 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2... ...

  9. BZOJ:4826: [Hnoi2017]影魔

    Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵 ...

随机推荐

  1. 关于 zfs 命令相关介绍

    三种设备:filesystem  volume  snapshot 1.zfs listroot@UA4300D-spa:~/hanhuakai/pro_07/git_0708# zfs listNA ...

  2. 最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)(转)

    原文转自 https://blog.csdn.net/leixiaohua1020/article/details/25346147/ 伴随着毕业论文的完成,这两天终于腾出了空闲,又有时间搞搞FFMP ...

  3. python爬虫模块之调度模块

    调度模块也就是对之前所以的模块的一个调度,作为一个流水的入口. 下面的代码的获取数据部分暂时没有写,细节部分在实际开发中,要根据要求再定义,这里说的是使用方法 from savedb import D ...

  4. sicily 1063. Who's the Boss

    Time Limit: 1sec    Memory Limit:32MB  Description Several surveys indicate that the taller you are, ...

  5. 【bzoj4896】补退选

    傻逼题. 每个点维护下vector,然后随便做. #include<bits/stdc++.h> ; using namespace std; typedef long long ll; ...

  6. FineReport——自定义控件实现填报提交事件和校验

    在报表内部或者在引用报表的HTML页面,定义一个按钮标签,通过FR提供的方法实现提交功能. <button onclick="_g('${sessionID}').writeRepor ...

  7. leetcode 之Reorder List(25)

    找到中间结点,将后半部分反转接入即可. ListNode *reoderList(ListNode* head) { if (head == nullptr || head->next == n ...

  8. linux命令行任务管理

    今天看到了linux命令行的任务管理命令感觉很实用: 1.ctrl+z  将当前前台执行的任务放到后台并暂停 2.fg恢复上次放入后台的任务 这两个命令组合起来很实用,比如在linux命令行中写pyt ...

  9. 循环select查询结果集

    --标记id --每次查询特定列比标记id大的第一条数据, --同时更新标记id,直到查询结果为空 ) set @id='' begin @id=id from T_SGZ where id>@ ...

  10. hihocoder 1145 : 幻想乡的日常

    #1145 : 幻想乡的日常 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 幻想乡一共有n处居所,编号从1到n.这些居所被n-1条边连起来,形成了一个树形的结构. 每处 ...