非常简单的单点修改+区间加+区间查询。我用的是最近刚学的区间修改版本树状数组。
  直接维护即可,注意修改后的单点值已经不是a[i],或者b[i],要通过区间查询求单点。不然是错的。

区间修改版本树状数组:

  1. #include<iostream>
  2. #include<string.h>
  3. #include<stdio.h>
  4. #include<algorithm>
  5. #define LL long long
  6. using namespace std;
  7. LL c_p[];
  8. LL sum_p[];
  9. LL c_y[];
  10. LL sum_y[];
  11. LL a[];
  12. LL b[];
  13. int n,m;
  14. int cnt1;
  15. int cnt2;
  16. int lowbit(int x){
  17. return x&(-x);
  18. }
  19. void update(int x,int w,LL c[],LL sum[]){
  20. for (int i=x;i<=n;i+=lowbit(i)){
  21. c[i]+=(LL)w;
  22. sum[i]+=(LL)w*(x-);
  23. }
  24. }
  25. LL sum(int x,LL c[],LL sum[]){
  26. LL ans=;
  27. for (int i=x;i>;i-=lowbit(i)){
  28. ans+=(LL)x*c[i]-sum[i];
  29. }
  30. return ans;
  31. }
  32. int main(){
  33.  
  34. while(~scanf("%d%d",&n,&m)){
  35. a[]=;
  36. cnt1=;
  37. cnt2=;
  38. for (int i=;i<=n;i++){
  39. scanf("%lld",&a[i]);
  40. update(i,a[i]-a[i-],c_p,sum_p);
  41. }
  42. b[]=;
  43. for (int i=;i<=n;i++){
  44. scanf("%lld",&b[i]);
  45. update(i,b[i]-b[i-],c_y,sum_y);
  46. }
  47. int ss=;
  48. while(m--){
  49. ss++;
  50. char op[];
  51. char to;
  52. int x,y,w;
  53. scanf("%s",op);
  54. if (op[]=='i'){
  55. scanf(" %c",&to);
  56. scanf("%d%d",&x,&y);
  57. if (to=='P'){
  58. int lw=sum(x,c_p,sum_p)-sum(x-,c_p,sum_p);
  59. int rw=sum(y,c_p,sum_p)-sum(y-,c_p,sum_p);
  60. update(x,rw-lw,c_p,sum_p);
  61. update(x+,lw-rw,c_p,sum_p);
  62. update(y,lw-rw,c_p,sum_p);
  63. update(y+,rw-lw,c_p,sum_p);
  64. }else {
  65. int lw=sum(x,c_y,sum_y)-sum(x-,c_y,sum_y);
  66. int rw=sum(y,c_y,sum_y)-sum(y-,c_y,sum_y);
  67. update(x,rw-lw,c_y,sum_y);
  68. update(x+,lw-rw,c_y,sum_y);
  69. update(y,lw-rw,c_y,sum_y);
  70. update(y+,rw-lw,c_y,sum_y);
  71. }
  72. }
  73. else if (op[]=='u'){
  74. scanf(" %c",&to);
  75. int l,r;
  76. scanf("%d%d%d",&l,&r,&w);
  77. if (to=='P'){
  78. update(l,w,c_p,sum_p);
  79. update(r+,-w,c_p,sum_p);
  80. }else {
  81. update(l,w,c_y,sum_y);
  82. update(r+,-w,c_y,sum_y);
  83. }
  84. }else if (op[]=='t'){
  85. scanf(" %c",&to);
  86. scanf("%d%d",&x,&y);
  87. if (to=='P'){
  88. w=sum(x,c_y,sum_y)-sum(x-,c_y,sum_y);
  89. update(x,-w,c_y,sum_y);
  90. update(x+,w,c_y,sum_y);
  91. update(y,w,c_p,sum_p);
  92. update(y+,-w,c_p,sum_p);
  93. }else {
  94. w=sum(x,c_p,sum_p)-sum(x-,c_p,sum_p);
  95. update(x,-w,c_p,sum_p);
  96. update(x+,w,c_p,sum_p);
  97. update(y,w,c_y,sum_y);
  98. update(y+,-w,c_y,sum_y);
  99. }
  100. }else {
  101. int l,r;
  102. scanf("%d%d",&l,&r);
  103. LL num_p=sum(r,c_p,sum_p)-sum(l-,c_p,sum_p);
  104. LL num_y=sum(r,c_y,sum_y)-sum(l-,c_y,sum_y);
  105. if (num_p>num_y){
  106. cnt1++;
  107. printf("P %lld\n",num_p);
  108. }else {
  109. cnt2++;
  110. printf("Y %lld\n",num_y);
  111. }
  112. }
  113. }
  114. if (cnt1>cnt2){
  115. printf("little P is winner!\n");
  116. }else if (cnt1<cnt2){
  117. printf("little Y is winner!\n");
  118. }else {
  119. printf("five five open\n");
  120. }
  121. }
  122. return ;
  123. }

线段树版本:。。。更新laze标记,以及sum的时候,一定要用+=,不要用=,因为往下更新的时候,有可能不为0。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string.h>
  4. #include<stdio.h>
  5. #define LL long long
  6. using namespace std;
  7. struct node{
  8. int l,r;
  9. LL laze;
  10. LL sum;
  11. }tree[][<<];
  12. int a[];
  13. int b[];
  14. int cnt1;
  15. int cnt2;
  16. inline int L(int r){return r<<;};
  17. inline int R(int r){return r<<|;};
  18. inline int MID(int l,int r){return (l+r)>>;};
  19. void push_down(int root,node tre[]){
  20. if (tre[root].laze){
  21. tre[L(root)].laze+=tre[root].laze;
  22. tre[R(root)].laze+=tre[root].laze;
  23. tre[L(root)].sum+=(LL)tre[root].laze*(tre[L(root)].r-tre[L(root)].l+);
  24. tre[R(root)].sum+=(LL)tre[root].laze*(tre[R(root)].r-tre[R(root)].l+);
  25. tre[root].laze=;
  26. }
  27. }
  28. void build(int root,int l,int r){
  29. tree[][root].l=l;
  30. tree[][root].l=l;
  31. tree[][root].r=r;
  32. tree[][root].r=r;
  33. tree[][root].laze=;
  34. tree[][root].laze=;
  35. tree[][root].sum=;
  36. tree[][root].sum=;
  37. if (l==r){
  38. tree[][root].sum=a[l];
  39. tree[][root].sum=b[l];
  40. return;
  41. }
  42. int mid=MID(l,r);
  43. build(L(root),l,mid);
  44. build(R(root),mid+,r);
  45. tree[][root].sum=tree[][L(root)].sum+tree[][R(root)].sum;
  46. tree[][root].sum=tree[][L(root)].sum+tree[][R(root)].sum;
  47. }
  48. void update(int root,int ul,int ur,LL w,node tre[]){
  49. int l=tre[root].l;
  50. int r=tre[root].r;
  51. // cout<<l<<" "<<r<<endl;
  52. if (ul<=l && r<=ur){
  53. tre[root].sum+=(LL)(r-l+)*w;
  54. tre[root].laze+=w;
  55. return;
  56. }
  57. push_down(root,tre);
  58. int mid=MID(l,r);
  59. if (ur<=mid)
  60. update(L(root),ul,ur,w,tre);
  61. else if (ul>mid)
  62. update(R(root),ul,ur,w,tre);
  63. else {
  64. update(L(root),ul,mid,w,tre);
  65. update(R(root),mid+,ur,w,tre);
  66. }
  67. tre[root].sum=tre[L(root)].sum+tre[R(root)].sum;
  68. }
  69. LL query(int root,int ql,int qr,node tre[]){
  70. int l=tre[root].l;
  71. int r=tre[root].r;
  72. if (ql<=l && r<=qr){
  73. return tre[root].sum;
  74. }
  75. push_down(root,tre);
  76. int mid=MID(l,r);
  77. LL ans=;
  78. if (qr<=mid){
  79. ans+=query(L(root),ql,qr,tre);
  80. }else if (ql>mid){
  81. ans+=query(R(root),ql,qr,tre);
  82. }else {
  83. ans+=query(L(root),ql,mid,tre);
  84. ans+=query(R(root),mid+,qr,tre);
  85. }
  86. return ans;
  87. }
  88. int main(){
  89. int n,m;
  90. while(~scanf("%d%d",&n,&m)){
  91. for (int i=;i<=n;i++){
  92. scanf("%d",&a[i]);
  93. }
  94. for (int i=;i<=n;i++){
  95. scanf("%d",&b[i]);
  96. }
  97. build(,,n);
  98. cnt1=;
  99. cnt2=;
  100. while(m--){
  101. char op[];
  102. char pe;
  103. int x,y;
  104. LL w;
  105. scanf("%s",op);
  106. if (op[]=='i'){
  107. scanf(" %c",&pe);
  108. if(pe=='P'){
  109. scanf("%d%d",&x,&y);
  110. LL lw=query(,x,x,tree[]);
  111. LL rw=query(,y,y,tree[]);
  112. update(,x,x,rw-lw,tree[]);
  113. update(,y,y,lw-rw,tree[]);
  114. }else {
  115. scanf("%d%d",&x,&y);
  116. int lw=query(,x,x,tree[]);
  117. int rw=query(,y,y,tree[]);
  118. update(,x,x,rw-lw,tree[]);
  119. update(,y,y,lw-rw,tree[]);
  120. }
  121. }else if (op[]=='u'){
  122. scanf(" %c",&pe);
  123. if (pe=='P'){
  124. scanf("%d%d%lld",&x,&y,&w);
  125. update(,x,y,w,tree[]);
  126. }else {
  127. scanf("%d%d%lld",&x,&y,&w);
  128. update(,x,y,w,tree[]);
  129. }
  130. }else if (op[]=='t'){
  131. scanf(" %c",&pe);
  132. scanf("%d%d",&x,&y);
  133. if (pe=='P'){
  134. w=query(,x,x,tree[]);
  135. //cout<<w<<endl;
  136. update(,x,x,-w,tree[]);
  137. update(,y,y,w,tree[]);
  138. // for (int i=1;i<=n;i++){
  139. // cout<<query(1,i,i,tree[0])<<" ";
  140. // }
  141. // cout<<endl;
  142. // for (int i=1;i<=n;i++){
  143. // cout<<query(1,i,i,tree[1])<<" ";
  144. // }
  145. // cout<<endl;
  146. }else {
  147. w=query(,x,x,tree[]);
  148. update(,x,x,-w,tree[]);
  149. update(,y,y,w,tree[]);
  150. }
  151. }else {
  152. scanf("%d%d",&x,&y);
  153. LL sum1=query(,x,y,tree[]);
  154. LL sum2=query(,x,y,tree[]);
  155. if (sum1>sum2){
  156. printf("P %lld\n",sum1);
  157. cnt1++;
  158. }else {
  159. printf("Y %lld\n",sum2);
  160. cnt2++;
  161. }
  162. }
  163. }
  164. if (cnt1>cnt2){
  165. printf("little P is winner!\n");
  166. }else if (cnt1<cnt2){
  167. printf("little Y is winner!\n");
  168. }else{
  169. printf("five five open\n");
  170. }
  171. }
  172. return ;
  173. }
  174. /*
  175. 5 5
  176. 1 3 2 5 1
  177. 1 3 2 5 1
  178. mig Y 3 2
  179. */

 

Contest1692 - 2019寒假集训第三十一场 UPC 11075 Problem D 小P的国际象棋的更多相关文章

  1. “全栈2019”Java多线程第三十一章:中断正在等待显式锁的线程

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  2. 2019寒假训练营第三次作业part2 - 实验题

    热身题 服务器正在运转着,也不知道这个技术可不可用,万一服务器被弄崩了,那损失可不小. 所以, 决定在虚拟机上试验一下,不小心弄坏了也没关系.需要在的电脑上装上虚拟机和linux系统 安装虚拟机(可参 ...

  3. 2019寒假训练营第三次作业part1-网络空间安全概论第五章

    第五章 网络攻防技术 5.1 网路信息收集技术--网络踩点 黑客入侵系统之前,需要了解目标系统可能存在的: 管理上的安全缺陷和漏洞 网络协议安全缺陷与漏洞 系统安全缺陷与漏洞 黑客实施入侵过程中,需要 ...

  4. 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化

    Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...

  5. 2019牛客多校第二场F Partition problem(暴搜)题解

    题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...

  6. “全栈2019”Java多线程第三十章:尝试获取锁tryLock()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  7. “全栈2019”Java第三十一章:二维数组和多维数组详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  9. Bootstrap <基础三十一>插件概览

    在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...

随机推荐

  1. CentOS7中安装MySQL5.7

    安装必要的组件 yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio li ...

  2. PATH_SEPARATOR

    PATH_SEPARATOR是一个常量,在Linux系统中是一个" : "号,Windows上是一个";"号.所以编写程序时最好用常量 PATH_SEPARAT ...

  3. Linux 小知识翻译 - 「虚拟化技术 续」

    这次,继续聊聊「虚拟化技术」. 根据上回的介绍,虚拟化技术可以使「计算机的台数和运行的OS的个数的比例不再是1:1」.这回介绍一下如何使用这个技术. 使用方法之一,「一台计算机上运行多个OS」.从个人 ...

  4. C语言 用π/4=1-1/3+1/5-1/7+... 求π的近似值

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #include<math.h> void m ...

  5. 023合并K个链表并排序

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

  6. Eclipse JVM terminated.exit code=13

    今天,在安装Nomad PIM时碰到这个问题,因为这个应用是基于32位的Eclipse平台开发的,而我的电脑是64位的Windows 7,当然安装的JDK也是64位的,于是报错. 搜索了网上,给了许多 ...

  7. JavaScript的基本包装类型概述与基本包装类型_Number类型

    JavaScript的基本包装类型示例 为了便于操作基本类型值,javaScript 提供了 3 个特殊的引用类型:Boolean.Number和 String. 这些类型与其他引用类型相似,但同时也 ...

  8. 使用c#封装海康SDK出现无法加载 DLL“..\bin\HCNetSDK.dll”: 找不到指定的模块

    最近在研究网络摄像头的二次开发,测试了一款海康威视的网络摄像头,程序调试的时候,出现如题的报错. 调试随机自带的demo时,程序运行正常,但当把该程序引入到我自己的程序中时,就开始报错.根据开发软件包 ...

  9. executequery要求已打开且可用的connection,连接的当前状态为已关闭

    问题: executequery要求已打开且可用的connection,连接的当前状态为已关闭 错误原因: 连接的当前状态为已关闭.或者只创建了Connection对象,没有调用Connection. ...

  10. Rancher2-----了解什么是rancher以及简单部署

    个人理解:就是相当于openstack的图形化界面,或者说应用程序的图形化界面,rancher功能就是在图形化界面去管理容器,包括运行容器,创建网络,存储等:rancher有个应用商店,可以根据自己的 ...