QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#504872 | #9104. Zayin and Forest | yaLen# | AC ✓ | 371ms | 70672kb | C++14 | 2.2kb | 2024-08-04 16:52:04 | 2024-08-04 16:52:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
#define int long long
#define all(x) (x).begin(), (x).end()
#define peace ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define bcount(t) __builtin_popcount(t)
#define lowbit(x) (x&(-x))
const int inf = 1e18;
int tree[N];
int a[N];
int n;
void add_dandian(int x,int k)
{
for(int i=x;i<=n;i+=lowbit(i))
tree[i]+=k;
}
void add(int pos, int val){ // tree为树状数组
while(pos <= n) // 不能越界
{
tree[pos] += val;
pos += lowbit(pos);
}
}
int query1(int x){ // a[1]..a[x]的和
int ans = 0;
while (x >= 1){
ans = ans + tree[x];
x = x - lowbit(x);
}
return ans;
}
int query(int l, int r){ // 前缀相减
return query1(r) - query1(l-1);//求的是(l,r)里面的所有数的和
}
int f(int x){
return x + lowbit(x);
}
void solve(){
int q;
cin >> n >> q;
vector<int> op(q), opa(q), opb(q);
vector<int> nums;
for(int i = 0 ; i < q; i++){
cin >> op[i] >> opa[i] >> opb[i];
if(op[i] == 1)
{
// 遍历算出所有x的f(x),在离散数组中插入,如果原来有的话就直接加num
int x = opa[i], v = opb[i];
for(int j = x; j <= n; j = f(j))
{
nums.push_back(j);
// cerr << j << endl;
}
}
else {
nums.push_back(opa[i]);
nums.push_back(opb[i]);
}
}
nums.erase(unique(all(nums)), nums.end());
sort(all(nums));
// for(auto it:nums){
// cout << it << " ";
// }
// cout << endl;
int nn = n;
n = nums.size();
int ind;
// cout << query(1,8);
for(int i = 0; i < q; i++){
if(op[i] == 1){
int x = opa[i], v = opb[i];
for(int j = x; j <= nn; j = f(j)){
ind = lower_bound(all(nums),j) - nums.begin() + 1;
// cerr << ind << " " << v << endl;
add(ind,v);
// cerr << query(ind,ind) << endl;
}
}else{
int l = opa[i], r = opb[i];
l = lower_bound(all(nums),l) - nums.begin() + 1;
r = lower_bound(all(nums),r) - nums.begin() + 1;
// cerr << l << r;
cout << query(l, r) << endl;
}
}
// for(int i = 1; i < 1000; i++){
// cout << f(i) << endl;
// }
}
signed main(){
peace;
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 31ms
memory: 8596kb
input:
1000000000 20000 2 384578735 526547442 1 64211261 592970906 1 512065247 448267721 1 44993150 127180320 1 880319036 927623947 1 170536687 572121854 1 896600029 804033011 1 666246328 754201635 1 654066651 179982083 2 240989825 984888006 2 372004567 858916479 2 76127818 98606736 1 181794163 902842353 1...
output:
0 43148875202 17613404710 0 32808578044 28190043566 15641637055 78276219892 14955165236 20262224725 105057452192 17002492367 57916137452 27165464255 72766353838 39458327919 38294102627 264448717384 0 70928519548 279674530483 88885017175 111664599432 69703816663 211506104092 104120007714 34403738515 ...
result:
ok 6649 lines
Test #2:
score: 0
Accepted
time: 371ms
memory: 70672kb
input:
1000000000000000000 100000 1 384578735 526547442 1 64211261 592970906 1 512065247 448267721 1 44993150 127180320 1 880319036 927623947 1 170536687 572121854 1 896600029 804033011 2 666246328 931651408754201635 1 654066651 179982083 1 984888006 240989825 1 372004567 858916479 1 76127818 98606736 1 18...
output:
144205553442 497561506762 903930740555 878459229726 689906696633 859703313829 735231510045 1177875391120 1461659121798 1612314483744 2027462020547 1991476058156 2381861014705 2033973986301 2117738140401 2946661001323 2187638958334 2593068002437 1854182975909 2262561461341 3038788266419 3070435321746...
result:
ok 12561 lines
Test #3:
score: 0
Accepted
time: 367ms
memory: 50200kb
input:
1000000000000000000 100000 1 395661363384578735 526547442 1 843068846064211261 592970906 1 550209039512065247 448267721 1 79278526044993150 127180320 1 193676380880319036 927623947 1 81194098170536687 572121854 1 223947079896600029 804033011 2 921340893666246328 931651408754201635 1 3605022006540666...
output:
0 152171177084 261154456848 98759395571 132967101279 214003153997 109857765028 569583036989 221377921411 943235254745 1036051470108 1290577544766 795223506189 1251272020902 662604469794 907615793356 1786684858550 488655543368 1495388560030 1818589934911 151873013358 717984689394 2039336496945 378996...
result:
ok 12561 lines