2017多校联合第三场/hdu6059 Kanade's trio(tire tree)
来源:互联网 发布:阿里云 vs aws 编辑:程序博客网 时间:2024/06/07 04:10
Kanade's trio
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1079 Accepted Submission(s): 398
There are T test cases.
For each test case , the first line consists of one integer
151 2 3 4 5
6
题意
给出
思路
事先把所有数字插入字典树中,用字典树维护
考虑当前位置
对于cnt[i][j]
数组记录下第i位为j的数之前出现了几次,那么在插入时,对于这一位置cnt[i][nxt ^ 1]
就是此时符合条件的
当我们把一个数从字典树中去掉时,也要考虑去掉这个数留下来的统计值。
这题特殊的地方在于,插入是连续的,之后是连续的删除,所以在插入完成后可以把cnt[i][j]
数组清空一遍,用来记录第i位为j的数被删除了几次。
考虑两个方面:
- 一个是这个数作为cnt[now][nxt ^ 1]
)就好。(这一步操作在Trie::Insert()
里面,与插入时的操作类似)
- 还有一个是这个数作为sum[tmp]
中,这里面还需去掉被删去的cnt[i][nxt]
中,现有的val[tmp]
中,这一部分不能被计入答案,相乘,减去。(sum[tmp] - val[tmp] * cnt[i][nxt]
这一步在函数solve()
里)
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <string.h>#include <map>#include <set>#include <queue>#include <deque>#include <list>#include <bitset>#include <stack>#include <stdlib.h>#define lowbit(x) (x&-x)#define e exp(1.0)#define eps 1e-8//ios::sync_with_stdio(false);// auto start = clock();// cout << (clock() - start) / (double)CLOCKS_PER_SEC<<endl;typedef long long ll;typedef long long LL;using namespace std;typedef unsigned long long ull;const int maxn=5e5+10;ll a[maxn];int bits[32];struct tire{ int tot,root; int val[maxn*30],ch[maxn*30][2]; ll sum[maxn*30],cnt[maxn][2]; int newnode() { val[tot]=sum[tot]=0; ch[tot][0]=ch[tot][1]=-1; return tot++; } void init() { tot=0; root=newnode(); memset(cnt,0,sizeof(cnt)); } void insert(int x,int v) { int now=root,nxt; for(int i=30;i>=0;i--) { nxt=!!(x & bits[i]); if(ch[now][nxt]==-1) ch[now][nxt]=newnode(); now=ch[now][nxt]; cnt[i][nxt]++; sum[now]+=v*cnt[i][nxt^1]; val[now]+=v; } } ll solve(int x) { ll ret=0; int now=root,tmp,nxt; for(int i=30;i>=0;--i) { nxt=!!(x & bits[i]); tmp=ch[now][nxt^1]; now=ch[now][nxt]; if(tmp!=-1) ret+=sum[tmp]-val[tmp]*cnt[i][nxt]; if(now==-1) break; } return ret; }}tire;int n;int main(){ ios::sync_with_stdio(false); int T; cin>>T; bits[0]=1; for(int i=1;i<32;i++) bits[i]=bits[i-1]<<1; while(T--) { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; ll ans=0; tire.init(); for(int i=1;i<=n;i++) tire.insert(a[i],1); memset(tire.cnt,0,sizeof(tire.cnt)); for(int i=1;i<=n;i++) { tire.insert(a[i],-1); ans+=tire.solve(a[i]); } cout<<ans<<endl; } return 0;}
- 2017多校联合第三场/hdu6059 Kanade's trio(tire tree)
- HDU6059-Kanade's trio
- HDU6059 Kanade's trio (Trie)
- HDU6059 Kanade's trio(异或字典树)
- hdu6059 Kanade's trio(字典树)
- hdu 6058/2017多校联合第三场Kanade's sum(链表!)
- HDU 6059-Kanade's trio(多校训练第三场->01字典树)
- 2017多校联合第三场 1003题 hdu 6058 Kanade's sum 链表
- 2017多校联合第六场String/hdu 6096 (tire tree/ac自动机)
- 2017多校训练Contest3: 1004 Kanade's sum hdu6059
- 2017第三次多校联合HDU6059
- Kanade's trio 2017多校#3 trie
- Kanade's trio
- Kanade's trio
- 2017 HDU 6058 多校联合赛 Kanade's sum
- HDU 6059 Kanade's trio
- [HDU]6059 Kanade's trio
- HDU 6059 Kanade's trio
- POJ
- 浏览器同源政策及其规避方法
- 泛型如何进行隐式转换
- 4.1线程组(Thread Group)
- 计算机网络基础10问
- 2017多校联合第三场/hdu6059 Kanade's trio(tire tree)
- Javaweb--- EL表达式 JSTL标准标签库
- bzoj2882 工艺
- Dijkstra算法之地铁修建
- 统计一个字符串中字母出现的次数
- java简单仿winhttp 发送get post请求
- bzoj2176: Strange string
- codeforces 55D. Beautiful numbers
- Android APK反编译就这么简单 详解