Contest1692 - 2019寒假集训第三十一场 UPC 11075 Problem D 小P的国际象棋
非常简单的单点修改+区间加+区间查询。我用的是最近刚学的区间修改版本树状数组。
直接维护即可,注意修改后的单点值已经不是a[i],或者b[i],要通过区间查询求单点。不然是错的。
区间修改版本树状数组:
- #include<iostream>
- #include<string.h>
- #include<stdio.h>
- #include<algorithm>
- #define LL long long
- using namespace std;
- LL c_p[];
- LL sum_p[];
- LL c_y[];
- LL sum_y[];
- LL a[];
- LL b[];
- int n,m;
- int cnt1;
- int cnt2;
- int lowbit(int x){
- return x&(-x);
- }
- void update(int x,int w,LL c[],LL sum[]){
- for (int i=x;i<=n;i+=lowbit(i)){
- c[i]+=(LL)w;
- sum[i]+=(LL)w*(x-);
- }
- }
- LL sum(int x,LL c[],LL sum[]){
- LL ans=;
- for (int i=x;i>;i-=lowbit(i)){
- ans+=(LL)x*c[i]-sum[i];
- }
- return ans;
- }
- int main(){
- while(~scanf("%d%d",&n,&m)){
- a[]=;
- cnt1=;
- cnt2=;
- for (int i=;i<=n;i++){
- scanf("%lld",&a[i]);
- update(i,a[i]-a[i-],c_p,sum_p);
- }
- b[]=;
- for (int i=;i<=n;i++){
- scanf("%lld",&b[i]);
- update(i,b[i]-b[i-],c_y,sum_y);
- }
- int ss=;
- while(m--){
- ss++;
- char op[];
- char to;
- int x,y,w;
- scanf("%s",op);
- if (op[]=='i'){
- scanf(" %c",&to);
- scanf("%d%d",&x,&y);
- if (to=='P'){
- int lw=sum(x,c_p,sum_p)-sum(x-,c_p,sum_p);
- int rw=sum(y,c_p,sum_p)-sum(y-,c_p,sum_p);
- update(x,rw-lw,c_p,sum_p);
- update(x+,lw-rw,c_p,sum_p);
- update(y,lw-rw,c_p,sum_p);
- update(y+,rw-lw,c_p,sum_p);
- }else {
- int lw=sum(x,c_y,sum_y)-sum(x-,c_y,sum_y);
- int rw=sum(y,c_y,sum_y)-sum(y-,c_y,sum_y);
- update(x,rw-lw,c_y,sum_y);
- update(x+,lw-rw,c_y,sum_y);
- update(y,lw-rw,c_y,sum_y);
- update(y+,rw-lw,c_y,sum_y);
- }
- }
- else if (op[]=='u'){
- scanf(" %c",&to);
- int l,r;
- scanf("%d%d%d",&l,&r,&w);
- if (to=='P'){
- update(l,w,c_p,sum_p);
- update(r+,-w,c_p,sum_p);
- }else {
- update(l,w,c_y,sum_y);
- update(r+,-w,c_y,sum_y);
- }
- }else if (op[]=='t'){
- scanf(" %c",&to);
- scanf("%d%d",&x,&y);
- if (to=='P'){
- w=sum(x,c_y,sum_y)-sum(x-,c_y,sum_y);
- update(x,-w,c_y,sum_y);
- update(x+,w,c_y,sum_y);
- update(y,w,c_p,sum_p);
- update(y+,-w,c_p,sum_p);
- }else {
- w=sum(x,c_p,sum_p)-sum(x-,c_p,sum_p);
- update(x,-w,c_p,sum_p);
- update(x+,w,c_p,sum_p);
- update(y,w,c_y,sum_y);
- update(y+,-w,c_y,sum_y);
- }
- }else {
- int l,r;
- scanf("%d%d",&l,&r);
- LL num_p=sum(r,c_p,sum_p)-sum(l-,c_p,sum_p);
- LL num_y=sum(r,c_y,sum_y)-sum(l-,c_y,sum_y);
- if (num_p>num_y){
- cnt1++;
- printf("P %lld\n",num_p);
- }else {
- cnt2++;
- printf("Y %lld\n",num_y);
- }
- }
- }
- if (cnt1>cnt2){
- printf("little P is winner!\n");
- }else if (cnt1<cnt2){
- printf("little Y is winner!\n");
- }else {
- printf("five five open\n");
- }
- }
- return ;
- }
线段树版本:。。。更新laze标记,以及sum的时候,一定要用+=,不要用=,因为往下更新的时候,有可能不为0。
- #include<iostream>
- #include<algorithm>
- #include<string.h>
- #include<stdio.h>
- #define LL long long
- using namespace std;
- struct node{
- int l,r;
- LL laze;
- LL sum;
- }tree[][<<];
- int a[];
- int b[];
- int cnt1;
- int cnt2;
- inline int L(int r){return r<<;};
- inline int R(int r){return r<<|;};
- inline int MID(int l,int r){return (l+r)>>;};
- void push_down(int root,node tre[]){
- if (tre[root].laze){
- tre[L(root)].laze+=tre[root].laze;
- tre[R(root)].laze+=tre[root].laze;
- tre[L(root)].sum+=(LL)tre[root].laze*(tre[L(root)].r-tre[L(root)].l+);
- tre[R(root)].sum+=(LL)tre[root].laze*(tre[R(root)].r-tre[R(root)].l+);
- tre[root].laze=;
- }
- }
- void build(int root,int l,int r){
- tree[][root].l=l;
- tree[][root].l=l;
- tree[][root].r=r;
- tree[][root].r=r;
- tree[][root].laze=;
- tree[][root].laze=;
- tree[][root].sum=;
- tree[][root].sum=;
- if (l==r){
- tree[][root].sum=a[l];
- tree[][root].sum=b[l];
- return;
- }
- int mid=MID(l,r);
- build(L(root),l,mid);
- build(R(root),mid+,r);
- tree[][root].sum=tree[][L(root)].sum+tree[][R(root)].sum;
- tree[][root].sum=tree[][L(root)].sum+tree[][R(root)].sum;
- }
- void update(int root,int ul,int ur,LL w,node tre[]){
- int l=tre[root].l;
- int r=tre[root].r;
- // cout<<l<<" "<<r<<endl;
- if (ul<=l && r<=ur){
- tre[root].sum+=(LL)(r-l+)*w;
- tre[root].laze+=w;
- return;
- }
- push_down(root,tre);
- int mid=MID(l,r);
- if (ur<=mid)
- update(L(root),ul,ur,w,tre);
- else if (ul>mid)
- update(R(root),ul,ur,w,tre);
- else {
- update(L(root),ul,mid,w,tre);
- update(R(root),mid+,ur,w,tre);
- }
- tre[root].sum=tre[L(root)].sum+tre[R(root)].sum;
- }
- LL query(int root,int ql,int qr,node tre[]){
- int l=tre[root].l;
- int r=tre[root].r;
- if (ql<=l && r<=qr){
- return tre[root].sum;
- }
- push_down(root,tre);
- int mid=MID(l,r);
- LL ans=;
- if (qr<=mid){
- ans+=query(L(root),ql,qr,tre);
- }else if (ql>mid){
- ans+=query(R(root),ql,qr,tre);
- }else {
- ans+=query(L(root),ql,mid,tre);
- ans+=query(R(root),mid+,qr,tre);
- }
- return ans;
- }
- int main(){
- int n,m;
- while(~scanf("%d%d",&n,&m)){
- for (int i=;i<=n;i++){
- scanf("%d",&a[i]);
- }
- for (int i=;i<=n;i++){
- scanf("%d",&b[i]);
- }
- build(,,n);
- cnt1=;
- cnt2=;
- while(m--){
- char op[];
- char pe;
- int x,y;
- LL w;
- scanf("%s",op);
- if (op[]=='i'){
- scanf(" %c",&pe);
- if(pe=='P'){
- scanf("%d%d",&x,&y);
- LL lw=query(,x,x,tree[]);
- LL rw=query(,y,y,tree[]);
- update(,x,x,rw-lw,tree[]);
- update(,y,y,lw-rw,tree[]);
- }else {
- scanf("%d%d",&x,&y);
- int lw=query(,x,x,tree[]);
- int rw=query(,y,y,tree[]);
- update(,x,x,rw-lw,tree[]);
- update(,y,y,lw-rw,tree[]);
- }
- }else if (op[]=='u'){
- scanf(" %c",&pe);
- if (pe=='P'){
- scanf("%d%d%lld",&x,&y,&w);
- update(,x,y,w,tree[]);
- }else {
- scanf("%d%d%lld",&x,&y,&w);
- update(,x,y,w,tree[]);
- }
- }else if (op[]=='t'){
- scanf(" %c",&pe);
- scanf("%d%d",&x,&y);
- if (pe=='P'){
- w=query(,x,x,tree[]);
- //cout<<w<<endl;
- update(,x,x,-w,tree[]);
- update(,y,y,w,tree[]);
- // for (int i=1;i<=n;i++){
- // cout<<query(1,i,i,tree[0])<<" ";
- // }
- // cout<<endl;
- // for (int i=1;i<=n;i++){
- // cout<<query(1,i,i,tree[1])<<" ";
- // }
- // cout<<endl;
- }else {
- w=query(,x,x,tree[]);
- update(,x,x,-w,tree[]);
- update(,y,y,w,tree[]);
- }
- }else {
- scanf("%d%d",&x,&y);
- LL sum1=query(,x,y,tree[]);
- LL sum2=query(,x,y,tree[]);
- if (sum1>sum2){
- printf("P %lld\n",sum1);
- cnt1++;
- }else {
- printf("Y %lld\n",sum2);
- cnt2++;
- }
- }
- }
- if (cnt1>cnt2){
- printf("little P is winner!\n");
- }else if (cnt1<cnt2){
- printf("little Y is winner!\n");
- }else{
- printf("five five open\n");
- }
- }
- return ;
- }
- /*
- 5 5
- 1 3 2 5 1
- 1 3 2 5 1
- mig Y 3 2
- */
Contest1692 - 2019寒假集训第三十一场 UPC 11075 Problem D 小P的国际象棋的更多相关文章
- “全栈2019”Java多线程第三十一章:中断正在等待显式锁的线程
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- 2019寒假训练营第三次作业part2 - 实验题
热身题 服务器正在运转着,也不知道这个技术可不可用,万一服务器被弄崩了,那损失可不小. 所以, 决定在虚拟机上试验一下,不小心弄坏了也没关系.需要在的电脑上装上虚拟机和linux系统 安装虚拟机(可参 ...
- 2019寒假训练营第三次作业part1-网络空间安全概论第五章
第五章 网络攻防技术 5.1 网路信息收集技术--网络踩点 黑客入侵系统之前,需要了解目标系统可能存在的: 管理上的安全缺陷和漏洞 网络协议安全缺陷与漏洞 系统安全缺陷与漏洞 黑客实施入侵过程中,需要 ...
- 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化
Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...
- 2019牛客多校第二场F Partition problem(暴搜)题解
题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...
- “全栈2019”Java多线程第三十章:尝试获取锁tryLock()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java第三十一章:二维数组和多维数组详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- Bootstrap <基础三十一>插件概览
在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...
随机推荐
- CentOS7中安装MySQL5.7
安装必要的组件 yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio li ...
- PATH_SEPARATOR
PATH_SEPARATOR是一个常量,在Linux系统中是一个" : "号,Windows上是一个";"号.所以编写程序时最好用常量 PATH_SEPARAT ...
- Linux 小知识翻译 - 「虚拟化技术 续」
这次,继续聊聊「虚拟化技术」. 根据上回的介绍,虚拟化技术可以使「计算机的台数和运行的OS的个数的比例不再是1:1」.这回介绍一下如何使用这个技术. 使用方法之一,「一台计算机上运行多个OS」.从个人 ...
- C语言 用π/4=1-1/3+1/5-1/7+... 求π的近似值
//凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ #include<stdio.h> #include<math.h> void m ...
- 023合并K个链表并排序
#include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...
- Eclipse JVM terminated.exit code=13
今天,在安装Nomad PIM时碰到这个问题,因为这个应用是基于32位的Eclipse平台开发的,而我的电脑是64位的Windows 7,当然安装的JDK也是64位的,于是报错. 搜索了网上,给了许多 ...
- JavaScript的基本包装类型概述与基本包装类型_Number类型
JavaScript的基本包装类型示例 为了便于操作基本类型值,javaScript 提供了 3 个特殊的引用类型:Boolean.Number和 String. 这些类型与其他引用类型相似,但同时也 ...
- 使用c#封装海康SDK出现无法加载 DLL“..\bin\HCNetSDK.dll”: 找不到指定的模块
最近在研究网络摄像头的二次开发,测试了一款海康威视的网络摄像头,程序调试的时候,出现如题的报错. 调试随机自带的demo时,程序运行正常,但当把该程序引入到我自己的程序中时,就开始报错.根据开发软件包 ...
- executequery要求已打开且可用的connection,连接的当前状态为已关闭
问题: executequery要求已打开且可用的connection,连接的当前状态为已关闭 错误原因: 连接的当前状态为已关闭.或者只创建了Connection对象,没有调用Connection. ...
- Rancher2-----了解什么是rancher以及简单部署
个人理解:就是相当于openstack的图形化界面,或者说应用程序的图形化界面,rancher功能就是在图形化界面去管理容器,包括运行容器,创建网络,存储等:rancher有个应用商店,可以根据自己的 ...