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 开发人员,也可以着手 ...
随机推荐
- mssql sqlserver 判断字符串大小写的方法分享
摘要:下文讲述使用sql脚本的方法判断字符串为大小写的方法分享,如下所示 实验环境:sqlserver 2008 R2 实现思路: 将字符串转换为大写或小写然后转换为二进制编码, 然后和源字符串做对比 ...
- Jenkins的构建编号和一个有趣的bug
什么是构建编号 jenkins每个job的每一次构建都有一个属于自己独立的构建编号,每一次的构建结果(成功或失败)所使用的编号都是不相同的. 正确的构建编号:每个job的每次构建结果使用不相同的构建编 ...
- 4.6Python数据处理篇之Matplotlib系列(六)---plt.hist()与plt.hist2d()直方图
目录 目录 前言 (一)直方图 (二)双直方图 目录 前言 今天我们学习的是直方图,导入的函数是: plt.hist(x=x, bins=10) 与plt.hist2D(x=x, y=y) (一)直方 ...
- 数组实例的 copyWithin()
用途:在当前数组内部,将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组.也就是说,使用这个方法,会修改数组本身. 用法:Array.prototype.copyWithin(targ ...
- 详解javascript立即执行函数表达式(IIFE)
立即执行函数,就是在定义函数的时候直接执行,这里不是申明函数而是一个函数表达式 1.问题 在javascript中,每一个函数在被调用的时候都会创建一个执行上下文,在函数内部定义的变量和函数只能在该函 ...
- Java多线程(三)如何创建线程
点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更新中~ 正式篇: Java多线程(一) 什么是线程 Java多线程(二)关于多线程的CPU密集型和IO密集型这件事 Java多线程(三)如何 ...
- C. Queen Codeforces Round #549 (Div. 2) dfs
C. Queen time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Rancher学习笔记-----1.分享链接
http://blog.csdn.net/csdn_duomaomao/article/details/76156334
- socket编程解决粘包和丢包问题
##socket 丢包粘包解决方式 采用固定头部长度(一般为4个字节),包头保存的是包体的长度 header+body 包头+包体 下面的例子不是按照上图中规定的格式编写的,但是思路都是一样的,先读出 ...
- 写了12年JS也未必全了解的连续赋值运算
引子 var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);/ ...