Xiongnu's Land UVALive - 7261

卫青,西汉王朝杰出将领,因对抗匈奴的战役而名声大噪。他是武帝汉武帝的姻亲,同时也是霍去病的舅舅,霍去病同样是一位在对抗匈奴的战争中展现出卓越军事才能的年轻将军。在卫青和霍去病击败匈奴后,匈奴人哀叹失去祁连山和燕支山。汉武帝为了奖励两位将领,决定将从匈奴手中夺来的土地一分为二,给予他们。该问题转化为在平面坐标系上,寻找一条直线,使得直线西侧的土地上的绿洲总面积不小于东侧,并且尽可能地让西侧的土地面积最大化。

Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against
the Xiongnu earned him great acclaim. He was a relative of Emperor Wu because he was the younger
half-brother of Empress Wei Zifu (Emperor Wu’s wife) and the husband of Princess Pingyang. He was
also the uncle of Huo Qubing, another notable Han general who participated in the campaigns against
the Xiongnu and exhibited outstanding military talent even as a teenager.
Defeated by Wei Qing and Huo Qubing, the Xiongnu sang: “Losing my Qilian Mountains, made
my cattle unthriving; Losing my Yanzhi Mountains, made my women lacking rouge.”
The text above is digested from Wikipedia. Since Wei and Huo’s distinguished achievements,
Emperor Wu decided to give them some awards — a piece of land taken by them from Xiongnu. This
piece of land was located in a desert, and there were many oases in it. Emperor Wu wanted to draw
a straight south-to-north dividing line to divide the land into two parts, and gave the western part to
Wei Qing while gave the eastern part to Huo Qubing. There are two rules about the land dividing:
1. The total area of the oases lay in Wei’s land must be larger or equal to the total area of the oases
lay in Huo’s land, and the difference must be as small as possible.
2. Emperor Wu wanted Wei’s land to be as large as possible without violating the rule 1.
To simplify the problem, please consider the piece of land given to Wei and Huo as a square on a
plane. The coordinate of its left bottom corner was (0, 0) and the coordinate of its right top corner
was (R, R). Each oasis in this land could also be considered as a rectangle which was parallel to the
coordinate axes. The equation of the dividing line was like x = n, and n must be an integer. If the
dividing line split an oasis, then Wei owned the western part and Huo owned the eastern part. Please
help Emperor Wu to find out how to draw the dividing line.
Input
The first line of the input is an integer K meaning that there are K (1 ≤ K ≤ 15) test cases.
For each test case:
The first line is an integer R, indicating that the land’s right top corner was at (R, R) (1 ≤ R ≤
1, 000, 000)
Then a line containing an integer N follows, indicating that there were N (0 < N ≤ 10000) oases.
Then N lines follow, each contains four integers L, T, W and H, meaning that there was an
oasis whose coordinate of the left top corner was (L, T), and its width was W and height was H.
(0 ≤ L, T ≤ R, 0 < W, H ≤ R). No oasis overlaps.
Output
For each test case, print an integer n, meaning that Emperor Wu should draw a dividing line whose
equation is x = n. Please note that, in order to satisfy the rules, Emperor might let Wei get the whole
land by drawing a line of x = R if he had to.
Sample Input
2
1000
2
1 1 2 1
5 1 2 1
1000
1
1 1 2 1
Sample Output
5
2

#include<bits/stdc++.h>
#define maxn 1000007
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
ll Sum[maxn<<2],Add[maxn<<2];
ll n;
void PushUp(ll rt){Sum[rt]=Sum[ls]+Sum[rs];}
void PushDown(ll rt,ll ln,ll rn){
	if(Add[rt]){
		Add[ls]+=Add[rt];
		Add[rs]+=Add[rt];
		Sum[ls]+=Add[rt]*ln;
		Sum[rs]+=Add[rt]*rn;
		Add[rt]=0;
	}
}
void Update(ll L,ll R,ll C,ll l,ll r,ll rt){
	if(L <= l && r <= R){
		Sum[rt]+=C*(r-l+1);
		Add[rt]+=C;
		return ;
	}
	ll m=(l+r)>>1;
	PushDown(rt,m-l+1,r-m);
	if(L <= m) Update(L,R,C,l,m,ls);
	if(R >  m) Update(L,R,C,m+1,r,rs);
	PushUp(rt);
}

ll Query(ll L,ll R,ll l,ll r,ll rt){
	if(L <= l && r <= R){
		return Sum[rt];
	}
	ll m=(l+r)>>1;
	PushDown(rt,m-l+1,r-m);

	ll ANS=0;
	if(L <= m) ANS+=Query(L,R,l,m,ls);
	if(R >  m) ANS+=Query(L,R,m+1,r,rs);
	return ANS;
}
int main()
{
    //
    ll t,m,sum;
    ll x,y,w,h;
    scanf("%d",&t);
    while(t--)
    {
        memset(Sum,0,sizeof(Sum));
        memset(Add,0,sizeof(Add));
        scanf("%lld",&n);
        scanf("%lld",&m);
        sum=0;
        for(ll i=1;i<=m;i++)
        {
            scanf("%lld%lld%lld%lld",&x,&y,&w,&h);
            sum+=w*h;
            Update(x+1,x+w,h,1,n,1);
        }
        sum=(sum+1)/2;
        ll l=1,r=n,ans=1,mid;
        ll uu,anss;
        while(l<=r)
        {
            mid=(l+r)>>1;
            ll oo=Query(1,mid,1,n,1);
            if(oo>=sum)
            {
                uu=oo;
                ans=mid;
                r=mid-1;
            }
            else
            {
               l=mid+1;
            }
        }
        l=ans,r=n;
        while(l<=r)
        {
            mid=(l+r)>>1;
            ll oo=Query(1,mid,1,n,1);
            if(oo>uu)
            {
               r=mid-1;
            }
            else
            {
                anss=mid;
                l=mid+1;
            }
        }
        printf("%lld\n",anss);
    }
}

 

This typically means that you attempted to use functionality that needed the current application. To solve this, set up an application context with app.app_context(). See the documentation for more information. Traceback (most recent call last): File "/home/ubuntu/桌面/rapidocr2.py", line 76, in ocr_processing result = future.result(timeout=30) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/_base.py", line 456, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/桌面/rapidocr2.py", line 106, in process_ocr return jsonify({ ^^^^^^^^^ File "/home/ubuntu/xn/lib/python3.12/site-packages/flask/json/__init__.py", line 170, in jsonify return current_app.json.response(*args, **kwargs) # type: ignore[return-value] ^^^^^^^^^^^^^^^^ File "/home/ubuntu/xn/lib/python3.12/site-packages/werkzeug/local.py", line 318, in __get__ obj = instance._get_current_object() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/xn/lib/python3.12/site-packages/werkzeug/local.py", line 519, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed the current application. To solve this, set up an application context with app.app_context(). See the documentation for more information. 2025-05-30 11:23:37,702 - werkzeug - INFO - 172.18.0.10 - - [30/May/2025 11:23:37] "POST /ocr HTTP/1.1" 500 - 2025-05-30 11:23:38,371 - OCRService - DEBUG - 原始文本[0]: 《满江红》 2025-05-30 11:23:38,371 - OCRService - DEBUG - 清洗后文本[0]: 满江红 2025-05-30 11:23:38,371 - OCRService - DEBUG - 原始文本[1]: 宋一岳飞 2025-05-30 11:23:38,371 - OCRService - DEBUG - 清洗后文本[1]: 宋一岳飞 2025-05-30 11:23:38,371 - OCRService - DEBUG - 原始文本[2]: 怒发冲冠,凭阑处、潇潇雨歇。 2025-05-30 11:23:38,371 - OCRService - DEBUG - 清洗后文本[2]: 怒发冲冠凭阑处潇潇雨歇 2025-05-30 11:23:38,371 - OCRService - DEBUG - 原始文本[3]: 抬望眼,仰天长啸,壮怀激烈。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[3]: 抬望眼仰天长啸壮怀激烈 2025-05-30 11:23:38,372 - OCRService - DEBUG - 原始文本[4]: 三十功名尘与土,八千里路云和月。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[4]: 三十功名尘与土八千里路云和月 2025-05-30 11:23:38,372 - OCRService - DEBUG - 原始文本[5]: 莫等闲、白了少年头,空悲切。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[5]: 莫等闲白了少年头空悲切 2025-05-30 11:23:38,372 - OCRService - DEBUG - 原始文本[6]: 靖康耻,犹未雪。臣子恨,何时灭。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[6]: 靖康耻犹未雪臣子恨何时灭 2025-05-30 11:23:38,372 - OCRService - DEBUG - 原始文本[7]: 驾长车,踏破贺兰山缺。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[7]: 驾长车踏破贺兰山缺 2025-05-30 11:23:38,372 - OCRService - DEBUG - 原始文本[8]: 壮志饥餐胡虏肉,笑谈渴饮匈奴血。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[8]: 壮志饥餐胡虏肉笑谈渴饮匈奴血 2025-05-30 11:23:38,372 - OCRService - DEBUG - 原始文本[9]: 待从头、收拾旧山河,朝天阙。 2025-05-30 11:23:38,372 - OCRService - DEBUG - 清洗后文本[9]: 待从头收拾旧山河朝天阙 2025-05-30 11:23:38,372 - OCRService - ERROR - 处理异常: Working outside of application context. This typically means that you attempted to use functionality that needed the current application. To solve this, set up an application context with app.app_context(). See the documentation for more information. Traceback (most recent call last): File "/home/ubuntu/桌面/rapidocr2.py", line 76, in ocr_processing result = future.result(timeout=30) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/_base.py", line 456, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/桌面/rapidocr2.py", line 106, in process_ocr return jsonify({ ^^^^^^^^^ File "/home/ubuntu/xn/lib/python3.12/site-packages/flask/json/__init__.py", line 170, in jsonify return current_app.json.response(*args, **kwargs) # type: ignore[return-value] ^^^^^^^^^^^^^^^^ File "/home/ubuntu/xn/lib/python3.12/site-packages/werkzeug/local.py", line 318, in __get__ obj = instance._get_current_object() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ubuntu/xn/lib/python3.12/site-packages/werkzeug/local.py", line 519, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context.
最新发布
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值