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 开发人员,也可以着手 ...
随机推荐
- 获取spring applicationcontext数据连接connection
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ...
- HTML—标签与表格 、框架
1.标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Linux内核线程kernel thread详解--Linux进程的管理与调度(十)
内核线程 为什么需要内核线程 Linux内核可以看作一个服务进程(管理软硬件资源,响应用户进程的种种合理以及不合理的请求). 内核需要多个执行流并行,为了防止可能的阻塞,支持多线程是必要的. 内核线程 ...
- Python的变量以及类型
1.程序是用来处理数据的,变量就是用来存储数据的 num1 = 100 2.为了更充分的利用内存空间以及更有效率的管理内存,变量是有不同的类型 3.怎样知道一个变量的类型呢? 3.1 在python ...
- numpy中stack、hstack,vstack,dstack函数功能解释
https://blog.csdn.net/Riverhope/article/details/78922006 https://blog.csdn.net/ygys1234/article/deta ...
- 对Can We MakeOperating SystemsReliable and Secure 的翻译
摘要:微内核-相对于大内核(monolithic kernels)来说,由于它的 lower performance,长期以来被认为是不可接受的.而现在,由于它潜 在的高可靠性(higher reli ...
- 在线编辑器ACE Editor的使用
ACE 是一个开源的.独立的.基于浏览器的代码编辑器,可以嵌入到任何web页面或JavaScript应用程序中.ACE支持超过60种语言语法高亮,并能够处理代码多达400万行的大型文档.ACE开发团队 ...
- Linux系统在信息社会的发展
Linux系统在信息社会的发展 随着信息技术的高速发展并迅速渗透到社会生活的各个方面,Linux日益成为人们学习.工作.生活不可缺少的基本工具,再过不了几年,不会使用Linux,就会象不识字一样使人举 ...
- JAVA序列化和反序列化XML
package com.lss.utils; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.Bu ...
- 匆忙记录 编译linux kernel zImage
arm的板子. 自己要定制下内核. 下载源码 cp 模板配置 .config make menuconfig 进行定制化 之后make zImage {注意 交叉编译 gcc 也要配置的} 之后 ./ ...