HDU1166:敌兵布阵(CDQ分治)
来源:互联网 发布:腾讯算法面试题 编辑:程序博客网 时间:2024/06/05 07:22
传送门
题意:单点修改,区间求和
题解:
当然,线段树或者树状数组或者其他数据结构就是裸题了,其实还有CDQ搞法,空间上直接优化logn。
首先区间求和等同于前缀求和,转化为时间上a< b,序列上c< d的二维偏序带权求值。然后就是裸题了。(相当于求逆序对)。
#include<bits/stdc++.h>using namespace std;const int Maxn=3e5+50;streambuf *ib,*ob;inline int read(){ char ch=ib->sbumpc();int i=0,f=1; while(!isdigit(ch)){if(ch=='-')f=-1;ch=ib->sbumpc();} while(isdigit(ch)){i=(i<<1)+(i<<3)+ch-'0';ch=ib->sbumpc();} return i*f;}int buf[50];inline void W(int x){ if(!x){ob->sputc('0');return;} if(x<0){ob->sputc('-');x=-x;} while(x){buf[++buf[0]]=x%10,x/=10;} while(buf[0])ob->sputc(buf[buf[0]--]+'0'); return;}int n,cnt,cnt2,ans[Maxn];char ch[10];struct query{ int pos,val,type;}q[Maxn],tmp[Maxn];inline void solve(int l,int r){ if(l==r)return; int mid=(l+r)>>1; solve(l,mid); solve(mid+1,r); for(int i=l;i<=r;i++)tmp[i]=q[i]; int head1=l,head2=mid+1,sum=0,pos=l; while(head1<=mid&&head2<=r) { if(tmp[head1].pos<=tmp[head2].pos) { if(tmp[head1].type==1)sum+=tmp[head1].val; q[pos++]=tmp[head1++]; } else { if(tmp[head2].type!=1)ans[tmp[head2].val]+=((tmp[head2].type==2)?-1:1)*sum; q[pos++]=tmp[head2++]; } } while(head1<=mid){q[pos++]=tmp[head1++];} while(head2<=r) { if(tmp[head2].type!=1)ans[tmp[head2].val]+=((tmp[head2].type==2)?-1:1)*sum; q[pos++]=tmp[head2++]; }}int main(){ ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);ib=cin.rdbuf(); ob = std::cout.rdbuf(); int T=read(); for(int tt=1;tt<=T;tt++) { cout<<"Case "<<tt<<":"<<'\n'; memset(q,0,sizeof(q));memset(ans,0,sizeof(ans)); n=read();cnt=cnt2=0; for(int i=1;i<=n;i++){q[++cnt].type=1;q[cnt].pos=i;q[cnt].val=read();} while(cin>>(ch+1),ch[1]!='E') { ++cnt; if(ch[1]=='A') { q[cnt].type=1;q[cnt].pos=read();q[cnt].val=read(); } else if(ch[1]=='S') { q[cnt].type=1;q[cnt].pos=read();q[cnt].val=-read(); } else { ++cnt2;q[cnt].type=2;q[cnt].pos=read()-1;q[cnt].val=cnt2; ++cnt;q[cnt].type=3;q[cnt].pos=read();q[cnt].val=cnt2; } } solve(1,cnt); for(int i=1;i<=cnt2;i++) {W(ans[i]),ob->sputc('\n');} }}
阅读全文
0 0
- HDU1166:敌兵布阵(CDQ分治)
- hdu1166 敌兵布阵(CDQ分治)
- hdu1166(敌兵布阵)
- HDU1166(敌兵布阵)
- hdu1166-分治&线段树-敌兵布阵
- hdu 1166 敌兵布阵(cdq分治)
- hdu 1166 敌兵布阵(cdq分治)
- hdu1166 敌兵布阵(线段树)
- hdu1166-敌兵布阵(线段树)
- HDU1166 敌兵布阵(线段树)
- hdu1166敌兵布阵(大意)树状数组
- hdu1166 敌兵布阵 (线段树模板)
- hdu1166敌兵布阵(线段树)
- HDU1166 敌兵布阵(树状数组)
- 线段树(hdu1166 敌兵布阵)
- HDU1166 敌兵布阵(树状数组)
- hdu1166敌兵布阵(线段树模板)
- hdu1166 敌兵布阵(线段树)
- 设计模式之观察者模式-笔记
- javascript常用工具类库bee.js
- Qt显示PDF之二 QAxWidget, QAxWidget
- unity transform组件position,rotation的世界和本地获取方法
- 实现一个简单的HttpClient连接池(转自博客园)
- HDU1166:敌兵布阵(CDQ分治)
- 线性模型学习笔记
- 移动端前端页面实现学习笔记
- ArcGIS属性表操作
- Spring回顾(一)IoC & DI
- 利用插件扩展Qt本身
- Java JVM——基础概念介绍
- Robot Framework + Appium测试Android设备
- c++ 构造没完成 别的对象不能访问 析构时基类不要调用虚函数