Periodic Signal

描述

Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling.

One day, the device fell on the ground accidentally. Profess X wanted to check whether the device can still work properly. So he ran another n Hz sampling to the fallen device and got B0 ... Bn-1.

To compare two periodic signals, Profess X define the DIFFERENCE of signal A and B as follow:

You may assume that two signals are the same if their DIFFERENCE is small enough. 
Profess X is too busy to calculate this value. So the calculation is on you.

输入

The first line contains a single integer T, indicating the number of test cases.

In each test case, the first line contains an integer n. The second line contains n integers, A0 ... An-1. The third line contains n integers, B0 ... Bn-1.

T≤40 including several small test cases and no more than 4 large test cases.

For small test cases, 0<n≤6⋅103.

For large test cases, 0<n≤6⋅104.

For all test cases, 0≤Ai,Bi<220.

输出

For each test case, print the answer in a single line.

样例输入
2
9
3 0 1 4 1 5 9 2 6
5 3 5 8 9 7 9 3 2
5
1 2 3 4 5
2 3 4 5 1
样例输出
80

0

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread(ch) freopen(ch,"r",stdin) #define fwrite(ch) freopen(ch,"w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const double eps = 1e-3; const int maxn = 61234; struct Line {     LL l,r,val;     bool operator <(const struct Line a)const     {         return l < a.l;     } } ln[maxn]; LL bit[maxn]; LL st,en,t; int n,m,tp; LL to[maxn]; map <LL,int> mp; int id,iid; int Lowbit(int x) {     return x&(-x); } void Add(int x,LL ad) {     x++;     while(x <= id+1)     {         bit[x] += ad;         x += Lowbit(x);     } } LL Sum(int x) {     LL ans = 0;     x++;     while(x)     {         ans += bit[x];         x -= Lowbit(x);     }     return ans; } int Search(LL x) {     int l,r;     l = 0,r = id;     int ans = -1;     while(l <= r)     {         int mid = (l+r)>>1;         if(to[mid] <= x)         {             ans = mid;             l = mid+1;         }         else r = mid-1;     }     return ans; } LL solve() {     LL mx = 0;     memset(bit,0,sizeof(bit));     sort(ln,ln+tp);     int l,r;     l = 0,r = 0;     for(int i = 0; i <= id; ++i)     {         int rr = Search(to[i]+t);         while(r < tp && mp[ln[r].l] <= rr)         {             Add(mp[ln[r].r],ln[r].val);             ++r;         }         while(l < tp && mp[ln[l].l] < i)         {             Add(mp[ln[l].r],-ln[l].val);             ++l;         }         mx = max(mx,Sum(rr)); //        printf("%d,%d %lld\n",i,rr,mx); //        printf("%d %d\n",l,r-1);     }     return mx; } int main() {     LL t2,x,y,val,sum;     while(~scanf("%lld%lld",&t,&t2))     {         mp.clear();         iid = 0;         scanf("%lld",&st);         en = st+t2;         scanf("%d%d",&n,&m);         tp = 0;         sum = 0;         for(int i = 0; i < n; ++i)         {             scanf("%lld%lld%lld",&x,&y,&val);             x += y;             if(st <= x && x <= en)             {                 ln[tp].l = x+y;                 ln[tp].r = (en-x)/(2*y)*(2*y)+ln[tp].l;                 ln[tp++].val = val;                 sum += val;                 to[iid++] = ln[tp-1].l;                 to[iid++] = ln[tp-1].r; //                printf("%lld %lld\n",ln[tp-1].l,ln[tp-1].r);             }         }         for(int i = 0; i < m; ++i)         {             scanf("%lld%lld%lld",&x,&y,&val);             ln[tp].l = x+y;             if(st <= x+2*y && x+2*y <= en)             {                 ln[tp].r = (en-x)/(2*y)*(2*y)+ln[tp].l;             }             else ln[tp].r = x+y;             ln[tp++].val = val;             sum += val;             to[iid++] = ln[tp-1].l;             to[iid++] = ln[tp-1].r;        }         sort(to,to+iid);         id = -1;         for(int i = 0; i < iid; ++i)         {             if(!i || to[i] != to[i-1])             {                 ++id;                 to[id] = to[i];                 mp[to[i]] = id;             }         }         printf("%lld\n",sum-solve());     }     return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值