Task: | Invert |
Sender: | Pietari Kaskela |
Submission time: | 2016-09-13 17:43:07 +0300 |
Language: | C++ |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.12 s | details |
#2 | ACCEPTED | 0.40 s | details |
#3 | ACCEPTED | 0.19 s | details |
#4 | ACCEPTED | 0.42 s | details |
#5 | ACCEPTED | 0.32 s | details |
#6 | ACCEPTED | 0.31 s | details |
#7 | ACCEPTED | 0.06 s | details |
#8 | ACCEPTED | 0.41 s | details |
#9 | ACCEPTED | 0.10 s | details |
#10 | ACCEPTED | 0.40 s | details |
#11 | ACCEPTED | 0.29 s | details |
#12 | ACCEPTED | 0.32 s | details |
#13 | ACCEPTED | 0.14 s | details |
#14 | ACCEPTED | 0.41 s | details |
#15 | ACCEPTED | 0.22 s | details |
#16 | ACCEPTED | 0.41 s | details |
#17 | ACCEPTED | 0.31 s | details |
#18 | ACCEPTED | 0.32 s | details |
#19 | ACCEPTED | 0.32 s | details |
#20 | ACCEPTED | 0.40 s | details |
Compiler report
input/code.cpp: In member function 'void sumsegtree::initfrom(std::vector<long long int>)': input/code.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] while(n < a.size()) ^ input/code.cpp:28:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int i = 0; i < a.size(); i++) ^
Code
#include <bits/stdc++.h> #define i64 long long #define u64 unsigned long long #define i32 int #define u32 unsigned int #define pii pair<int, int> #define pll pair<long long, long long> #define ld long double #define defmod 1000000007 #define ll long long #define mati64(a,b) vector<vector<i64>>(a, vector<i64>(b, 0)); using namespace std; struct sumsegtree{ vector<ll> p; vector<ll> ad; ll n = 1; void initfrom(vector<ll> a){ while(n < a.size()) n*=2; p = vector<ll>(2*n, 0); ad = vector<ll>(2*n, 0); for(int i = 0; i < a.size(); i++) p[i+n] = a[i]; for(int i = n-1; i >= 0; i--) p[i] = p[i*2]+p[i*2+1]; } void init(int nn){ while(n < nn) n*=2; p = vector<ll>(2*n, 0); ad = vector<ll>(2*n, 0); } void add2(int idx, int a, int b, int l, int r, ll x){ if(a >= r || b <= l) return; else if(a >= l && b <= r){ ad[idx]+=x; p[idx]+=(b-a)*x; } else{ int m = (a+b)/2; add2(idx*2, a, m, l, r, x); add2(idx*2+1, m, b, l, r, x); p[idx] = p[idx*2]+p[idx*2+1]+(b-a)*ad[idx]; } } //closed range -> closed-open void add(int l, int r, ll x){ add2(1, 0, n, l, r+1, x); } ll querysum2(int idx, int a, int b, int l, int r, i64 ad2){ if(a >= r || b <= l) return 0; else if(a >= l && b <= r){ return p[idx]+(b-a)*ad2; } else{ int m = (a+b)/2; return querysum2(idx*2, a, m, l, r, ad2+ad[idx])+querysum2(idx*2+1, m, b, l, r, ad2+ad[idx]); } } //closed range -> closed-open ll querysum(int l, int r){ return querysum2(1, 0, n, l, r+1, 0); } }; int main(){ cin.sync_with_stdio(0); cin.tie(0); int q; cin >> q; sumsegtree lol; lol.init(100010); i64 inv = 0; while(q--){ int typ; cin >> typ; if(typ == 1){ int a; cin >> a; if(a != 100000) inv+=lol.querysum(a+1, 100000); lol.add(a, a, 1); } else if(typ == 2){ int a; cin >> a; if(a != 1) inv+=lol.querysum(1, a-1); lol.add(a, a, 1); } else if(typ == 3){ inv = 0; } else if(typ == 4){ cout << inv << endl; } } return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
116682 2 16788 1 18822 1 33396 2 69805 ... |
correct output |
---|
2718 19980 51212 68871 108108 ... |
user output |
---|
2718 19980 51212 68871 108108 ... |
Test 2
Verdict: ACCEPTED
input |
---|
500000 2 98355 2 60191 1 725 2 44093 ... |
correct output |
---|
4591136 513858069 2568146952 3245381276 3834167771 ... |
user output |
---|
4591136 513858069 2568146952 3245381276 3834167771 ... |
Test 3
Verdict: ACCEPTED
input |
---|
279091 1 1715 2 67456 3 4 ... |
correct output |
---|
0 7 8 8 10 ... |
user output |
---|
0 7 8 8 10 ... |
Test 4
Verdict: ACCEPTED
input |
---|
500000 2 61543 1 60828 1 50355 1 40328 ... |
correct output |
---|
68875 35642 76306 9043 53444 ... |
user output |
---|
68875 35642 76306 9043 53444 ... |
Test 5
Verdict: ACCEPTED
input |
---|
401545 1 20583 1 54459 2 5060 2 18660 ... |
correct output |
---|
7135104 67093970 87978149 240022409 353132099 ... |
user output |
---|
7135104 67093970 87978149 240022409 353132099 ... |
Test 6
Verdict: ACCEPTED
input |
---|
500000 2 41157 2 37822 2 72091 3 ... |
correct output |
---|
2 2 2 2 5 ... |
user output |
---|
2 2 2 2 5 ... |
Test 7
Verdict: ACCEPTED
input |
---|
10079 2 52135 1 5407 2 94 1 37058 ... |
correct output |
---|
158419 6571 89379 171235 7854 ... |
user output |
---|
158419 6571 89379 171235 7854 ... |
Test 8
Verdict: ACCEPTED
input |
---|
500000 1 83689 1 70194 1 17167 2 52286 ... |
correct output |
---|
16433238 99620800 142458990 274873638 411495712 ... |
user output |
---|
16433238 99620800 142458990 274873638 411495712 ... |
Test 9
Verdict: ACCEPTED
input |
---|
90533 2 34119 4 3 3 ... |
correct output |
---|
0 0 16 27 0 ... |
user output |
---|
0 0 16 27 0 ... |
Test 10
Verdict: ACCEPTED
input |
---|
500000 2 63136 2 11519 1 91986 1 92054 ... |
correct output |
---|
191 11103 16624 90718 101186 ... |
user output |
---|
191 11103 16624 90718 101186 ... |
Test 11
Verdict: ACCEPTED
input |
---|
323662 1 18238 1 62832 1 53482 1 43447 ... |
correct output |
---|
158283253 677654483 1023537391 121975751 144502380 ... |
user output |
---|
158283253 677654483 1023537391 121975751 144502380 ... |
Test 12
Verdict: ACCEPTED
input |
---|
500000 1 11725 4 1 99988 1 88696 ... |
correct output |
---|
0 21 0 0 0 ... |
user output |
---|
0 21 0 0 0 ... |
Test 13
Verdict: ACCEPTED
input |
---|
125498 1 65018 1 41218 2 93534 2 78789 ... |
correct output |
---|
41 1213 49209 27631 56275 ... |
user output |
---|
41 1213 49209 27631 56275 ... |
Test 14
Verdict: ACCEPTED
input |
---|
500000 2 22435 2 98738 1 57359 2 77794 ... |
correct output |
---|
5948069 17495286 58672750 137825207 482842950 ... |
user output |
---|
5948069 17495286 58672750 137825207 482842950 ... |
Test 15
Verdict: ACCEPTED
input |
---|
343000 1 14035 3 1 36003 2 2676 ... |
correct output |
---|
0 0 0 0 0 ... |
user output |
---|
0 0 0 0 0 ... |
Test 16
Verdict: ACCEPTED
input |
---|
500000 2 67715 2 16062 1 12953 2 43464 ... |
correct output |
---|
14302 25179 21705 13316 26515 ... |
user output |
---|
14302 25179 21705 13316 26515 ... |
Test 17
Verdict: ACCEPTED
input |
---|
372731 1 16333 1 58828 2 1790 2 56950 ... |
correct output |
---|
23264093 180691208 58859618 1007439070 357775222 ... |
user output |
---|
23264093 180691208 58859618 1007439070 357775222 ... |
Test 18
Verdict: ACCEPTED
input |
---|
500000 1 24800 1 30022 3 3 ... |
correct output |
---|
0 0 2 0 7 ... |
user output |
---|
0 0 2 0 7 ... |
Test 19
Verdict: ACCEPTED
input |
---|
398012 1 7709 1 12219 1 95709 1 75173 ... |
correct output |
---|
1733 3398 7720 34986 79727 ... |
user output |
---|
1733 3398 7720 34986 79727 ... |
Test 20
Verdict: ACCEPTED
input |
---|
500000 1 97423 2 25928 1 85082 2 35253 ... |
correct output |
---|
92435834 311688637 486359207 492535988 540678377 ... |
user output |
---|
92435834 311688637 486359207 492535988 540678377 ... |