emmm…标题卡着长度上限…

LCT板题…(ε=ε=ε=┏(゜ロ゜;)┛)

CODE

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getchar());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getchar());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 30005;
int n, q, w[MAXN];
namespace LCT {
#define ls ch[x][0]
#define rs ch[x][1]
int ch[MAXN][2], fa[MAXN], sum[MAXN];
bool rev[MAXN];
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline bool get(int x) { return ch[fa[x]][1] == x; }
inline void upd(int x) { sum[x] = w[x] + sum[ls] + sum[rs]; }
inline void mt(int x) { if(rev[x]) rev[x]^=1, rev[ls]^=1, rev[rs]^=1, swap(ls, rs); }
void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
inline void rot(int x) {
int y = fa[x], z = fa[y];
bool l = get(x), r = l^1;
if(!isr(y)) ch[z][get(y)] = x;
fa[ch[x][r]] = y, fa[y] = x, fa[x] = z;
ch[y][l] = ch[x][r], ch[x][r] = y;
upd(y), upd(x);
}
inline void splay(int x) { mtpath(x);
for(; !isr(x); rot(x))
if(!isr(fa[x])) rot(get(x) == get(fa[x]) ? fa[x] : x);
}
inline void access(int x) { int y = 0;
for(; x; x=fa[y=x]) splay(x), ch[x][1] = y, upd(x);
}
inline void bert(int x) { access(x), splay(x), rev[x]^=1; }
inline int sert(int x) {
access(x), splay(x);
while(ch[x][0]) x=ch[x][0];
return x;
}
inline bool judge(int x, int y) { bert(x); return sert(y) == x; }
inline bool link(int x, int y) { if(judge(x, y)) return 0; fa[x] = y; return 1; }
inline int querysum(int x, int y) { if(x == y) return w[x]; if(!judge(x, y)) return -1; return sum[y]; }
inline void modify(int x, int val) {
bert(x); w[x] = val; upd(x);
}
}
int main () {
read(n);
for(int i = 1; i <= n; ++i)
read(w[i]), LCT::sum[i] = w[i];
read(q);
int x, y; char s[2];
while(q--) {
while(!isalpha(s[0]=getchar()));
while(isalpha(s[1]=getchar()));
read(x), read(y);
if(s[0] == 'b') puts(LCT::link(x, y) ? "yes" : "no");
else if(s[0] == 'p') LCT::modify(x, y);
else {
int ans = LCT::querysum(x, y);
if(~ans) printf("%d\n", ans);
else puts("impossible");
}
}
}

最新文章

  1. WordPress建站 新手入门
  2. Learning Roadmap of Robotic Operating System (ROS)
  3. openjdk 完全编译指南
  4. Java操作Excel: POI不能创建xlsm问题的方法(源自StackOverFlow)
  5. JAVA应用程序占用CPU、内存过高分析过程
  6. hdu Dylans loves tree [LCA] (树链剖分)
  7. 命令行参数的处理函数getopt
  8. C++结构体:默认构造函数,复制构造函数,重载=运算符
  9. Couchbase 中的分布式储存
  10. 第一章 oracle数据库基础
  11. iOS上new Date异常解决办法
  12. LeetCode - 626. Exchange Seats
  13. 好用好玩的Python包
  14. 全排列问题Ⅱ(Java实现)
  15. 数组的splice方法
  16. 快速掌握activity的生命周期
  17. java代码--实现随机输出10个随机数,并显示最大值,最小值
  18. 解决 jenkins 下使用 HTML Publisher 插件后查看 html 报告显示不正常
  19. MongoTemplate复合条件查询
  20. python笔记三:函数式编程

热门文章

  1. C语言--分支结构
  2. lab3:系统调用
  3. (四)Spring 的 bean 管理(注解方式)
  4. HDU 4614 线段树+二分查找
  5. Thinkphp自定义生成缩略图尺寸的方法
  6. S03_CH02_AXI_DMA PL发送数据到PS
  7. S02_CH05_UBOOT实验Enter a post title
  8. 【GCN】图卷积网络初探——基于图(Graph)的傅里叶变换和卷积
  9. 2-MySQL DBA笔记-MySQL安装部署和入门
  10. 微信小程使用getCurrentPages函数操作父级数据